流转成值
1.代码
package org.java8.collector;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Stream;
import static java.util.stream.Collectors.*;
import static java.util.Comparator.*;
import org.java8.vo.Artist;
public class TranslatToValue {
public static void main(String[] args) {
Stream<Artist> example = Stream.of(
new Artist(
"haha",
Arrays.asList(new Artist("haha", "mei")),
"mei"),
new Artist(
"haha1",
Arrays.asList(),
"mei1"),
new Artist(
"haha2",
Arrays.asList(
new Artist("haha", "mei"),
new Artist("haha", "mei")),
"mei1")
);
Optional<Artist> artist = biggestGroup(example);
System.out.println(artist.get());
}
//输出拥有最多members的
public static Optional<Artist> biggestGroup(Stream<Artist> artists) {
Function<Artist,Long> getCount = artist -> artist.getMembers().count();
return artists.collect(maxBy(comparing(getCount)));
}
}
2.输出
haha2