maxBy minBy


import java.util.stream.Stream;

public class Artist {

    private final String name;
    private final String origin;
    private Stream members;

    public Artist(String name, String origin) {
        this.name = name;
        this.origin = origin;
        this.members = null;
    }

    public Artist(String name, String origin,Stream members) {
        this.name = name;
        this.origin = origin;
        this.members = members;
    }

    public String getName() {
        return name;
    }

    public String getOrigin() {
        return origin;
    }

    public Stream getMembers() {
        return members;
    }

    @Override
    public String toString() {
        return name;
    }
}

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Stream;

import static java.util.Comparator.comparing;
import static java.util.stream.Collectors.maxBy;

public class MaxByDemo {
    public static void main(String[] args) {
        List artists = Arrays.asList(
                new Artist("Fossils", "Kolkata", Stream.of(new Artist("Rupam Islam", "Kolkata"), new Artist("Deep", "Kolkata"), new Artist("Allan", "Kolkata"), new Artist("Chandramouli", "Kolkata"), new Artist("Tanmoy", "Kolkata"))),
                new Artist("Linkin Park", "California", Stream.of(new Artist("Chester Bennington", "California"), new Artist("Dave Farrell", "California"), new Artist("Mike Shinoda", "California"), new Artist("Rob Bourdon", "California"), new Artist("Brad Delson", "California"))),
                new Artist("Cactus", "Kolkata", Stream.of(new Artist("Sidhartha Sankar", "Kolkata"), new Artist("Dibyendu Mukherjee", "Kolkata"), new Artist("Ritaprabha", "Kolkata"), new Artist("Sudipto", "Kolkata"), new Artist("Mainak", "Kolkata"))),
                new Artist("Backstreet Boys", "Florida", Stream.of(new Artist("A. J. McLean", "Florida"), new Artist("Howie D.", "Florida"), new Artist("Nick Carter", "Florida"), new Artist("Kevin Richardson", "Florida"), new Artist("Brian Littrell", "Florida"))),
                new Artist("Prohori", "Kolkata", Stream.of(new Artist("Pritam", "Kolkata"))));
//        int totalMember = 0;
//        for (Artist artist : artists) {
//            Stream members = artist.getMembers();
//            totalMember += members.count();
//        }
//        System.out.println("totalMember: " + totalMember);

        int totalMember2 = artists.stream()
                .filter(d -> d.getOrigin().equals("Kolkata"))
                .flatMap(e -> e.getMembers())
                .map(f->1).reduce(0, (a, b) -> a + b);
        System.out.println("totalMember2: " + totalMember2);

//        Long totalMember3 = artists.stream()
//                .filter(d -> d.getOrigin().equals("Kolkata"))
//                .flatMap(e -> e.getMembers()).count();
//        System.out.println("totalMember3: " + totalMember3);
    }

    public Optional biggestGroup(Stream artists) {
        Function getCount=artist -> artist.getMembers().count();
        return artists.collect(maxBy(comparing(getCount)));
    }

}

你可能感兴趣的:(maxBy minBy)