高级集合——流转成值

流转成值

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 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 = biggestGroup(example);
        
        System.out.println(artist.get());
    }

        //输出拥有最多members的
    public static Optional biggestGroup(Stream artists) {
        Function getCount = artist -> artist.getMembers().count();
        return artists.collect(maxBy(comparing(getCount)));
    }
}

2.输出

haha2

你可能感兴趣的:(高级集合——流转成值)