/** * 升序/降序/随机数/求和/平均数/最大数/最小数 * @Author: Mr.li * @Date: 2020/9/8 */ @Test public void demo1() { //对象类型stream排序 List<User> users = new ArrayList<User>() { { add(new User("a", "1983/12/24", 36)); add(new User("a", "1993/12/24", 26)); add(new User("a", "1973/12/24", 46)); add(new User("a", "1995/12/24", 24)); add(new User("a", "1981/12/24", 38)); add(new User("a", "1991/12/24", 28)); } }; /*升序*/ List<User> ascUsers = users.stream().sorted(Comparator.comparing(User::getAge)).collect(Collectors.toList()); System.out.println("升序:" + ascUsers); /*降序*/ List<User> descUsers = users.stream().sorted(Comparator.comparing(User::getAge).reversed()).collect(Collectors.toList()); System.out.println("降序:" + descUsers); /*随机数*/ Random random = new Random(); //random.ints(0,100).limit(10).sorted().forEach(System.out::print); random.ints(0, 100).limit(10).sorted().forEach(obj -> { System.out.print(obj + ","); }); List<Integer> integers = Arrays.asList(1, 2, 13, 4, 15, 6, 17, 8, 19); IntSummaryStatistics stats = integers.stream().mapToInt((x) -> x).summaryStatistics(); System.out.println("列表中最大的数 : " + stats.getMax()); System.out.println("列表中最小的数 : " + stats.getMin()); System.out.println("所有数之和 : " + stats.getSum()); System.out.println("平均数 : " + stats.getAverage()); System.out.println("ToString : " + stats.toString()); }
result: