欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java8如何通过Lambda处理List集合

程序员文章站 2022-03-28 16:14:30
这篇文章主要介绍了java8如何通过lambda处理list集合,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 java 8新增的...

这篇文章主要介绍了java8如何通过lambda处理list集合,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

java 8新增的lambda表达式,我们可以用简洁高效的代码来处理list。

1、遍历

public static void main(string[] args) {
  list<user> userlist = lists.newarraylist();

  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 ->{
   system.out.println(user.getname());
  });

 }

运行结果:

Java8如何通过Lambda处理List集合

2、list转为map

public static void main(string[] args) {
  list<user> userlist = lists.newarraylist();//存放user对象集合

  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<long,user> usermap = userlist.stream().collect(collectors.tomap(user::getid, a -> a,(k1, k2)->k1));
  system.out.println(usermap);

 }

运行结果:

Java8如何通过Lambda处理List集合

3、将list分组:list里面的对象元素,以某个属性来分组

public static void main(string[] args) {
  list<user> userlist = lists.newarraylist();//存放user对象集合

  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<string, list<user>> groupby = userlist.stream().collect(collectors.groupingby(user::getname));
  system.out.println(groupby);

 }

运行结果:

Java8如何通过Lambda处理List集合

4、过滤:从集合中过滤出来符合条件的元素

public static void main(string[] args) {
  list<user> userlist = lists.newarraylist();//存放user对象集合

  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<user> filterlist = userlist.stream().filter(user -> user.getname().equals("张三")).collect(collectors.tolist());
  filterlist.stream().foreach(user ->{
   system.out.println(user.getname());
  });

 }

运行结果:

Java8如何通过Lambda处理List集合

5、求和:将集合中的数据按照某个属性求和

public static void main(string[] args) {
  list<user> userlist = lists.newarraylist();//存放user对象集合

  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);


  //取出名字为张三的用户
  int totalage = userlist.stream().maptoint(user::getage).sum();
  system.out.println("和:" + totalage);

 }

运行结果:

Java8如何通过Lambda处理List集合

6、从list转为map,key与value 一 一对应

public static void main(string[] args) {
  list<user> userlist = lists.newarraylist();

  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);

  map<long/*id*/,user> usermap = userlist.stream().collect(collectors.tomap(user::getid, user -> user));
  system.out.println("tomap:" + jsonarray.tojsonstring(usermap));

 }

运行结果:

Java8如何通过Lambda处理List集合

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。