package com.test;
import com.test.dao.Person;
import com.test.dao.Person1;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class StreamTest {
static List<Person1> personList = new ArrayList<Person1>();
private static void initPerson1(){
personList.add(new Person1("zhangsan",25, 3000, "male", "tieling"));
personList.add(new Person1("lisi",27, 5000, "male", "tieling"));
personList.add(new Person1("wangwu",29, 7000, "female", "tieling"));
personList.add(new Person1("sunliu",26, 3000, "female", "dalian"));
personList.add(new Person1("yinqi",27, 5000, "male", "dalian"));
personList.add(new Person1("guba",21, 7000, "female", "dalian"));
}
public static void main(String[] args) throws IOException {
test01();
test02();
test03();
test04();
test05();
}
/**
* 8、分组(partitioningBy/groupingBy)
* 分区:将stream按条件分为两个 Map,比如员工按薪资是否高于8000分为两部分。
*
* 分组:将集合分为多个Map,比如员工按性别分组。有单级分组和多级分组。5
*/
private static void test01(){
// 将员工按薪资是否高于8000分组
Map<Boolean, List<Person1>> part = personList.stream().collect(Collectors.partitioningBy(x -> x.getSalary() > 8000));
// 将员工按性别分组
Map<String, List<Person1>> group = personList.stream().collect(Collectors.groupingBy(Person1::getSex));
// 将员工先按性别分组,再按地区分组
Map<String, Map<String, List<Person1>>> group2 = personList.stream().collect(Collectors.groupingBy(Person1::getSex, Collectors.groupingBy(Person1::getArea)));
System.out.println("员工按薪资是否大于8000分组情况:" + part);
System.out.println("员工按性别分组情况:" + group);
System.out.println("员工按性别、地区:" + group2);
}
/**
* 9、连接joining
* joining可以将stream中的元素用特定的连接符(没有的话,则直接连接)连接成一个字符串。
*/
private static void test02(){
initPerson1();
String collect = personList.stream().map(x -> x.getName()).collect(Collectors.joining(","));
System.out.println(collect);
}
/**
* 10、排序sorted
* 将员工按工资由高到低(工资一样则按年龄由大到小)排序
*/
private static void test03(){
//按工资升序排序
initPerson1();
List<Person1> collect = personList.stream().sorted(Comparator.comparing(Person1::getSalary)).collect(Collectors.toList());
System.out.println("按工资升序排序:"+collect);
//按工资倒序排序
List<String> collect1 = personList.stream().sorted(Comparator.comparing(Person1::getSalary)).map(Person1::getName).collect(Collectors.toList());
System.out.println("按工资倒序排序:"+collect1);
// 先按工资再按年龄升序排序
List<Person1> collect2 = personList.stream().sorted(Comparator.comparing(Person1::getSalary).thenComparing(Person1::getAge)).collect(Collectors.toList());
System.out.println("先按工资再按年龄升序排序:"+collect2);
// 先按工资再按年龄自定义排序(降序)
List<String> collect3 = personList.stream().sorted((s1, s2) -> {
if (s1.getSalary() == s2.getSalary()) {
return s2.getAge() - s1.getAge();
} else {
return s2.getSalary() - s1.getSalary();
}
}).map(Person1::getName).collect(Collectors.toList());
System.out.println("先按工资再按年龄自定义排序(降序):"+collect3);
}
/**
* 11、提取/组合
* 流也可以进行合并、去重、限制、跳过等操作。
*/
private static void test04(){
String[] arr1 = { "a", "b", "c", "d" };
String[] arr2 = { "d", "e", "f", "g" };
Stream<String> stream1 = Stream.of(arr1);
Stream<String> stream2 = Stream.of(arr2);
// concat:合并两个流 distinct:去重
List<String> newList = Stream.concat(stream1, stream2).distinct().collect(Collectors.toList());
// limit:限制从流中获得前n个数据
List<Integer> collect = Stream.iterate(1, x -> x + 2).limit(10).collect(Collectors.toList());
// skip:跳过前n个数据
List<Integer> collect2 = Stream.iterate(1, x -> x + 2).skip(1).limit(5).collect(Collectors.toList());
System.out.println("流合并:" + newList);
System.out.println("limit:" + collect);
System.out.println("skip:" + collect2);
}
/**
* 12 读取文件的流操作
*/
private static void test05() throws IOException {
String fileName= "C:\\Users\\Administrator\\Desktop\\sss.txt";
Path path = new File(fileName).toPath();
Stream<String> lines = Files.lines(path, StandardCharsets.UTF_8);
lines.onClose(() -> System.out.println("Done!")).forEach(System.out::println);
}
/**
* 13、计算两个list中的差集
*/
private static void test06(){
//计算两个list中的差集
initPerson1();
// List<String> reduce1 = personList.stream().filter(item -> !wList.contains(item)).collect(Collectors.toList());
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了