Java8常⽤stream流处理集合转换
⼀、List转Map
list.stream().Map(MultiMemberDTO::getMultiMemberGuid, MultiMemberDTO::getMultiMemberName));⼆、List转Listjava stream
List<WxBrandAuthRespDTO> respList = wxBrandAuthClientService.listWxBrand();
List<String> collect = respList.stream().map(WxBrandAuthRespDTO::getMultiMemberGuid).List());三、List转换
List<User> list = new ArrayList<User>();
list =  Arrays.asList(
new User("⼩强", 11, "男"),
new User("⼩玲", 15, "⼥"),
new User("⼩虎", 23, "男"),
new User("⼩⾬", 26, "⼥"),
new User("⼩飞", 19, "男"),
new User("⼩玲", 15, "⼥")
);
//分组
Map<String, List<User>> listMap = list.stream().upingBy(User::getSex));
for(String key:listMap.keySet()){
System.out.print(key+"组:");
<(key).forEach(user -> System.out.Name()));
System.out.println();
}
//排序
list.stream().sorted(Comparatorparing(user-> Age()))
.forEach(user -> System.out.Name()));
//过滤
list.stream().filter(user -> Sex().equals("男")).List())
.forEach(user -> System.out.Name()));
//多条件去重
list.stream().llectingAndThen(
Comparatorparing(user -> Age() + ";" + Name()))), ArrayList::new))
.
forEach(user -> System.out.Name()));
//最⼩值
Integer min = list.stream().mapToInt(User::getAge).min().getAsInt();
//最⼤值
Integer max = list.stream().mapToInt(User::getAge).max().getAsInt();
//平均值
Double average = list.stream().mapToInt(User::getAge).average().getAsDouble();
//和
Integer sum = list.stream().mapToInt(User::getAge).sum();
System.out.println("最⼩值:"+min+", 最⼤值"+max+", 平均值:"+average+", 和:"+sum);
//分组求和
Map<String, IntSummaryStatistics> collect = list.stream().upingBy(User::getSex, Collectors.summarizingInt(User::getAge))); IntSummaryStatistics statistics1 = ("男");
IntSummaryStatistics statistics2 = ("⼥");
System.out.Sum());
System.out.Average());
System.out.Max());
System.out.Min());
System.out.Count());
System.out.Sum());
System.out.Average());
System.out.Max());
System.out.Min());
System.out.Count());
//提取list中两个属性值,转为map
Map<String, String> userMap = list.stream().Map(User::getName, User::getSex));
System.out.Json(userMap))
//取出所有名字
List<String> names = list.stream().map(User::getName).List());
System.out.Json(names))

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。