Java Stream ??????
???????????? ???????[ 2016/3/11 13:20:45 ] ??????????????????? ???????
????Stream?? Java 8????????????????浼�????
????Stream???????????????械??????????????????????????????????????
????Stream?????????????????????????????????????????????????????????????效????(?????)????Stream????????????????????????????????????????????????????????????????????校????BaseStream.iterator()??BaseStream.spliterator()????????????????????
????Java Stream??????????泻???????????????????????渭????????????????????????????屑??????????胁??????Java????????????????????????????????????????????????Java?????????
???????????????Java Stream????????????????????????????屑??????????????????????????Java Stream?????????
????????????????写???些????Java 8 Lambda??Stream?????锟�??????????????????伞?????????????写??Scala??????????(Scala Collections Cookbook)???椋�??????眉?写J(r猫n)ava Stream?????锟�????????????????? Java Stream????????伞?
????????
????????????????? Javadoc ????????????些??????????????????
????Stream???????????????????????????IntStream?? LongStream ?? DoubleStream??
?????????????????????????????????????????斜??
???????娲�???? ???????????????????????????娲�????????????????????????????????????????
???????????? ???????????????????????????filter??????????械??????????
?????????? ?????????????filter??map???屑???????????械????械???????????????????小?
?????????? ??????????????????????些????????????????????????????limit(n)??findFirst()????些???????????”??路”(Short-circuiting)???????????????????????
?????????? ?????????????????危?????Iterator????????谢??路??????????????路???????????????????????????????渭?????
??????????????????????????????????????????????????????????N???屑?????????????????????????
???????? Parallelism
???????械??????????????????谢????????小?????????????????????????Java???写???????????????Collection.stream()????????????????Collection.parallelStream()????????????????IntStream.range(int?? int)??????????????????parallel()???????????????????????????sequential()????????????????????
?????????????Javadoc?????????????????械????????????(????findAny??forEach)???????泻??????械??????????????
?????????? Non-interference
???????????????????????写???????????????械??????concurrent??????????????????????????java.util.ConcurrentModificationException????
????List<String> l = new ArrayList(Arrays.asList("one"?? "two"));Stream<String> sl = l.stream();sl.forEach(s -> l.add("three"));
???????????屑?????????????????????????????????????????????锌?????????????(????????????????????)???????????????????????
????List<String> l = new ArrayList(Arrays.asList("one"?? "two"));Stream<String> sl = l.stream();l.add("three");sl.forEach(System.out::println);
????????concurrent??????????????????????????????????????????
????List<String> l = new CopyOnWriteArrayList<>(Arrays.asList("one"?? "two"));Stream<String> sl = l.stream();sl.forEach(s -> l.add("three"));
??????????????????????????????卸?????????????????????????????????????????????????????????胁???????
???????? Stateless behaviors
???????????????????????????????????????Lambda?????????????????????????????????????????(behavioral parameters)??
?????????些???????????????????????????????????????????????????:
????List<String> l = new ArrayList(Arrays.asList("one"?? "two"?? ……));class State { boolean s;}final State state = new State();Stream<String> sl = l.stream().map(e -> { if (state.s) return "OK"; else { state.s = true; return e; } });sl.forEach(System.out::println);
???????????????????????蔚???薪???????????????????????lambda?????????????
?????????? Side-effects
?????懈?????????????????????????
??????????????????????????械????????????????????????????????
???????????Java???????些?????????????????????????????????????????????????????????????????小?
????????懈?????????????????????????????????????????println()?????????????????泻???
????ArrayList<String> results = new ArrayList<>();stream.filter(s -> pattern.matcher(s).matches()) .forEach(s -> results.add(s)); // ?????????
??????????????????????????
????List<String>results = stream.filter(s -> pattern.matcher(s).matches()) .collect(Collectors.toList()); // No side-effects!
???????? Ordering
?????些??????????????????????????????encounter order??????????????????????????????????encounter order?????????????????List????????????(iteration order)??????HashSet???????????encounter order??
??????????????encounter order???????????????????屑???????????????List??Array????????????????(ordered)????????HashSet??????????????????
????sorted()??????????????????????unordered????????????????????????????????????????????????????map???????????貌??????????????婊�???械??????????????????????????????????????????????filter????????????????????些????????????????????????????
????????????????????????????????????????????????(determinism)??????????????械???????????????????
????????????????????????????????????????????distinct??groupingBy??些????????
????????? Associativity
??????????????????op??????????味???????????????????
????(a op b) op c == a op (b op c)
???????????????????????????????????????????屑???
????a op b op c op d == (a op b) op (c op d)
????????min??max?????????????????????????
????????Stream
?????????????????????????
????1??????????stream()????????parallelStream()??????Arrays.asList(1??2??3).stream()??
????2?????Arrays.stream(Object[])?????? ????Arrays.stream(new int[]{1??2??3})??
????3?????????????????????Stream.of(Object[])??IntStream.range(int?? int)????Stream.iterate(Object?? UnaryOperator)????Stream.iterate(0?? n -> n * 2)??????generate(Supplier<T> s)??Stream.generate(Math::random)??
????4??BufferedReader.lines()??????谢???械?????
????5??Files??????路???????????list??find??walk???
????6?????????Random.ints()??
????7???????些????????????????????BitSet.stream()??Pattern.splitAsStream(java.lang.CharSequence)?? ??JarFile.stream()??
????8???????????StreamSupport?????????Spliterator??????????????
?????屑???? intermediate operations
?????屑????????????渭???????????????????械?(lazy)????????????????????????????????????????????????????????小????Scala?????????????????Scala?????????????????????渭??屑浼�?????????Java??????????????屑????????伞?
?????????????????些?屑??????
????distinct
????distinct???????????邪????????????????Object.equals(Object)????????????????????
????List<String> l = Stream.of("a"??"b"??"c"??"b") .distinct() .collect(Collectors.toList());System.out.println(l); //[a?? b?? c]
????filter
????filter?????????????????????(predicate)???????
?????????????????械?????????
????List<Integer> l = IntStream.range(1??10) .filter( i -> i % 2 == 0) .boxed() .collect(Collectors.toList());System.out.println(l); //[2?? 4?? 6?? 8]
????map
????map?????????械?????????????????渭???????????????????????????
?????????????薪???????????????????(ASCII?)??
????List<Integer> l = Stream.of('a'??'b'??'c') .map( c -> c.hashCode()) .collect(Collectors.toList());System.out.println(l); //[97?? 98?? 99]
????flatmap
????flatmap?????????map+flattern?????????????????????????????????渭????小???????????????锟�?
????<R> Stream<R> flatMap(Function<? super T??? extends Stream<? extends R>> mapper)
???????????mapper?????????????????????????????flatMap?????????????????????mapper????????械?????
????????????????薪?????????????????蟹??????????????????????flatmap????????小写???????????????????????????????
????String poetry = "Where?? before me?? are the ages that have gone?/n" + "And where?? behind me?? are the coming generations?/n" + "I think of heaven and earth?? without limit?? without end??/n" + "And I am all alone and my tears fall down.";Stream<String> lines = Arrays.stream(poetry.split("/n"));Stream<String> words = lines.flatMap(line -> Arrays.stream(line.split(" ")));List<String> l = words.map( w -> { if (w.endsWith("??") || w.endsWith(".") || w.endsWith("?")) return w.substring(0??w.length() -1).trim().toLowerCase(); else return w.trim().toLowerCase();}).distinct().sorted().collect(Collectors.toList());System.out.println(l); //[ages?? all?? alone?? am?? and?? are?? before?? behind?? coming?? down?? earth?? end?? fall?? generations?? gone?? have?? heaven?? i?? limit?? me?? my?? of?? tears?? that?? the?? think?? where?? without]
????flatMapToDouble??flatMapToInt??flatMapToLong?????????????????????
????limit
????limit???????????????????????????????????????????效??????????????韪�???n???????????????????????????????????????????????????????????????????????????????????????????????
????List<Integer> l = IntStream.range(1??100).limit(5) .boxed() .collect(Collectors.toList());System.out.println(l);//[1?? 2?? 3?? 4?? 5]
????peek
????peek????????????????Consumer???????械??????????????????????????????械?????
????String[] arr = new String[]{"a"??"b"??"c"??"d"};Arrays.stream(arr) .peek(System.out::println) //a??b??c??d .count();
????sorted
????sorted()?????械???????????????????????????????????Comparable???????????????????java.lang.ClassCastException????sorted(Comparator<? super T> comparator)????????????????
??????

???路???
??????????????????
2023/3/23 14:23:39???写?貌??????????
2023/3/22 16:17:39????????????????????些??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???路???????路
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11