Collections.sort
只有一个参数
参数为要排序的集合,将集合升序排序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| import com.ledao.entity.Student;
import java.util.*;
public class Test {
public static void main(String[] args) {
List<Integer> integerList = Arrays.asList(1, 4, 2, 5, 3); Collections.sort(integerList); integerList.forEach(System.out::println); System.out.println("---------------");
List<Character> characterList = Arrays.asList('a','v','b','c'); Collections.sort(characterList); characterList.forEach(System.out::println); System.out.println("---------------");
List<String> stringList = Arrays.asList("abc", "fcd", "bfd"); Collections.sort(stringList); stringList.forEach(System.out::println); System.out.println("---------------");
List<Long> longList = Arrays.asList(111L,332L,22L); Collections.sort(longList); longList.forEach(System.out::println); } }
|
有两个参数
第一个参数是要排序的集合,第二个参数是比较器,排序结果由比较器确定(比较器内第一个参数在前面就是升序,在后面就是降序)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
| import com.ledao.entity.Student;
import java.util.*;
public class Test {
public static void main(String[] args) {
List<Integer> integerList = Arrays.asList(1, 4, 2, 5, 3); Collections.sort(integerList, new Comparator<Integer>() { @Override public int compare(Integer o1, Integer o2) { return o2-o1; } }); integerList.forEach(System.out::println); System.out.println("---------------");
List<Character> characterList = Arrays.asList('a','v','b','c','A'); Collections.sort(characterList, new Comparator<Character>() { @Override public int compare(Character o1, Character o2) { return o2.compareTo(o1); } }); characterList.forEach(System.out::println); System.out.println("---------------");
List<String> stringList = Arrays.asList("abc", "fcd", "bfd","Abd"); Collections.sort(stringList, new Comparator<String>() { @Override public int compare(String o1, String o2) { return o2.compareToIgnoreCase(o1); } }); stringList.forEach(System.out::println); System.out.println("---------------");
List<Long> longList = Arrays.asList(111L,332L,22L); Collections.sort(longList, new Comparator<Long>() { @Override public int compare(Long o1, Long o2) { return o2.compareTo(o1); } }); longList.forEach(System.out::println); } }
|
List.sort
只有一个参数,参数为比较器,一般推荐用这种方法排序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| import com.ledao.entity.Student;
import java.util.*;
public class Test {
public static void main(String[] args) {
List<Integer> integerList = Arrays.asList(1, 4, 2, 5, 3); integerList.sort(new Comparator<Integer>() { @Override public int compare(Integer o1, Integer o2) { return o2-o1; } }); integerList.forEach(System.out::println); System.out.println("---------------");
List<Character> characterList = Arrays.asList('a','v','b','c','A'); characterList.sort(new Comparator<Character>() { @Override public int compare(Character o1, Character o2) { return o2.compareTo(o1); } }); characterList.forEach(System.out::println); System.out.println("---------------");
List<String> stringList = Arrays.asList("abc", "fcd", "bfd","Abd"); stringList.sort(new Comparator<String>() { @Override public int compare(String o1, String o2) { return o2.compareTo(o1); } }); stringList.forEach(System.out::println); System.out.println("---------------");
List<Long> longList = Arrays.asList(111L,332L,22L); longList.sort(new Comparator<Long>() { @Override public int compare(Long o1, Long o2) { return o2.compareTo(o1); } }); longList.forEach(System.out::println); } }
|
Stream流操作
通过Stream流操作会返回一个新的并且经过排序的List集合,具体步骤为:①创建要排序集合的流 ②进行排序 ③生成新集合
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| import com.ledao.entity.Student;
import java.util.*; import java.util.stream.Collectors;
public class Test {
public static void main(String[] args) {
List<Integer> integerList = Arrays.asList(1, 4, 2, 5, 3); List<Integer> resultList = integerList .stream() .sorted(new Comparator<Integer>() { @Override public int compare(Integer o1, Integer o2) { return o2 - o1; } }) .collect(Collectors.toList()); resultList.forEach(System.out::println); } }
|