public class TestStreamAPI {
// 创建Stream的四种方式
@Test
public void test01(){
// 1.通过Collection系列集合提供的stream() 或者 parallelStream()
List list = Arrays.asList("aa", "bb", "cc");
Stream stream1 = list.stream();
// 2.通过Arrays中的静态方法stream() 获取数组流
Arrays.stream(new String[5]);
// 3.通过Stream中的静态方法of()
Stream.of("aa","bb","cc");
// 4.创建无限流
//(1)迭代无限流
Stream.iterate(0, x -> x+2)
.limit(10)
.forEach(System.out::println);
//(2)生成无限流
Stream.generate( () -> Math.random() * 100)
.limit(5)
.forEach(System.out::println);
}
}
//中间操作
public class TestStreamAPI2 {
List eList = Arrays.asList(
new Employee("张三",11,9999.99),
new Employee("李四",22,19999.88),
new Employee("王五",33,3599.88),
new Employee("马六",44,88999.88),
new Employee("田七",55,19999.88),
new Employee("田七",55,19999.88),
new Employee("田七",55,19999.88)
);
List strList = Arrays.asList("aaa","bbb","ccc","ddd","eee");
@Test
public void test01(){
eList.stream().filter( x -> x.getAge() > 33)
.forEach(System.out::println);
}
@Test
public void test02(){
Stream> streamStream = strList.stream()
.map(TestStreamAPI2::flatStr);
streamStream.forEach(x -> {
x.forEach(System.out::println);
});
}
@Test
public void test03(){
strList.stream()
.flatMap(TestStreamAPI2::flatStr)
.forEach(System.out::println);
}
public static Stream flatStr(String str){
List cList = new ArrayList<>();
for (Character c:
str.toCharArray()) {
cList.add(c);
}
return cList.stream();
}
@Test
public void test04(){
eList.stream()
.sorted((e1,e2) -> {
if (e1.getAge() == e2.getAge()){
return e1.getName().compareTo(e2.getName());
}else
return Integer.toString(e1.getAge()).compareTo(Integer.toString(e2.getAge()));
})
.forEach(System.out::println);
}
}
public class TestStreamAPI3 {
List eList = Arrays.asList(
new Employee("张三",11,9999.99,Employee.Status.BUSY),
new Employee("李四",22,19999.88,Employee.Status.FREE),
new Employee("王五",33,3599.88,Employee.Status.FREE),
new Employee("马六",44,88999.88,Employee.Status.VOCATION),
new Employee("田七",55,19999.88,Employee.Status.BUSY)
);
@Test
public void test01(){
boolean b = eList.stream()
.allMatch(x -> x.getStatus().equals(Employee.Status.BUSY));
System.out.println(b);
System.out.println("========================================");
boolean b1 = eList.stream()
.anyMatch(x -> x.getStatus().equals(Employee.Status.BUSY));
System.out.println(b1);
System.out.println("========================================");
boolean b2 = eList.stream()
.noneMatch(x -> x.getStatus().equals(Employee.Status.BUSY));
System.out.println(b2);
System.out.println("========================================");
Optional first = eList.stream()
.sorted((a, c) -> -Double.compare(a.getMoney(), c.getMoney()))
.findFirst();
System.out.println(first.get());
System.out.println("========================================");
Optional any = eList.stream()
.filter(x -> x.getStatus().equals(Employee.Status.FREE))
.findAny();
System.out.println(any.get());
System.out.println("========================================");
}
@Test
public void test02(){
long count = eList.stream()
.count();
System.out.println(count);
Optional max = eList.stream()
.max((a, b) -> Double.compare(a.getMoney(), b.getMoney()));
System.out.println(max);
Optional min = eList.stream()
.min((a, b) -> Double.compare(a.getMoney(), b.getMoney()));
System.out.println(min);
}
@Test
public void test03(){
List integerList = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
Integer reduce = integerList.stream()
.reduce(0, (x, y) -> x + y);
System.out.println(reduce);
}
@Test
public void test04(){
eList.stream()
.map(Employee::getName)
.collect(Collectors.toList())
.forEach(System.out::println);
System.out.println("==========================");
eList.stream()
.map(Employee::getName)
.collect(Collectors.toCollection(HashSet::new))
.forEach(System.out::println);
}
@Test
public void test05(){
// 总数
Long sum = eList.stream()
.collect(Collectors.counting());
System.out.println("总的数量为:"+sum);
System.out.println("==============================");
// 平均值
Double avg = eList.stream()
.collect(Collectors.averagingDouble(Employee::getMoney));
System.out.println("工资平均值为:"+avg);
System.out.println("==============================");
// 统计
DoubleSummaryStatistics sumMoney = eList.stream()
.collect(Collectors.summarizingDouble(Employee::getMoney));
System.out.println("工资总和为:"+sumMoney);
System.out.println("==============================");
// 最大值
Optional max = eList.stream()
.collect(Collectors.maxBy((x, y) -> Double.compare(x.getMoney(), y.getMoney())));
System.out.println("工资最大值为:"+max.get());
System.out.println("==============================");
// 最小值
Optional min = eList.stream()
.map(Employee::getMoney)
.collect(Collectors.minBy(Double::compare));
System.out.println("最小值为:"+min);
}
// 普通分组
@Test
public void test06(){
Map> listMap = eList.stream()
.collect(Collectors.groupingBy(Employee::getStatus));
listMap.forEach((a,b) -> {
System.out.println("key: "+a+" val: "+b);
});
}
// 多级分组
@Test
public void test07(){
Map>> map = eList.stream()
.collect(Collectors.groupingBy(Employee::getStatus,
Collectors.groupingBy(x -> {
if (x.getAge() <= 22) {
return "青年";
} else if (x.getAge() <= 44) {
return "壮年";
} else {
return "老年";
}
})));
map.forEach((x,y) -> {
System.out.println("第一层的key: "+x+" 第一层的val: "+y);
});
}
//分区
@Test
public void test08(){
Map> map = eList.stream()
.collect(Collectors.partitioningBy(x -> x.getMoney() > 8000));
System.out.println(map);
}
}