写点什么

jdk8 Stream 流中将集合转成 map,重复 key 处理,统计最大值,获取某个属性集合等 10 种最常用方法

作者:javaNice
  • 2023-11-19
    四川
  • 本文字数:2935 字

    阅读完需:约 10 分钟

jdk8 Stream 流中将集合转成 map,重复 key 处理,统计最大值,获取某个属性集合等 10 种最常用方法

🔊stream10 种常用方法

//1、list转map,指定key-value,key,value是对象中的某个属性值.Map<String,String> userMap1 = userList.stream().collect(Collectors.toMap(User::getId,User::getName));System.out.println("1->"+userMap1);
//2、list转map 指定key-value,key为属性值,value是对象本身Map<String,User> userMap2 = userList.stream().collect(Collectors.toMap(User::getId,User->User));System.out.println("2->"+userMap2);
//3、list转map 指定key-value,value是对象本身,Function.identity()是简洁写法,返回对象本身Map<String,User> userMap3 = userList.stream().collect(Collectors.toMap(User::getId, Function.identity()));System.out.println("3->"+userMap3);
//4、指定key-value,value是对象本身,Function.identity()是简洁写法,返回对象本身,遇到key 冲突的解决办法,选择第二个key的值value2覆盖第一个key的值value1。Map<String,User> userMap4 = userList.stream().collect(Collectors.toMap(User::getName, Function.identity(),(value1,value2)->value2));System.out.println("4->"+userMap4);
//5、指定key-value,按照name进行分组 value是对象集合Map<String, List<User>> userMap5 = userList.stream().collect(Collectors.groupingBy(User::getName));System.out.println("5->"+userMap5);
//6.统计所有ageInteger totalAge = userList.stream().map(User::getAge).reduce(0, Integer::sum);System.out.println("6总年龄->"+totalAge);
//7.只对名称为zhangsan的age 进行统计汇总Integer totalAgeFilter = userList.stream().filter(t -> t.getName().equals("zhangsan")).map(User::getAge).reduce(0, Integer::sum);System.out.println("7总年龄filter->"+totalAgeFilter);
//8.获取最大ageInteger maxAge = userList.stream().map(User::getAge).max((x, y) -> x - y).get();System.out.println("8最大年龄->"+maxAge);
//9.循环打印userList.stream().forEach(user -> System.out.println("9->"+user));
//10.获取名称集合List<String> nameList = userList.stream().map(User::getName).collect(Collectors.toList());System.out.println("10名称集合->"+ JSON.toJSONString(nameList));
复制代码



/** * @author gzh -》javaNice * 用户类 */public class User {
/** * id */ private String id;
/** * 名称 */ private String name;
/** * 年龄 */ private Integer age;
public String getId() { return id; }
public void setId(String id) { this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public Integer getAge() { return age; }
public void setAge(Integer age) { this.age = age; }
@Override public String toString() { return "User{" + "id='" + id + '\'' + ", name='" + name + '\'' + ", age=" + age + '}'; }
public User() { }
public User(String id, String name, Integer age) { this.id = id; this.name = name; this.age = age; }}
复制代码


import com.alibaba.fastjson.JSON;import org.assertj.core.util.Lists;
import java.util.List;import java.util.Map;import java.util.function.Function;import java.util.stream.Collectors;
/** * @author gzh -》javaNice * * */public class StreamDemo { public static void main(String[] args) { User user1=new User("1","zhangsan",1); User user2=new User("2","zhangsan",2); User user3=new User("3","lisi",2);
List<User> userList= Lists.newArrayList(); userList.add(user1); userList.add(user2); userList.add(user3);

//1、list转map,指定key-value,key,value是对象中的某个属性值. Map<String,String> userMap1 = userList.stream().collect(Collectors.toMap(User::getId,User::getName)); System.out.println("1->"+userMap1);
//2、list转map 指定key-value,key为属性值,value是对象本身 Map<String,User> userMap2 = userList.stream().collect(Collectors.toMap(User::getId,User->User)); System.out.println("2->"+userMap2);
//3、list转map 指定key-value,value是对象本身,Function.identity()是简洁写法,返回对象本身 Map<String,User> userMap3 = userList.stream().collect(Collectors.toMap(User::getId, Function.identity())); System.out.println("3->"+userMap3);
//4、指定key-value,value是对象本身,Function.identity()是简洁写法,返回对象本身,遇到key 冲突的解决办法,选择第二个key的值value2覆盖第一个key的值value1。 Map<String,User> userMap4 = userList.stream().collect(Collectors.toMap(User::getName, Function.identity(),(value1,value2)->value2)); System.out.println("4->"+userMap4);
//5、指定key-value,按照name进行分组 value是对象集合 Map<String, List<User>> userMap5 = userList.stream().collect(Collectors.groupingBy(User::getName)); System.out.println("5->"+userMap5);
//6.统计所有age Integer totalAge = userList.stream().map(User::getAge).reduce(0, Integer::sum); System.out.println("6总年龄->"+totalAge);
//7.只对名称为zhangsan的age 进行统计汇总 Integer totalAgeFilter = userList.stream().filter(t -> t.getName().equals("zhangsan")).map(User::getAge).reduce(0, Integer::sum); System.out.println("7总年龄filter->"+totalAgeFilter);
//8.获取最大age Integer maxAge = userList.stream().map(User::getAge).max((x, y) -> x - y).get(); System.out.println("8最大年龄->"+maxAge);
//9.循环打印 userList.stream().forEach(user -> System.out.println("9->"+user));
//10.获取名称集合 List<String> nameList = userList.stream().map(User::getName).collect(Collectors.toList()); System.out.println("10名称集合->"+ JSON.toJSONString(nameList));


}}
复制代码

jdk8 Stream 流中将集合转成 map,重复 key 处理,统计最大值,获取某个属性集合等 10 种最常用方法

🧣最后的话

🖲要熟练掌握技巧,一定多多练习:纸上得来终觉浅,绝知此事要躬行



发布于: 刚刚阅读数: 5
用户头像

javaNice

关注

还未添加个人签名 2023-11-02 加入

还未添加个人简介

评论

发布
暂无评论
jdk8 Stream流中将集合转成map,重复key处理,统计最大值,获取某个属性集合等10种最常用方法_Java_javaNice_InfoQ写作社区