排序Java集合

排序Java集合

我有一个Java集合:

Collection<CustomObject> list = new ArrayList<CustomObject>();

CustomObjectid现在在显示列表之前有一个字段我想按此排序这个集合id

有什么方法可以做到这一点吗?


慕尼黑8549860
浏览 360回答 3
3回答

慕田峪9158850

使用比较器:List<CustomObject>&nbsp;list&nbsp;=&nbsp;new&nbsp;ArrayList<CustomObject>();Comparator<CustomObject>&nbsp;comparator&nbsp;=&nbsp;new&nbsp;Comparator<CustomObject>()&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;@Override &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;int&nbsp;compare(CustomObject&nbsp;left,&nbsp;CustomObject&nbsp;right)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;left.getId()&nbsp;-&nbsp;right.getId();&nbsp;//&nbsp;use&nbsp;your&nbsp;logic &nbsp;&nbsp;&nbsp;&nbsp;}};Collections.sort(list,&nbsp;comparator);&nbsp;//&nbsp;use&nbsp;the&nbsp;comparator&nbsp;as&nbsp;much&nbsp;as&nbsp;u&nbsp;wantSystem.out.println(list);另外,如果CustomObject实现Comparable,那么只需使用Collections.sort(list)使用JDK 8,语法更简单。List<CustomObject>&nbsp;list&nbsp;=&nbsp;getCustomObjectList();Collections.sort(list,&nbsp;(left,&nbsp;right)&nbsp;->&nbsp;left.getId()&nbsp;-&nbsp;right.getId());System.out.println(list);更简单List<CustomObject>&nbsp;list&nbsp;=&nbsp;getCustomObjectList();list.sort((left,&nbsp;right)&nbsp;->&nbsp;left.getId()&nbsp;-&nbsp;right.getId());System.out.println(list);简单List<CustomObject>&nbsp;list&nbsp;=&nbsp;getCustomObjectList();list.sort(Comparator.comparing(CustomObject::getId));System.out.println(list);显然,初始代码也可以用于JDK 8。

慕勒3428872

问题是:“排序集合”。所以你不能使用Collections.sort(List<T> l, Comparator<? super T> comparator)。一些技巧:对于收集类型:Comparator<String>&nbsp;defaultComparator&nbsp;=&nbsp;new&nbsp;Comparator<String>()&nbsp;{ &nbsp;&nbsp;&nbsp;@Override &nbsp;&nbsp;&nbsp;public&nbsp;int&nbsp;compare(String&nbsp;o1,&nbsp;String&nbsp;o2)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;o1.compareTo(o2); &nbsp;&nbsp;&nbsp;}};Collection<String>&nbsp;collection&nbsp;=&nbsp;getSomeStringCollection();String[]&nbsp;strings&nbsp;=&nbsp;collection.toArray(new&nbsp;String[collection.size()]);Arrays.sort(strings,&nbsp;defaultComparator);List<String>&nbsp;sortedStrings&nbsp;=&nbsp;Arrays.asList(strings);Collection<String>&nbsp;collection&nbsp;=&nbsp;getSomeStringCollection();List<String>&nbsp;list&nbsp;=&nbsp;new&nbsp;ArrayList(collection);Collections.sort(list,&nbsp;defaultComparator);collection&nbsp;=&nbsp;list;&nbsp;//&nbsp;if&nbsp;you&nbsp;wish对于列表类型:List<String>&nbsp;list&nbsp;=&nbsp;getSomeStringList();Collections.sort(list,&nbsp;defaultComparator);对于Set类型:Set<String>&nbsp;set&nbsp;=&nbsp;getSomeStringSet();//&nbsp;Than&nbsp;steps&nbsp;like&nbsp;in&nbsp;'For&nbsp;Collection&nbsp;type'&nbsp;section&nbsp;or&nbsp;use&nbsp;java.util.TreeSet//&nbsp;TreeSet&nbsp;sample://&nbsp;Sorted&nbsp;using&nbsp;java.lang.Comparable.Set<String>&nbsp;naturalSorted&nbsp;=&nbsp;new&nbsp;TreeSet(set);Set<String>&nbsp;set&nbsp;=&nbsp;getSomeStringSet();Set<String>&nbsp;sortedSet&nbsp;=&nbsp;new&nbsp;TreeSet(defaultComparator);sortedSet.addAll(set);Java 8版本。有java.util.List#sort(Comparator<? super E> c)方法List<String>&nbsp;list&nbsp;=&nbsp;getSomeStringList();list.sort(defaultComparator);要么List<String>&nbsp;list&nbsp;=&nbsp;getSomeStringList();list.sort((String&nbsp;o1,&nbsp;String&nbsp;o2)&nbsp;->&nbsp;o1.compareTo(o2));或者对于实现Comparable的类型:List<String>&nbsp;list&nbsp;=&nbsp;getSomeStringList();list.sort(String::compareTo);

交互式爱情

一个稍微不同的例子说,如果你有一个类没有实现Comparable但你仍然想要在一个字段或方法上对它进行排序。Collections.sort(allMatching,&nbsp;new&nbsp;Comparator<ClassOne>()&nbsp;{ &nbsp;&nbsp;@Override&nbsp;public&nbsp;int&nbsp;compare(final&nbsp;ClassOne&nbsp;o1,&nbsp;final&nbsp;ClassOne&nbsp;o2)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(o1.getMethodToSort()&nbsp;>&nbsp;o2.getMethodToSort())&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;1; &nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;else&nbsp;if&nbsp;(o1.getMethodToSort()&nbsp;<&nbsp;o2.getMethodToSort())&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;-1; &nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;0; &nbsp;&nbsp;}});
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java