Java8 Stream详解

测试实体类

public class User {
      private String id;
      private String name;
      private Integer age;
      private Integer sex;
       public User() {
    }
    public User(String id, String name, Integer age, Integer sex) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.sex = sex;
    }
      .....省略get set
}      
//测试数据
List<User> userList=new ArrayList<>();
        userList.add(new User("1","test-1",21,1));
        userList.add(new User("1","test-1",17,0));
        userList.add(new User("1","test-1",17,0));
        userList.add(new User("1","test-1",18,1));

1.filter 对元素进行过滤,当过滤条件为true时返回对应元素

  • 过滤性别为0的用户
   List<User> userTmp = userList.stream().
                 filter(user -> user.getSex() == 0).
                     collect(Collectors.toList());
         System.out.println(userTmp.size());
  • 过滤性别为0的用户,并把年龄设置为17
List<User> userTmp2 = userList.stream().
                filter(user -> user.getSex() == 0)
                .peek(user -> user.setAge(17))
                .collect(Collectors.toList());
 System.out.println(userTmp2.size());

2.sorted 对元素按指定规则进行排序

  • 按年龄升序排序
        //方法一
        List<User> newUserList2=userList.stream().sorted((user1,user2)->{
            return user1.getAge().compareTo(user2.getAge());
        }).collect(Collectors.toList());
        //方法二
        List<User> newUserList = userList.stream().
                 sorted(Comparator.comparing(User::getAge)).
                  collect(Collectors.toList());
        //遍历集合
        newUserList2.forEach(System.out::println);
  • 按年龄降序排序
List<User> newUserList = userList.stream().
                 sorted(Comparator.comparing(User::getAge).reversed())
                  .collect(Collectors.toList());
        //遍历集合
        newUserList2.forEach(System.out::println);

3.map

         // 从集合中提取id
        List<String> ids = userList.stream().map(user -> user.getId()).collect(Collectors.toList());
        ids.forEach(System.out::println);

4.mapToDouble、mapToInt、mapToLong

  • 求年龄平均数
        //方法一
        double asDouble = userList.stream().mapToLong(User::getAge).average().getAsDouble();
        System.out.println(asDouble);
        //方法二
        Double avgAge = userList.stream().collect(Collectors.averagingDouble(User::getAge));
        System.out.println(avgAge);
  • 年龄最大值
 int maxAge = userList.stream().mapToInt(User::getAge).max().getAsInt();
 System.out.println(maxAge);
  • 年龄最小值
int minAge = userList.stream().mapToInt(User::getAge).min().getAsInt();
System.out.println(minAge);
  • 年龄求和
 long sumAge = userList.stream().mapToLong(user -> user.getAge()).sum();
 System.out.println(sumAge);

4.count从Stream中获取指定元素数量

// 获取年龄为0的用户数量
long count = userList.stream().filter(user -> user.getSex() == 0).count();

5.limit获取指定数量的元素

 // 获取前两条数据
 List<User> newUserList = userList.stream().limit(2).collect(Collectors.toList());
 newUserList.forEach(System.out::println);

6.skip 跳过指定个数的元素获取后面的元素

 List<User> newUserList = userList.stream().skip(2).collect(Collectors.toList());
 newUserList.forEach(System.out::println);

7.map转list

Map<String, User> collect = userList.stream().collect(Collectors.toMap(user -> user.getId(), user -> user));

8.去重

//根据年龄去重
 List<User> users = userList.stream().
                collect(collectingAndThen(
                toCollection(() -> new TreeSet<>(comparingLong(User::getAge))), ArrayList::new));
发布了36 篇原创文章 · 获赞 3 · 访问量 6846
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章