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