日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java8 stream流操作集合交集,差集,并集,过滤,分组,去重,排序,聚合等

發布時間:2023/12/10 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java8 stream流操作集合交集,差集,并集,过滤,分组,去重,排序,聚合等 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

測試對象

public class Person {private String name;private Integer age;private Integer weight;public Person() {}public Integer getWeight() {return weight;}public void setWeight(Integer weight) {this.weight = weight;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;} }

測試數據

兩個list的交集、差集、并集

package Map;import com.alibaba.fastjson.JSON;import java.util.*; import java.util.stream.Collectors;public class ListStream {public static void main(String[] args) {List<Person> list = new ArrayList<Person>();Person person1 = new Person();person1.setName("張三");person1.setAge(18);person1.setWeight(50);Person person2 = new Person();person2.setName("李四");person2.setAge(26);person2.setWeight(70);Person person3 = new Person();person3.setName("張三");person3.setAge(26);person3.setWeight(60);list.add(person1);list.add(person2);list.add(person3);List<String> list1 = new ArrayList<>();list1.add("1");list1.add("2");list1.add("3");List<String> list2 = new ArrayList<>();list2.add("2");// 交集List<String> intersection = list1.stream().filter(item -> list2.contains(item)).collect(Collectors.toList());System.out.println("交集:"+JSON.toJSONString(intersection));// 差集 (list1 - list2)List<String> reduce1 = list1.stream().filter(item -> !list2.contains(item)).collect(Collectors.toList());System.out.println("差集1:"+JSON.toJSONString(reduce1));// 差集 (list2 - list1)List<String> reduce2 = list2.stream().filter(item -> !list1.contains(item)).collect(Collectors.toList());System.out.println("差集2:"+JSON.toJSONString(reduce2));// 并集List<String> listAll = list1.parallelStream().collect(Collectors.toList());List<String> listAll2 = list2.parallelStream().collect(Collectors.toList());listAll.addAll(listAll2);System.out.println("并集:"+JSON.toJSONString(listAll));// 去重并集List<String> listAllDistinct = listAll.stream().distinct().collect(Collectors.toList());System.out.println("去重并集:"+JSON.toJSONString(listAllDistinct));}}

過濾

//過濾出符合條件的數據 保留name等于張三 List<Person> temp = list.stream().filter(a -> a.getName().equals("張三")).collect(Collectors.toList()); //過濾出符合條件的數據 過濾name等于張三 List<Person> temp1 = list.stream().filter(a -> !a.getName().equals("張三")).collect(Collectors.toList());

分組

//List 以Name分組 Map<String, List<Person>> groupBy = list.stream().collect(Collectors.groupingBy(Person::getName));

去重

//去重 List<String> collect = list.stream().map(Person::getName).distinct().collect(Collectors.toList()); //去重 返回list對象 List<Person> unique = list.stream().collect(collectingAndThen(toCollection(() -> new TreeSet<>(comparingLong(Person::getAge))), ArrayList::new));

排序

//排序-->升序 List<Person> sortName = list.stream().sorted(Comparator.comparing(Person::getAge)).collect(Collectors.toList()); String sortName1 = JSON.toJSONString(sortName); System.out.println("升序:"+sortName1); //排序-->降序 List<Person> temp2 = list.stream().sorted(Comparator.comparing(Person::getAge).reversed()).collect(Collectors.toList()); String sortName2 = JSON.toJSONString(temp2); System.out.println("降序:"+sortName2); //排序-->兩個字段排序 如果年齡相同,再使用體重排序 List<Person> temp3 = list.stream().sorted(Comparator.comparing(Person::getAge).thenComparing(Person::getName)).collect(Collectors.toList()); String sortName3 = JSON.toJSONString(temp3); System.out.println("兩個字段排序:"+sortName3);

聚合-->求和,最大值,最小值,平均值,統計

//mapToInt(),mapToDouble(),mapToLong()等 list.stream().mapToDouble(Person::getHeight).sum();//和 list.stream().mapToDouble(Person::getHeight).max();//最大 list.stream().mapToDouble(Person::getHeight).min();//最小 list.stream().mapToDouble(Person::getHeight).average();//平均值 list.stream().mapToDouble(Person::getHeight).count();//統計

list中本身存的就是基本類型的數字

List<Integer> listInteger = Arrays.asList(10,20,30); listInteger.add(10); listInteger.add(20); Integer sum= listInteger.stream().reduce(Integer::sum).orElse(0); Integer max = listInteger.stream().reduce(Integer::max).orElse(0);

List集合拼接成逗號分隔的字符串

List<String> listStr = Arrays.asList("a", "b", "c"); //String自帶方式 String s2 = String.join(",", listStr); System.out.println(s2); //用stream流 String s = listStr.stream().collect(Collectors.joining(",")); System.out.println(s);

總結

以上是生活随笔為你收集整理的java8 stream流操作集合交集,差集,并集,过滤,分组,去重,排序,聚合等的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。