package com.gang; import java.security.SecureRandom; import java.text.DecimalFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.time.LocalDate; import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; import java.util.stream.Stream; public class Harbor { public static void main(String[] args) throws ParseException { /* List<String> list = Arrays.asList("hello", "world", "Java"); list.stream() .filter(str -> str.length()>=5) .forEach(str-> System.out.println(str)); list.stream().limit(2).forEach(su -> System.out.println(su));*/ /*List<Integer> list1 = Arrays.asList(1, 5, 6, 8, 4, 6);*/ //list1.stream().sorted(Comparator.reverseOrder()).forEach(str -> System.out.println(str)); /* System.out.println(list1.stream().max(Integer::compareTo).get()); System.out.println(list1.stream().min(Integer::compareTo).get());*/ /*list1.stream().map(str ->str+10).forEach(str -> System.out.println(str));*/ // System.out.println(list1.stream().reduce((sum, num) -> sum + num).get()); /*List<Integer> collect = list1.stream().filter(num -> num % 2 == 0).collect(Collectors.toList()); System.out.println(collect);*/ ArrayList<Student> list = new ArrayList<>(); list.add(new Student("张三",16)); list.add(new Student("张四",19)); list.add(new Student("李五",15)); list.add(new Student("张马六",16)); System.out.println(list); Collections.sort(list, new Comparator<Student>() { @Override public int compare(Student o1, Student o2) { //按照年龄升序排序 //如果两个人年龄相同,再使用姓名的第一个字比较 int result = o1.getName().charAt(0) - o2.getName().charAt(0); return result; } }); System.out.println(list); } }比较器一:比较推荐这一种写法
Collections.sort(list1, new Comparator<Student>() { @Override public int compare(Student s1, Student s2) { return s1.getName().compareTo(s2.getName()); } }); 比较器二: Collections.sort(list1, new Comparator<Student>() { @Override public int compare(Student o1, Student o2) { //按照年龄升序排序 //如果两个人年龄相同,再使用姓名的第一个字比较 int result = o1.getName().charAt(0) - o2.getName().charAt(0); return result; } });
比较器三:
list1.stream() .distinct() .sorted((o1, o2) -> o1.getName().compareTo(o2.getName())) .forEach(author -> System.out.println(author));
排序中的倒序排列:
List<String> list = Arrays.asList("张三", "李四", "张三"); list.stream().sorted(Comparator.reverseOrder()).forEach(str -> System.out.println(str)); // Collections.sort(list); System.out.println(list);
=========================================================================
package com.gang; import java.security.SecureRandom; import java.text.DecimalFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.time.LocalDate; import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; import java.util.stream.Stream; public class Harbor { public static void main(String[] args) throws ParseException { /* List<String> list = Arrays.asList("hello", "world", "Java"); list.stream() .filter(str -> str.length()>=5) .forEach(str-> System.out.println(str)); list.stream().limit(2).forEach(su -> System.out.println(su));*/ /*List<Integer> list1 = Arrays.asList(1, 5, 6, 8, 4, 6);*/ //list1.stream().sorted(Comparator.reverseOrder()).forEach(str -> System.out.println(str)); /* System.out.println(list1.stream().max(Integer::compareTo).get()); System.out.println(list1.stream().min(Integer::compareTo).get());*/ /*list1.stream().map(str ->str+10).forEach(str -> System.out.println(str));*/ // System.out.println(list1.stream().reduce((sum, num) -> sum + num).get()); /*List<Integer> collect = list1.stream().filter(num -> num % 2 == 0).collect(Collectors.toList()); System.out.println(collect);*/ ArrayList<Student> list = new ArrayList<>(); list.add(new Student("张三",16)); list.add(new Student("张四",19)); list.add(new Student("李五",15)); list.add(new Student("张马六",16)); System.out.println(list); Collections.sort(list, new Comparator<Student>() { @Override public int compare(Student o1, Student o2) { //按照年龄升序排序 //如果两个人年龄相同,再使用姓名的第一个字比较 int result = o1.getName().charAt(0) - o2.getName().charAt(0); return result; } }); System.out.println(list); } }