java8stream如何(获取最大值,获取最小值,排序)操作呢?
下文笔者讲述java8之stream获取一个列表的最大值和最小值及排序的方法及示例分享,如下所示
获取最大值,最小值,排序的实现思路
stream获取最大值 可使用max()方法 获取最小值 可使用min()方法 排序可借助sort方法例:stream最大值,最小值,排序的示例
import java.io.Serializable; import java.util.Arraylist; import java.util.Comparator; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.function.Function; import java.util.function.Predicate; import java.util.stream.Collectors; public class User implements Serializable { private static final long serialVersionUID = 1220193146286930256L; private String id; private String name; private String groupId; private int num; private int age; /**get set 省略**/ private static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) { Map<Object, Boolean> seen = new ConcurrentHashMap<>(); return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null; } public static void main(String[] args) { ArrayList<User> list = new ArrayList<>(10); list.add(new User("1", "1号", "A", 10, 10)); list.add(new User("2", "2号", "A", 20, 10)); list.add(new User("3", "3号", "E", 50, 12)); list.add(new User("4", "4号", "B", 60, 20)); list.add(new User("5", "5号", "C", 80, 40)); list.add(new User("6", "6号", "B", 220, 20)); list.add(new User("7", "7号", "A", 110, 2)); list.add(new User("8", "8号", "D", 110, 68)); //去重复获取组 System.out.println("----------去重复获取组-------------------"); list.stream().filter(distinctByKey(User::getGroupId)).map(User::getGroupId).forEach(s -> { System.out.println(s); }); //从第2个开始(下标) System.out.println("-------------从第2个开始----------------"); list.stream().skip(2).forEach(s -> { System.out.println(s); }); //取10以内的 System.out.println("-------------取10以内的----------------"); list.stream().filter(user -> user.getAge() <= 10).forEach(s -> { System.out.println(s); }); //取最大 System.out.println("----------------取最大-------------"); User user = list.stream().max(Comparator.comparing(User::getNum)).get(); System.out.println(user); //分组求合 System.out.println("---------------分组求合--------------"); list.stream().collect(Collectors.groupingBy(User::getGroupId, Collectors.summingInt(User::getNum))).forEach((k, v) -> { System.out.println("K:" + k + "-V:" + v); }); //分组取组内最大 System.out.println("--------------分组取组内最大---------------"); list.stream().collect(Collectors.groupingBy(User::getGroupId, Collectors.maxBy(Comparator.comparingInt(User::getNum)))).forEach((k, v) -> { System.out.println("K:" + k + "-V:" + v.get()); }); //分组取组内最小 System.out.println("------------分组取组内最小-----------------"); list.stream().collect(Collectors.groupingBy(User::getGroupId, Collectors.minBy(Comparator.comparingInt(User::getNum)))).forEach((k, v) -> { System.out.println("K:" + k + "-V:" + v.get()); }); //分组聚合多列 System.out.println("---------------分组聚合多列计算--------------"); list.stream().collect(Collectors.groupingBy(User::getGroupId, Collectors.collectingAndThen(Collectors.toList(), users -> { Double[] ints = new Double[2]; Double asDouble1 = users.stream().mapToInt(User::getNum).average().getAsDouble(); Double asDouble2 = users.stream().mapToInt(User::getAge).average().getAsDouble(); ints[0] = asDouble1; ints[1] = asDouble2; return ints; }))).forEach((k, v) -> { System.out.println("K:" + k + "-V:" + v[0] + "," + v[1]); }); //分组后组内排序 System.out.println("---------------分组后组内排序--------------"); list.stream().collect(Collectors.groupingBy(User::getGroupId, Collectors.collectingAndThen(Collectors.toList(), users -> { users.sort(Comparator.comparing(User::getNum).reversed()); return users; }))).forEach((k, v) -> { System.out.println("k:" + k); System.out.println("v----->:"); v.forEach(s -> { System.out.println(s); }); }); } }
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。