java Stream流式编程
1. 直接上代码
package com.ggh.study; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.List; import java.util.Map; import java.util.Optional; import java.util.function.BinaryOperator; import java.util.function.Consumer; import java.util.function.Function; import java.util.function.Predicate; import java.util.stream.Collectors; import java.util.stream.Stream; public class StreamStudy { public static void main(String[] args) { Collection<String> collection1 = Arrays.asList("abc","def","def","gh","opq","lmn","igk"); /*初始操作*/ //将集合转换为并行流对象,通过流对象的方法对集合做各种遍历操作更方便 Stream<String> parallelStream = collection1.parallelStream(); //将集合转为串行流对象,通过流对象的方法对集合做各种遍历操作更方便 Stream<String> stream1 = collection1.stream(); /*中间操作*/ //将流对象中符合条件(长度大于2)的元素保留,不符合条件的元素删除 Stream<String> filteredStream = stream1.filter(new Predicate<String>() { @Override public boolean test(String s) { return s.length() > 2 ? true : false; } }); //将流对象中重复的元素去重,利用各元素的hashCode()和equals()判断元素是否相同 Stream<String> distinctStream = filteredStream.distinct(); //将流对象中的前4个元素保留其他元素删除 Stream<String> limitedStream = distinctStream.limit(4); //将流对象中的前2个元素删除其他元素保留 Stream<String> skippedStream = limitedStream.skip(2); //将对象中的元素进行排序,如果对默认的排序(字典顺序)不满意可以传入比较器对象Comparator Stream<String> sortedStream = skippedStream.sorted(); //将流对象中的所有元素都进行一个相同的处理(将各字符串元素的首字符转字符类型保留下来) Stream<Character> mappedStream = sortedStream.map(new Function<String, Character>() { @Override public Character apply(String s) { return s.charAt(0); } }); //将流对象中的所有元素都进行一个相同的处理(将各字符串元素拆分成单个字符后转字符类型保存下来) 区别:处理时可以将每个元素都拆分成多个结果分别保存下来 // Stream<Character> mappedStream = sortedStream.flatMap(new Function<String, Stream<Character>>() { // @Override // public Stream<Character> apply(String s) { // Collection<Character> collection = new ArrayList<>(); // for (Character character : s.toCharArray()) { // collection.add(character); // } // return collection.stream(); // } // }); /*最终操作*/ //将流对象转换成集合List List<Character> collect1 = mappedStream.collect(Collectors.toList()); System.out.println(collect1); //[l, o] /*最终操作*/ Collection<String> collection2 = Arrays.asList("abc","def","def","gh","opq","lmn","igk"); Stream<String> stream2 = collection2.stream(); //判断流对象中是否存在满足条件(字符串元素的首字符是'l')的元素 boolean b2 = stream2.anyMatch(new Predicate<String>() { @Override public boolean test(String s) { if (s.charAt(0) == 'l') { return true; } return false; } }); System.out.println(b2); //true Collection<String> collection3 = Arrays.asList("abc","def","def","gh","opq","lmn","igk"); Stream<String> stream3 = collection3.stream(); //判断流对象中是否全部元素都满足条件(字符串元素的首字符是'l') boolean b3 = stream3.allMatch(new Predicate<String>() { @Override public boolean test(String s) { if (s.charAt(0) == 'l') { return true; } return false; } }); Collection<String> collection4 = Arrays.asList("abc","def","def","gh","opq","lmn","igk"); Stream<String> stream4 = collection4.stream(); //判断流对象中是否全部元素都不满足条件(字符串元素的首字符是'l') boolean b4 = stream4.allMatch(new Predicate<String>() { @Override public boolean test(String s) { if (s.charAt(0) == 'l') { return true; } return false; } }); System.out.println(b4); //false Collection<String> collection5 = Arrays.asList("abc","def","def","gh","opq","lmn","igk"); Stream<String> stream5 = collection5.stream(); //获取流对象中的首元素放入Optional容器中 Optional<String> firstOptional = stream5.findFirst(); //获取容器对象中的元素 if (firstOptional.isPresent()) { String s = firstOptional.get(); System.out.println(s); //abc } Collection<String> collection6 = Arrays.asList("abc","def","def","gh","opq","lmn","igk"); Stream<String> stream6 = collection6.stream(); //获取流对象中的任意一个元素放入Optional容器中 Optional<String> anyOptional = stream6.findAny(); if (anyOptional.isPresent()) { String s = anyOptional.get(); System.out.println(s); //abc } Collection<String> collection7 = Arrays.asList("abc","def","def","gh","opq","lmn","igk"); Stream<String> stream7 = collection7.stream(); List newList = new ArrayList(); //将流对象中的所有元素都进行一个相同的处理(将各元素插入新集合newList中) stream7.forEach(new Consumer<String>() { @Override public void accept(String s) { newList.add(s); } }); Collection<String> collection8 = Arrays.asList("abc","def","def","gh","opq","lmn","igk"); Stream<String> stream8 = collection8.stream(); //流对象转List // List<String> collect2 = stream8.collect(Collectors.toList()); //流对象转Set // Set<String> collect2 = stream8.collect(Collectors.toSet()); //流对象转Map Map<Object, Object> collect2 = stream8.collect(Collectors.toMap( //指定Map中元素的键 new Function<String, Object>() { @Override public Object apply(String s) { return s + "Key"; } }, //指定Map中元素的值 new Function<String, Object>() { @Override public Object apply(String s) { return s + "Value"; } }, //指定当有相同元素时保留哪个元素,o为前者o2为后者,不指定时有相同元素时会报错 new BinaryOperator<Object>() { @Override public Object apply(Object o, Object o2) { return o2; } } )); Collection<String> collection9 = Arrays.asList("abc","def","def","gh","opq","lmn","igk"); Stream<String> stream9 = collection9.stream(); //根据处理规则(提取各元素中前两个字符组成的子串与临时汇总结果拼接)获取流对象中各元素的最终汇总结果(第一个元素所有字符和其他各元素中前两个字符组成的子串拼接形成的新字符串)的容器对象Optional 注意.汇总结果有一个默认初始值(第一个元素) Optional<String> reducedOptional = stream9.reduce(new BinaryOperator<String>() { @Override public String apply(String s, String s2) { return s + s2.substring(0,2); } }); if (reducedOptional.isPresent()) { String s = reducedOptional.get(); System.out.println(s); //abcdedeghoplmig } Collection<String> collection10 = Arrays.asList("abc","def","def","gh","opq","lmn","igk"); Stream<String> stream10 = collection10.stream(); //根据处理规则(提取各元素中前两个字符组成的子串与临时汇总结果拼接)获取流对象中各元素的最终汇总结果(各元素中前两个字符组成的子串拼接形成的新字符串) 与上一个区别:可以直接获取汇总结果不用从容器对象中取,且临时汇总结果可以自己设定 String reduced = stream10.reduce("REDUCE", new BinaryOperator<String>() { @Override public String apply(String s, String s2) { return s + s2.substring(0, 2); } }); System.out.println(reduced); //REDUCEabdedeghoplmig Collection<String> collection11 = Arrays.asList("abc","def","def","gh","opq","lmn","igk"); Stream<String> stream11 = collection11.stream(); //获取流对象中元素的数量 long count = stream11.count(); System.out.println(count); //7 //man() min()...... /*推荐使用方式*/ Collection<String> collection12 = Arrays.asList("abc","def","def","gh","opq","lmn","igk"); //一个初始操作+若干中间操作+一个最终操作 List<String> resultList = collection12.stream().limit(4).skip(2).collect(Collectors.toList()); System.out.println(resultList); //[def, gh] } }