????????????????? 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 ??????????????