????????????????? Stream ??????????????????????????????? Stream ???÷?????????????????????? Stream ?????????????????????
?????????????reduction operation ??????????????????fold ????????????????????????????????????????????????????????????С??????????????????????????????????б???????????????????Stream ??????????????????? reduce() ?? collect() ??????Щ?????д????????ù???????????? sum() ?? max() ?? min() ?? count() ???
???????С??????????????????????????????????????????????????? reduce() ?? collect() ???????????????????
??????????reduce()
????reduce ?????????????????????????????? sum() ?? max() ?? min() ?? count() ????? reduce ???????????????????????????????á? reduce() ?????????????????д?????
????· Optional<T> reduce(BinaryOperator<T> accumulator)
????· T reduce(T identity?? BinaryOperator<T> accumulator)
????· <U> U reduce(U identity?? BiFunction<U??? super T??U> accumulator?? BinaryOperator<U> combiner)
????????????????????????????岻?????????????????????????????? identity ??????????????????????????????????????????? combiner ???? reduce() ?????????????????????????????????????????????????С???????????????????в??????????????“??”??“С”????“???"??????в???????塣
???????? ????鵥?????????????? ??????“??”???????“??”??
????// ??????????![](http://images2015.cnblogs.com/blog/939998/201703/939998-20170314192638495-351834305.png)
????Stream<String> stream = Stream.of("I"?? "love"?? "you"?? "too");
????Optional<String> longest = stream.reduce((s1?? s2) -> s1.length()>=s2.length() ? s1 : s2);
????//Optional<String> longest = stream.max((s1?? s2) -> s1.length()-s2.length());
????System.out.println(longest.get());
???????????????????????? love ?????? Optional ????????????????????????????? null ????鷳???????????? Stream.max(Comparator<? super T> comparator) ???????????Ч?????? reduce() ?????????????ɡ?

???????? ?????鵥????????? ???????“???”???????????????????????? String ????????????? Integer ??
????// ??????????
????Stream<String> stream = Stream.of("I"?? "love"?? "you"?? "too");
????Integer lengthSum = stream.reduce(0????// ??????// (1)
????(sum?? str) -> sum+str.length()?? // ????? // (2)
????(a?? b) -> a+b);??// ?????????????????????????? // (3)
????// int lengthSum = stream.mapToInt(str -> str.length()).sum();
????System.out.println(lengthSum);
??????????????(2)????i. ?????????????ii. ?????????????????????????????????? reduce() ????????????????????????????????????????????? map() ?? sum() ???????????????????????
????reduce() ?ó??????????????????????? Stream ?????????????? Map ?????????????????????? collect() ????????
????>>> ????collect() <<<
?????????????????????????????? Stream ?????????????а???????? collect() ???????? collect() ?? Stream ??????????????????????????????????Java?????????????????????С?????
????// ??Stream???????????Map
????Stream<String> stream = Stream.of("I"?? "love"?? "you"?? "too");
????List<String> list = stream.collect(Collectors.toList()); // (1)
????// Set<String> set = stream.collect(Collectors.toSet()); // (2)
????// Map<String?? Integer> map = stream.collect(Collectors.toMap(Function.identity()?? String::length)); // (3)
???????????????о?????ν? Stream ????? List ?? Set ?? Map ?????????????????????????????????м????????
????1??Function.identity() ????????
????2??String::length ?????????
????3??Collectors ???????????
??????????????????????
????Function ???????????? Function.identity() ?????????????????????????
????1??Java 8?????????м?????巽????????е???巽????????? default ?????? static ?????? identity() ?? Function ????????????????
????2??Function.identity() ?????????????????????Lambda????????????????? t -> t ?????Lambda??????
??????????????????????????????????????????п????о??巽??????????????????? t -> t ?? identity() ?????????????????????е? default ???????????????????Java 7?????????????????м????μ?????????????????????????????????????y???????????????????? Collection ????м?????? stream() ?????????????? default ????????????????????????????????????????????????????????????? default ????????β?????? static ??????????????????????
????????????
???????? String::length ???????????????????? method references ??????????????????Щ??????Lambda?????????Lambda????????????????????????е??????????????÷????????????Lambda?????????????????????????
??????????????? ??????????????????????????????????
???????t??????  ??????????????????????????    Integer::sum
???????????????????  ????????????????????    list::add
????????????????? ????????????????????????String::length
???????ù?????  ??????????????????????????   HashMap::new

???????????????????????÷??????á?
?????????
??????????深??????????????????????Java????????????飬???????????????????????????

??????????? Collector ????? Stream.collect() ????????????????????????????????? Stream ?????????????????? Map ?????????Щ???????????????????????????
??????????????????? ArrayList ???? HashSet ????????? TreeMap ??
?????????????????????У??? List.add() ???? Map.put() ??
??????????е???й????????????? collect() 3. ???????????κ?????????
???????????????? collect() ????????? <R> R collect(Supplier<R> supplier?? BiConsumer<R??? super T> accumulator?? BiConsumer<R??R> combiner) ?????????????ζ??????????????????????ε??? collect() ???????????????????鷳??????? Collector ????????????????????????? collect() ?????????? <R??A> R collect(Collector<? super T??A??R> collector) ?? Collectors ???????????????????????????? Collector ????????????????? Stream ????? List ?????????????????????
????//????Stream?????List
????Stream<String> stream = Stream.of("I"?? "love"?? "you"?? "too");
????List<String> list = stream.collect(ArrayList::new?? ArrayList::add?? ArrayList::addAll);// ?????
????//List<String> list = stream.collect(Collectors.toList());// ???2
????System.out.println(list);
?????????????????????????? collect() ??????????????????? collect(Collector<? super T??A??R> collector) ??????????????е? Collector ????????????? Collectors ???????á?????????? ???????????????? collect() ????? ??
???????collect()????Collection
??????????????? collect() ?????? Stream ??????????????????????????????? Stream ????? List ?? Set ?????????????????? Collectors ?????????????????????????????????′?????????
????// ??Stream?????List??Set
????Stream<String> stream = Stream.of("I"?? "love"?? "you"?? "too");
????List<String> list = stream.collect(Collectors.toList()); // (1)
????Set<String> set = stream.collect(Collectors.toSet()); // (2)
??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? Collectors.toCollection(Supplier<C> collectionFactory) ??????ɡ?
????// ???toCollection()????????????????
????ArrayList<String> arrayList = stream.collect(Collectors.toCollection(ArrayList::new));// (3)
????HashSet<String> hashSet = stream.collect(Collectors.toCollection(HashSet::new));// (4)
????????????(3)????????????? ArrayList ????(4)???????????? HashSet ??????????????