JAVA-Stream流的常用方法

目录

1、快速创建List

不使用Stream流

Stream流写法

2、取对象的某一列:

(1)、遍历

(2)、Stream流:map

3、过滤,条件筛选出目标对象

(1)、遍历加 if 

(2)、Stream流:filter

4、分组

(1)、遍历加 if

(2)、Stream流:groupingBy 

5、求和

(1)、int、double、long:

6、Map、List互转

list转map:

(1)、遍历:

(2)、stream流: 

 map转list:

(1)、遍历:

(2)、stream流: 

7、做判断

(1)、anyMatch():

(2)、allMatch(): 

(3)、noneMatch():

(4)、求取目标和:

 8、合并List

合并多个List:

 9、分页

 10、排序


1、快速创建List
不使用Stream流
List<User> userList = new ArrayList<>();
userList.add(user1);
userList.add(user2);
userList.add(user3);
Stream流写法
// stream流,创建的是动态数组,可以添加元素
List<User> userList = Stream.of(user1, user2, user3).collect(Collectors.toList());
2、取对象的某一列:

拿上面userList举例,我取出list中所有user的name属性放到一个新的list中:

(1)、遍历
// 遍历
List<String> userNameList = new ArrayList<>();
for (User user : userList) {
	userNameList.add(user.getName());
}
(2)、Stream流:map
List<String> userNameList = userList.stream().map(User::getName).collect(Collectors.toList());
3、过滤,条件筛选出目标对象

还拿上面的userList,比如我要筛选出userList中name不为空的user

(1)、遍历加 if 
List<User> newUserList = new ArrayList<>();
// if判断
for (User user : userList) {
	if(user.getName() != null) {
		newUserList.add(user); 
	}
}
(2)、Stream流:filter
// 获取userName不为空的user的List
List<User> userList = userList.stream().filter(user-> user.getName() != null).collect(Collectors.toList());
4、分组

把userList中的user根据年龄分组:

(1)、遍历加 if
 Map<String, List<User>> map = new HashMap<>();
// if判断
for (User user : userList) {
    //存在map
    if (map.containsKey(user.getAge())) {
        map.put(user.getAge(), new ArrayList());
    }else{
        map.get(user.getAge()).add(user);
    }
   
}
(2)、Stream流:groupingBy 
Map<String, List<User>> map =userList.stream().collect( Collectors.groupingBy(User::getAge, Collectors.toList()));
5、求和
(1)、int、double、long:

求和的普通遍历方式跟上面差不多;

double max = userList.stream().mapToDouble(User::getHeight).sum();
6、Map、List互转
list转map:
(1)、遍历:
Map<String, User> userMap = new Map<>();
    for (User user : userList) {
		userMap.put(user.getName(), user);
	}
(2)、stream流: 

代码中使用(key1,key2)->key2表达式主要解决重复的key就使用key2覆盖前面的key1

	Map<String, User> userMap= userList.stream().collect(Collectors.toMap(User::getName, Function.identity(),(key1, key2)->key2));

 value是某一属性的值,例如,key是id,value是name:

Map<String, String> userMap = userList.stream().
      collect(Collectors.toMap(User::getId, User::getName));
 map转list:
(1)、遍历:
List<User> userList = new List<>();
    for (String userName : userMap.keySet()) {
		userList.add(userMap.get(userName));
	}
(2)、stream流: 
   List<User> userList = userMap.entrySet().stream().map(e ->e.getValue()).collect(Collectors.toList());
7、做判断
(1)、anyMatch():

判断的条件里,任意一个元素成功,返回true;
比如上面的userlList,我想判断是否有height > 175的:

userList.stream().anyMatch(user -> user.getHeight() > 175);
(2)、allMatch(): 

allMatch:判断条件里的元素,所有的都是,返回true;
比如上面的userlList,我想判断是否全部height > 175的: 

userList.stream().allMatch(user -> user.getHeight() > 175);
(3)、noneMatch():

与allMatch相反,判断条件里的元素,所有的都不是,返回true 

 userList.stream().noneMatch(user -> user.getHeight() > 175);
(4)、求取目标和:
 userList.stream().filter(user -> user.getHeight() > 175).count();
 8、合并List
合并多个List:
 List<Integer> list1 = Arrays.asList(1, 2, 3);
   List<Integer> list2 = Arrays.asList(4, 5, 6);
   List<Integer> list3 = Arrays.asList(7, 8, 9);
   List<Integer> mergedList = Stream.of(list1, list2, list3).flatMap(Collection::stream).collect(Collectors.toList());
   System.out.println(mergedList);// [1, 2, 3, 4, 5, 6, 7, 8, 9]
 9、分页
        Map<String,Object> map = Maps.newHashMap();
        List list = new ArrayList();
        //list -> 当前页所有数据
        map.put("list",list.stream().skip((long) (pageNo - 1) * pageSize).limit(pageSize).collect(Collectors.toList()));
        //count -> 记录总条数
        map.put("count",list.size());
 10、排序
// 根据名称正序排序
list.stream().sorted(Comparator.comparing(User::getName)).collect(Collectors.toList());
// 根据名称倒序排序
list.stream().sorted(Comparator.comparing(User::getName).reversed()).collect(Collectors.toList());

  • 51
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值