/**
* @PACKAGE_NAME: com.ewaytek.edf.web.controllers.jdk1_8demo
* @USER:小清新
* @date: 2021年02月02日 13:34
* @PROJECT_NAME: CGCP
**/
import java.util.*;
import java.util.stream.Collectors;
import static java.util.stream.Collectors.toList;
public class Java8Tester {
public static void main(String args[]){
// 计算空字符串
List<String> strings = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl");
long count = 0;
// 删除空字符串
List<String> filtered = new ArrayList<>();
String mergedString ="";
List<Integer> squaresList =new ArrayList<>();
List<Integer> integers = Arrays.asList(1,2,13,4,15,6,17,8,19);
List<Integer> numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5);
Random random = new Random();
System.out.println("使用 Java 8: ");
System.out.println("列表: " +strings);
//filter 方法用于通过设置的条件过滤出元素
count = strings.stream().filter(string->string.isEmpty()).count();
System.out.println("空字符串数量为: " + count);
count = strings.stream().filter(string -> string.length() == 3).count();
System.out.println("字符串长度为 3 的数量为: " + count);
//Collectors 类实现了很多归约操作,例如将流转换成集合和聚合元素
filtered = strings.stream().filter(string ->!string.isEmpty()).collect(Collectors.toList());
System.out.println("筛选后的列表: " + filtered);
mergedString = strings.stream().filter(string ->!string.isEmpty()).collect(Collectors.joining(", "));
System.out.println("合并字符串: " + mergedString);
squaresList = numbers.stream().map( i ->i*i).distinct().collect(Collectors.toList());
System.out.println("Squares List: " + squaresList);
System.out.println("列表: " +integers);
//IntSummaryStatistics和stream类配合使用,在java.util包中,主要用于统计整形数组中元素的最大值,最小值,平均值,个数,元素总和等等
IntSummaryStatistics stats = integers.stream().mapToInt((x) ->x).summaryStatistics();
System.out.println("列表中最大的数 : " + stats.getMax());
System.out.println("列表中最小的数 : " + stats.getMin());
System.out.println("所有数之和 : " + stats.getSum());
System.out.println("平均数 : " + stats.getAverage());
System.out.println("随机数: ");
random.ints().limit(10).sorted().forEach(System.out::println);
// 并行处理
count = strings.parallelStream().filter(string -> string.isEmpty()).count();
System.out.println("空字符串的数量为: " + count);
System.out.println("---交集 intersection---");
List<String> list1 = new ArrayList<String>();
list1.add("1");
list1.add("2");
list1.add("3");
list1.add("5");
list1.add("6");
List<String> list2 = new ArrayList<String>();
list2.add("2");
list2.add("3");
list2.add("7");
list2.add("8");
// 交集
List<String> intersection = list1.stream().filter(item -> list2.contains(item)).collect(toList());
System.out.println("---交集 intersection---");
intersection.parallelStream().forEach(System.out :: println);
// 差集 (list1 - list2)
List<String> reduce1 = list1.stream().filter(item -> !list2.contains(item)).collect(toList());
System.out.println("---差集 reduce1 (list1 - list2)---");
reduce1.parallelStream().forEach(System.out :: println);
// 差集 (list2 - list1)
List<String> reduce2 = list2.stream().filter(item -> !list1.contains(item)).collect(toList());
System.out.println("---差集 reduce2 (list2 - list1)---");
reduce2.parallelStream().forEach(System.out :: println);
// 并集
List<String> listAll = list1.parallelStream().collect(toList());
List<String> listAll2 = list2.parallelStream().collect(toList());
listAll.addAll(listAll2);
System.out.println("---并集 listAll---");
listAll.parallelStream().forEachOrdered(System.out :: println);
// 去重并集
List<String> listAllDistinct = listAll.stream().distinct().collect(toList());
System.out.println("---得到去重并集 listAllDistinct---");
listAllDistinct.parallelStream().forEachOrdered(System.out :: println);
//对集合某个对象中的值就行排序
List<StudentInfo> studentList = new ArrayList<>();
StudentInfo studentInfo1=new StudentInfo();
studentInfo1.setName("李小明44");
studentInfo1.setAge(23);
studentInfo1.setGender("女");
StudentInfo studentInfo2=new StudentInfo();
studentInfo2.setName("无敌1111");
studentInfo2.setAge(19);
studentInfo2.setGender("一个死太监");
StudentInfo studentInfo3=new StudentInfo();
studentInfo3.setName("小清新66");
studentInfo3.setAge(22);
studentInfo3.setGender("男");
studentList.add(studentInfo3);
studentList.add(studentInfo2);
studentList.add(studentInfo1);
//按年龄排序(Integer类型)
List<StudentInfo> studentsSortName = studentList.stream().sorted(Comparator.comparing(StudentInfo::getAge)).collect(Collectors.toList());
studentsSortName.forEach(
System.out::println
);
//根据某个对象字段重新生成list
List<String>nameList = studentList.stream().map(StudentInfo::getName).collect(Collectors.toList());
//按年龄倒序序(Integer类型)
List<StudentInfo> studentsSortName2 = studentList.stream().sorted(Comparator.comparing(StudentInfo::getAge).reversed()).collect(Collectors.toList());
studentsSortName2.forEach(
System.out::println
);
//List根据某个字段分组
Map<String,List<StudentInfo>> sexGroupMap = studentsSortName.stream().collect(Collectors.groupingBy(StudentInfo::getGender));
sexGroupMap.forEach((key, value) -> {
System.out.println("key:" + key + " value:" + value);
});
// 求两个list交集中某个对象值相同的对象集合
List<StudentInfo> studentList2 = new ArrayList<>();
StudentInfo studentInfo4=new StudentInfo();
studentInfo4.setName("李小明");
studentInfo4.setAge(20);
studentInfo4.setGender("女");
StudentInfo studentInfo5=new StudentInfo();
studentInfo5.setName("无敌");
studentInfo5.setAge(19);
studentInfo5.setGender("一个死太监");
StudentInfo studentInfo6=new StudentInfo();
studentInfo6.setName("小清新");
studentInfo6.setAge(22);
studentInfo6.setGender("男");
studentList2.add(studentInfo4);
studentList2.add(studentInfo6);
studentList2.add(studentInfo5);
List<StudentInfo> result = studentList.stream().filter(
(studentInfo) ->studentList2.stream().map(StudentInfo::getAge).collect(Collectors.toList()).contains(studentInfo.getAge())
).collect(Collectors.toList());
System.out.println("111111111111111");
result.forEach(
System.out::println
);
}
}
java1.8 最全stream集合demo
最新推荐文章于 2024-06-08 23:24:37 发布