package proxy; import java.util.*; import java.util.function.Function; import java.util.stream.Collectors; import java.util.stream.Stream; public class TestStream { public static void main(String[] args) { List<User> users = Arrays.asList( new User(1L, "Jack", 23), new User(2L, "Mary", 31), new User(3L, "Tom", 17), new User(4L, "Cat", 16), new User(5L, "Peter", 19), new User(6L, "Joe", 40), new User(7L, "Lee", 19), new User(8L, "Sam", 33), new User(9L, "Bill", 40), new User(10L, "Bob", 15) ); //List<User> collect = users.stream().filter(TestStream::test).collect(Collectors.toList()); //年龄大于30的 List<User> ages = users.stream().filter(user -> user.getAge()>30).collect(Collectors.toList()); //年龄大于30岁的 List<String> names = users.stream().filter(user -> user.getAge() > 30).map(user -> user.getName()).collect(Collectors.toList()); //按年龄从小到大排列 Stream<User> orders=users.stream().sorted(Comparator.comparing(user ->user.getAge())); // reversed //按年龄从大到小排列 Stream<User> orders1=users.stream().sorted(Comparator.comparing(User::getAge).reversed()); // 获取年龄最小的 Optional<User> min = users.stream().min(Comparator.comparing(user -> user.getAge())); //获取年龄最大的 Optional<User> max = users.stream().max(Comparator.comparing(user -> user.getAge())); //获得所有的年龄综合 long count = users.stream().map(User::getAge).reduce((user, user2) -> user+user2).get().longValue(); // users.stream().map(user -> {new HashMap<>().put(user.getId(),user)}). //把list转成Map ,key为用户id,值为用户本身 Map<Long, Object> longObjectMap = users.stream().collect(Collectors.toMap(user -> user.getId(),user -> user)); //按年龄分组 Map<Integer, List<User>> integerListMap = users.stream().collect(Collectors.groupingBy(User::getAge)); //按年龄统计 Map<Integer, Long> integerLongMap = users.stream().collect(Collectors.groupingBy(User::getAge, Collectors.counting())); //按年年龄分组,返回每组的name最⻓长的 Function<User, Integer> function = p -> -p.getName().length(); Map<Integer, Optional<User>> integerOptionalMap = users.stream(). collect(Collectors.groupingBy(User::getAge,Collectors.maxBy(Comparator.comparing(function).reversed()))); //取得分组年龄的平均值 Double collect = users.stream().collect(Collectors.averagingDouble(User::getAge)); ages.forEach(user -> System.out.println(user.getName())); System.out.println("==============="); names.forEach(s -> System.out.println(s)); System.out.println("==============="); orders.forEach(user -> System.out.println(user.getName()+","+user.getAge())); System.out.println("==============="); orders1.forEach(user -> System.out.println(user.getName()+","+user.getAge())); System.out.println("==============="); System.out.println(min.get().getName()+","+min.get().getAge()); System.out.println("==============="); System.out.println(max.get().getName()+","+max.get().getAge()); System.out.println("==============="); System.out.println(count); System.out.println("==============="); Iterator<Map.Entry<Long, Object>> iterator = longObjectMap.entrySet().iterator(); while (iterator.hasNext()){ Map.Entry<Long, Object> next = iterator.next(); System.out.println(next.getKey()+","+next.getValue().toString()); } System.out.println("==============="); Iterator<Map.Entry<Integer, List<User>>> iterator1 = integerListMap.entrySet().iterator(); while (iterator1.hasNext()){ Map.Entry<Integer, List<User>> next = iterator1.next(); System.out.println(next.getKey()+","+next.getValue().toString()); } System.out.println("==============="); Iterator<Map.Entry<Integer, Long>> iterator2 = integerLongMap.entrySet().iterator(); while (iterator2.hasNext()){ Map.Entry<Integer,Long> next = iterator2.next(); System.out.println(next.getKey()+","+next.getValue().toString()); } System.out.println("==============="); Iterator<Map.Entry<Integer, Optional<User>>> iterator3 = integerOptionalMap.entrySet().iterator(); while (iterator3.hasNext()){ Map.Entry<Integer, Optional<User>> next = iterator3.next(); System.out.println(next.getKey()+","+next.getValue().toString()); } System.out.println("==============="); System.out.println(collect); }; private static boolean test(User user){ return user.getAge()>30; } }
java8 stream 的简单使用
最新推荐文章于 2024-09-01 15:30:33 发布