Flask接口签名sign原理与实例代码浅析
273
2022-12-15
java8如何通过Lambda处理List集合
这篇文章主要介绍了java8如何通过Lambda处理List集合,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
Java 8新增的Lambda表达式,我们可以用简洁高效的代码来处理List。
1、遍历
public static void main(String[] args) {
List
User user1 = new User(1L, "张三", 24);
User user2 = new User(2L, "李四", 27);
User user3 = new User(3L, "王五", 21);
userList.add(user1);
userList.add(user2);
userList.add(user3);
userList.stream().forEach(user ->{
KJQrnijgES System.out.println(user.getName());
});
}
运行结果:
2、list转为Map
public static void main(String[] args) {
List
User user1 = new User(1L, "张三", 24);
User user2 = new User(2L, "李四", 27);
User user3 = new User(3L, "王五", 21);
userList.add(user1);
userList.add(user2);
userList.add(user3);
//ID为key,转为Map
Map
System.out.println(userMap);
}
运行结果:
3、将List分组:List里面的对象元素,以某个属性来分组
public static void main(String[] args) {
List
User user1 = new User(1L, "张三", 24);
User user2 = new User(2L, "李四", 27);
User user3 = new User(3L, "王五", 21);
User user4 = new User(4L, "张三", 22);
User user5 = new User(5L, "李四", 20);
User user6 = new User(6L, "王五", 28);
userList.add(user1);
userList.add(user2);
userList.add(user3);
userList.add(user4);
userList.add(user5);
userList.add(user6);
//根据name来将userList分组
Map
System.out.println(groupBy);
}
运行结果:
4、过滤:从集合中过滤出来符合条件的元素
public static void main(String[] args) {
List
User user1 = new User(1L, "张三", 24);
User user2 = new User(2L, "李四", 27);
User user3 = new User(3L, "王五", 21);
User user4 = new User(4L, "张三", 22);
User user5 = new User(5L, "李四", 20);
User user6 = new User(6L, "王五", 28);
userList.add(user1);
userList.add(user2);
userList.add(user3);
userList.add(user4);
userList.add(user5);
userList.add(user6);
//取出名字为张三的用户
List
filterList.stream().forEach(user ->{
System.out.println(user.getName());
});
}
运行结果:
5、求和:将集合中的数据按照某个属性求和
public static void main(String[] args) {
List
User user1 = new User(1L, "张三", 24);
User user2 = new User(2L, "李四", 27);
User user3 = new User(3L, "王五", 21);
User user4 = new User(4L, "张三", 22);
User user5 = new User(5L, "李四", 20);
User user6 = new User(6L, "王五", 28);
userList.add(user1);
userList.addKJQrnijgES(user2);
userList.add(user3);
userList.add(user4);
userList.add(user5);
userList.add(user6);
//取出名字为张三的用户
int totalAge = userList.stream().mapToInt(User::getAge).sum();
System.out.println("和:" + totalAge);
}
运行结果:
6、从List转为Map,key与value 一 一对应
public static void main(String[] args) {
List
User user1 = new User(1L, "张三", 24);
User user2 = new User(2L, "李四", 27);
User user3 = new User(3L, "王五", 21);
User user4 = new User(4L, "张三", 22);
User user5 = new User(5L, "李四", 20);
User user6 = new User(6L, "王五", 28);
userList.addhttp://(user1);
userList.add(user2);
userList.add(user3);
userList.add(user4);
userList.add(user5);
userList.add(user6);
Map
System.out.println("toMap:" + jsONArray.toJSONString(userMap));
}
运行结果:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持KJQrnijgES我们。
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~