/*
* Stream操作的三个步骤:
* 1.创建Stream
* 2.操作Stream
* 3.终止操作
* */
public class TestStreamAPI {
// 创建Stream的四种方式
@Test
public void test01(){
// 1.通过Collection系列集合提供的stream() 或者 parallelStream()
List<String> list = Arrays.asList("aa", "bb", "cc");
Stream<String> 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);
}
}
//中间操作
/*
* 筛选与切片 (test01)
* filter——接收lambda,从流中排除这些元素
* limit——截断流,使其元素不超过给定值
* skip(n)——跳过元素,返回一个扔掉了前n个元素的流,若流中元素不足n个,则返回一个空流,与limit互补
* distinct——筛选,通过流所生成元素的hashCode()和equals()去除重复元素(所以如果想要去重需要重写hashCOde()和equals())
* */
/*
* 映射 (test02-test03)
* map——接收Lambda,将元素转换成其他形式或者提取信息,接收一个函数作为参数,该函数会被应用到每个元素上,并将其映射成一个新的元素
* flatMap——接收一个函数作为参数,将流中的每个值都换成另外一个流,然后把所有的流连接成一个流
* */
/*
* 排序 (test04)
* sorted()——自然排序(Comparable)
* sorted(Comparator com)——定制排序(Comparator)
* */
public class TestStreamAPI2 {
List<Employee> 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<String> 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<Stream<Character>> 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<Character> flatStr(String str){
List<Character> 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);
}
}
/*
* 终止操作
* */
/*
* 查找与匹配 (test01-test02)
* allMatch——检查是否匹配所有元素
* anyMatch——检查是否至少匹配一个元素
* noneMatch——检查是否没有匹配所有元素
* findFirst——返回第一个元素
* findAny——返回当前流中的任意元素
* count——返回流中元素的总个数
* max——返回流中最大值
* min——返回流中最小值
* */
/*
* 规约 (test03)
* reduce——将流中元素反复结合起来,得到一个值
* */
/*
* 收集 (test04-test05)
* collect——将流转换为其他形式,接收一个Collector接口的实现,用于stream中元素做汇总的方法
* */
/*
* 分组 (test6-test7)
* */
/*
* 分区 (test8)
* */
public class TestStreamAPI3 {
List<Employee> 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<Employee> first = eList.stream()
.sorted((a, c) -> -Double.compare(a.getMoney(), c.getMoney()))
.findFirst();
System.out.println(first.get());
System.out.println("========================================");
Optional<Employee> 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<Employee> max = eList.stream()
.max((a, b) -> Double.compare(a.getMoney(), b.getMoney()));
System.out.println(max);
Optional<Employee> min = eList.stream()
.min((a, b) -> Double.compare(a.getMoney(), b.getMoney()));
System.out.println(min);
}
@Test
public void test03(){
List<Integer> 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<Employee> max = eList.stream()
.collect(Collectors.maxBy((x, y) -> Double.compare(x.getMoney(), y.getMoney())));
System.out.println("工资最大值为:"+max.get());
System.out.println("==============================");
// 最小值
Optional<Double> min = eList.stream()
.map(Employee::getMoney)
.collect(Collectors.minBy(Double::compare));
System.out.println("最小值为:"+min);
}
// 普通分组
@Test
public void test06(){
Map<Employee.Status, List<Employee>> listMap = eList.stream()
.collect(Collectors.groupingBy(Employee::getStatus));
listMap.forEach((a,b) -> {
System.out.println("key: "+a+" val: "+b);
});
}
// 多级分组
@Test
public void test07(){
Map<Employee.Status, Map<String, List<Employee>>> 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<Boolean, List<Employee>> map = eList.stream()
.collect(Collectors.partitioningBy(x -> x.getMoney() > 8000));
System.out.println(map);
}
}