Alexey Dudarev Asked:2020-04-21 17:43:12 +0000 UTC2020-04-21 17:43:12 +0000 UTC 2020-04-21 17:43:12 +0000 UTC 创建现有列表的副本 772 下午好。有一个填充的 ArrayList(例如 list1)。如何创建一个新的 ArrayList (list2),它将是 list1 的副本但已排序? java 1 个回答 Voted Best Answer Peter Samokhin 2020-04-21T17:52:18Z2020-04-21T17:52:18Z TreeSet将删除所有重复项并对您的原始列表进行排序。 ArrayList<String> list = new ArrayList<>(); list.add("three"); list.add("three"); list.add("two"); list.add("one"); TreeSet<String> set = new TreeSet<>(list); System.out.println(list); // [three, three, two, one] System.out.println(set); // [one, three, two] 或者像这样(不丢失重复项): ArrayList<String> list = new ArrayList<>(); list.add("three"); list.add("two"); list.add("one"); ArrayList<String> copyOfList = new ArrayList<>(list); copyOfList.sort(Comparator.naturalOrder()); System.out.println(list); // [three, three, two, one] System.out.println(copyOfList); // [one, three, three, two]
TreeSet
将删除所有重复项并对您的原始列表进行排序。或者像这样(不丢失重复项):