1.JDK Stream流的使用方式
1. 获取流单列集合:使用Collection接口中的默认方法stream()生成流ArrayList<String> list = new ArrayList<>();list.stream().forEach(li-> System.out.println(li));双列集合:不能直接获取stream流 要先通过keySet或者entrySet获取一个Set集合,在获取Stream流HashMap<String, String> hm = new HashMap<>();// 获取map集合的健集合 , 在进行输出打印hm.keySet().stream().forEach(s -> System.out.println(s));// 获取map集合的entry对象 , 在输出打印hm.entrySet().stream().forEach(s -> System.out.println(s));数组: Arrays中的静态方法stream()生成int[] arr = {1, 2, 3, 4, 5, 6};Arrays.stream(arr).forEach(s -> System.out.println(s));2.中间操作过滤filter():从一个数组中筛选出来一些数据。List<Student> collect1 = studentList.stream() // 获取流.filter(s -> s.getSex().equals(1)) //过滤 只保留性别为男的 返回值是boolean类型.collect(Collectors.toList()); //将处理后的结果收集到list集合中截取limit():截取指定参数个数的数据List<Student> limitList = studentList.stream().limit(2) //只取前两条数据.collect(Collectors.toList());跳过skip():跳过指定参数个数的数据List<Student> skipList = studentList.stream().skip(2) //跳过前两条 取后面的数据.collect(Collectors.toList());去重distinct(): 去除流中重复元素 基于hashCode和equals方法List<Student> collect2 = studentList.stream().distinct().collect(Collectors.toList());求和sum()、求最大值max()、求最小值min();排序sort():将流中元素按照类里面的某个属性进行排序/ Comparator比较器排序:创建TreeSet对象的时候传递Comparator的实现类对象,重写compare方法,根据返回值进行排序// 按照自然排序的规则排序list2.stream().sorted().forEach(s->{System.out.println(s);});// 自定义比较器规则排序list.stream().sorted(new Comparator<String>() {@Overridepublic int compare(String o1, String o2) {return o1.length() - o2.length();}}).forEach(s->{System.out.println(s);});// 按照类里面的某个属性进行排序studentList.stream().sorted(Comparator.comparing(Student::getAge).reversed()).collect(Collectors.toList());快速匹配anyMatch(): 快速在集合中找到指定的数据//是否存在学号为0001的人boolean match = studentList.stream().anyMatch(detail -> "0001".equals(detail.getStudentNo()));3.终结操作forEach(); 对流中的每一个元素执行操作//打印流中的所有元素list.stream().forEach(s->{System.out.println(s);});count(); 返回流中元素的数量long count=list.stream().count();4.收集操作:CollectorsCollector 接口中方法的实现决定了如何对流执行收集操作(如收集到 List、Set、Map)。但是 Collectors 实用类提供了很多静态方法,可以方便地创建常见收集器.list.stream().collect(Collectors.toList()); //把流中元素收集到Listlist.stream().collect(Collectors.toSet()); //把流中元素收集到Setlist.stream().collect(Collectors.toCollection(ArrayList::new)); //把流中元素收集到创建的集合list.stream().collect(Collectors.counting()); //计算流中元素的个数list.stream().collect(Collectors.summingInt(User::getAge)); //对流中元素的整数属性求和list.stream().collect(Collectors.averagingInt(User::getAge)); //计算流中元素Integer属性的平均值list.stream().collect(Collectors.summarizingInt(User::getAge)); //收集流中Integer属性的统计值。如:平均值list.stream().collect(Collectors.groupingBy(User::getAge)); //根据某属性值对流分组,属性为K,结果为V最大值maxBy和最小值 minBy
2.发布文章的流程(业务流程,涉及表,具体实现)
