diff --git a/src/test/java/com/wind/java8learn/model/Employee.java b/src/test/java/com/wind/java8learn/model/Employee.java index 4b41aea74a876e7ad98ef456f67191f49ab7ec7e..e7c1b403b9eadfcdb39f54f5a1e458f4acec667f 100644 --- a/src/test/java/com/wind/java8learn/model/Employee.java +++ b/src/test/java/com/wind/java8learn/model/Employee.java @@ -2,8 +2,10 @@ package com.wind.java8learn.model; import lombok.AllArgsConstructor; import lombok.Data; +import lombok.NonNull; +import lombok.RequiredArgsConstructor; -import java.math.BigDecimal; +import java.util.Objects; /** * @Author :wind @@ -11,13 +13,42 @@ import java.math.BigDecimal; * @Date : 2019/12/31 16:46 */ @Data +@RequiredArgsConstructor @AllArgsConstructor public class Employee { - + @NonNull private String name; - + @NonNull private Integer age; - + @NonNull private Double salary; + private Status status; + + + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Employee employee = (Employee) o; + return Objects.equals(getName(), employee.getName()) && + Objects.equals(getAge(), employee.getAge()) && + Objects.equals(getSalary(), employee.getSalary()); + } + + @Override + public int hashCode() { + return Objects.hash(getName(), getAge(), getSalary()); + } + + /** + * 状态枚举 + */ + public enum Status{ + FREE, + BUSY, + VOCATION; + } + } diff --git a/src/test/java/com/wind/java8learn/streamapi/StreamAPIEndTest.java b/src/test/java/com/wind/java8learn/streamapi/StreamAPIEndTest.java new file mode 100644 index 0000000000000000000000000000000000000000..722501fc413099864c0de1bb4a891fb3a5761a46 --- /dev/null +++ b/src/test/java/com/wind/java8learn/streamapi/StreamAPIEndTest.java @@ -0,0 +1,98 @@ +package com.wind.java8learn.streamapi; + +import com.wind.java8learn.model.Employee; +import org.junit.jupiter.api.Test; + +import java.util.Arrays; +import java.util.List; +import java.util.Optional; + +/** + * @Author :wind + * @Description : Steam API 终止操作 + * 查找与匹配 + * allMatch:检查是否匹配所有元素 + * anyMatch:检查是否至少匹配一个元素 + * noneMatch:检查是否没有匹配所有元素 + * findFirst:返回第一个元素 + * findAny:返回当前流的任意元素 + * count:返回流中的元素的总个数 + * max:返回流中元素的最大值 + * min:返回流中元素的最小值 + * @Date : 2020/1/2 14:14 + */ +public class StreamAPIEndTest { + + List employeeList = Arrays.asList( + //new Employee("张三", 29, 19800.00, Employee.Status.FREE), + new Employee("李四", 19, 8800.00, Employee.Status.BUSY), + new Employee("王五", 21, 10800.00,Employee.Status.BUSY), + new Employee("麻子", 25, 13900.00,Employee.Status.FREE), + new Employee("大佬", 24, 10600.00,Employee.Status.BUSY), + new Employee("麻子2", 26, 10900.00,Employee.Status.FREE), + new Employee("麻子3", 27, 10900.00,Employee.Status.FREE) + ); + + @Test + public void test(){ + /** + * 是否所有的都是FREE + */ + boolean b1 = employeeList.stream() + .allMatch(e -> e.getStatus().equals(Employee.Status.FREE)); + System.out.println("是否所有的都是FREE: "+b1); + /** + * 是否有一个是 FREE + */ + boolean b2 = employeeList.stream() + .anyMatch(e -> e.getStatus().equals(Employee.Status.FREE)); + System.out.println("是否有一个是 FREE: "+b2); + /** + * 是否一个都没有FREE + */ + boolean b3 = employeeList.stream() + .noneMatch(e -> e.getStatus().equals(Employee.Status.VOCATION)); + System.out.println("是否一个都不是 VOCATION: "+b3); + + //返回的结果有可能为空,将结果封装到Optional容器中 + Optional op = employeeList.stream() + .sorted((e1, e2) -> Double.compare(e1.getSalary(), e2.getSalary())) + .findFirst(); + System.out.println("工资最低的:"+op.get()); + + //串行(stream 串行流)找 任意一个 + Optional op2 = employeeList.stream() + .filter(e -> e.getStatus().equals(Employee.Status.FREE)) + .findAny(); + System.out.println("状态为Free的(串行) 任意一个:"+op2.get()); + + //并行(parallelStream 并行流)找 任意一个 + Optional op3= employeeList.parallelStream() + .filter(e -> e.getStatus().equals(Employee.Status.FREE)) + .findAny(); + System.out.println("状态为Free的(并行) 任意一个:"+op3.get()); + } + + /** + * count、max、min 操作 + */ + @Test + public void test3(){ + Long count = employeeList.stream() + .count(); + System.out.println("employeeList 对象个数:"+count); + + Optional maxEE = employeeList.stream() + .max((e1, e2) -> Double.compare(e1.getSalary(),e2.getSalary())); + System.out.println("薪资最高的:" + maxEE.get()); + + Optional minEE = employeeList.stream() + .min((e1, e2) -> Double.compare(e1.getSalary(),e2.getSalary())); + System.out.println("薪资最低的:" + minEE.get()); + + + } + + + +} diff --git a/src/test/java/com/wind/java8learn/streamapi/StreamAPIMappingTest.java b/src/test/java/com/wind/java8learn/streamapi/StreamAPIMappingTest.java new file mode 100644 index 0000000000000000000000000000000000000000..8c75406f133db773a22957e0f320b026955026ce --- /dev/null +++ b/src/test/java/com/wind/java8learn/streamapi/StreamAPIMappingTest.java @@ -0,0 +1,80 @@ +package com.wind.java8learn.streamapi; + +import com.wind.java8learn.model.Employee; +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Stream; + +/** + * @Author :wind + * @Description : Stream API 映射操作 + * @Date : 2020/1/2 10:34 + */ +public class StreamAPIMappingTest { + + @Test + public void mapTest(){ + + + } + + /** + * 映射 + * map : 接收一个Lambda ,将元素转换成其他形式 或者提取信息。接收 一个函数作为参数,该函数会被应用到每个元素上, + * 并将其映射成一个新的元素。 + * flatMap:接收一个函数作为参数,将流中的每个流都替换成另一个流,然后把所有流都连接成一个流 + */ + @Test + public void mapTest2(){ + + List stringList = Arrays.asList("aaa", "bbb", "ccc"); + stringList.stream() + .map( x -> x.toUpperCase()) + .forEach(System.out::println); + System.out.println("============分隔线1=============="); + + List employeeList = Arrays.asList( + new Employee("张三", 19, 9800.00), + new Employee("李四", 20, 8800.00), + new Employee("王五", 21, 10800.00) + ); + employeeList.stream() + .map(Employee::getName) //在函数式接口中只取Employee的name属性 + .forEach(System.out::println); //循环打印name + + System.out.println("======第一种方式:map============="); + + Stream> streamStream = stringList.stream() + //每个map返回的结果是一个Stream + .map(StreamAPIMappingTest :: filterCharter) ; + //遍历streamStream,即每个 itemStream 也是一个流 + streamStream.forEach( itemStream -> { + itemStream.forEach( + System.out::println + ); + }); + System.out.println("======第二种方式:flatMap========"); + Stream characterList = stringList.stream() + //直接将流中的流 替换成另一个流,并将所有流连成同一个流 + //即 [{a,a,a},{b,b,b},{c,c,c}] 转成{a,a,a,b,b,b,c,c,c} + .flatMap(StreamAPIMappingTest :: filterCharter); + characterList.forEach(System.out::println); + } + + private static Stream filterCharter(String str){ + List characterList = new ArrayList<>(); + for(Character cc : str.toCharArray()){ + characterList.add(cc); + } + return characterList.stream(); + } + + + + + + +} diff --git a/src/test/java/com/wind/java8learn/streamapi/StreamAPISortTest.java b/src/test/java/com/wind/java8learn/streamapi/StreamAPISortTest.java new file mode 100644 index 0000000000000000000000000000000000000000..afe6959281529cd693ce7c57c662b243342a4d56 --- /dev/null +++ b/src/test/java/com/wind/java8learn/streamapi/StreamAPISortTest.java @@ -0,0 +1,76 @@ +package com.wind.java8learn.streamapi; + +import com.wind.java8learn.model.Employee; +import org.junit.jupiter.api.Test; + +import java.util.*; + +/** + * @Author :wind + * @Description : Stream API 排序 + * @Date : 2020/1/2 13:38 + */ +public class StreamAPISortTest { + + /** + * 排序 + * sorted() :自然排序 + * sorted(Comparator com) : 定制排序(Comparator) + */ + @Test + public void test(){ + //自然排序 + List stringList = Arrays.asList("aaa", "bbb", "ccc", "ddd"); + stringList.stream() + .sorted() + .forEach(System.out::println); + + System.out.println("===========分割线=========="); + + List employeeList = Arrays.asList( + new Employee("张三", 19, 9800.00), + new Employee("李四", 20, 8800.00), + new Employee("王五", 21, 10800.00), + new Employee("麻子", 22, 11800.00) + ); + + employeeList.stream() + .sorted( (e1,e2) ->{ + //按年龄排序(默认升序),年龄相等则按名字排序 + if(e1.getAge().equals(e2.getAge())){ + return e1.getName().compareTo(e2.getName()); + }else{ + return e1.getAge().compareTo(e2.getAge()); + } + }).forEach(System.out::println); + + } + + /** + * list的常规排序方式 + */ + @Test + public void test2(){ + List employeeList = Arrays.asList( + new Employee("张三", 19, 9800.00), + new Employee("李四", 25, 8800.00), + new Employee("王五", 21, 10800.00), + new Employee("麻子", 18, 11800.00) + ); + Collections.sort(employeeList, new Comparator() { + @Override + public int compare(Employee o1, Employee o2) { + if(o1.getAge() > o2.getAge()){ + return 1; + }else if (o1.getAge() < o2.getAge()){ + return -1; + }else{ + return 0; + } + } + }); + System.out.println("排序后的:"+employeeList); + } + + +} diff --git a/src/test/java/com/wind/java8learn/streamapi/StreamApiTest2.java b/src/test/java/com/wind/java8learn/streamapi/StreamApiTest2.java index 3d97083cbd02fc64fddf7f4259e87dd6797cbeac..cacb40070797710ba361fb7ac00cd852627d1286 100644 --- a/src/test/java/com/wind/java8learn/streamapi/StreamApiTest2.java +++ b/src/test/java/com/wind/java8learn/streamapi/StreamApiTest2.java @@ -21,12 +21,13 @@ import java.util.stream.Stream; */ public class StreamApiTest2 { - List employeeList = Arrays.asList( new Employee("张三", 21, 10200.99), new Employee("李四", 30, 16200.99), new Employee("张三", 24, 11800.99), new Employee("张三", 28, 16110.99), + //最后面的两个对象的属性值都一样 + new Employee("张三", 28, 17110.99), new Employee("张三", 28, 17110.99) ); @@ -53,7 +54,6 @@ public class StreamApiTest2 { System.out.println("Stream API 的中间操作 "); return e.getAge() >28; }); - /** * 终止操作 * 【中间操作只有终止操作执行后才会执行,即 惰性求值 】 @@ -89,9 +89,23 @@ public class StreamApiTest2 { @Test public void skipTest(){ employeeList.stream() - .filter(e ->e.getSalary() >11000.00) - .limit(2) //跳过 满足条件的前 2 条数据 + .filter(e ->e.getSalary() >10200.00) + .skip(2) //跳过 满足条件的前 2 条数据 + .forEach(System.out::println); + } + + /** + * distinct使用 :去重,但必须重写equals()和 hashCode()方法 + */ + @Test + public void skipTest3(){ + employeeList.stream() + .filter(e ->e.getSalary() >10200.00) + .skip(2) //跳过 满足条件的前 2 条数据 + .distinct() //重写hashCode() 和 equals()方法后,distinct()才会生效 .forEach(System.out::println); } + + }