第五章 高级集合类和收集器

5.1方法引用

artist->artist.getName();
Artist::getName

用lambda表达式创建一个artist对象:
(name,nationality)->new Artist(name,nationality)
Artist::new

创建字符串型数组
String[]::new

5.2元素顺序

List集合类型中的元素是按顺序排列,HashSet是无序的,增加了流操作后,顺序问题更加复杂
例5-1 顺序测试永远通过
List number=asList(1,2,3,4); ListsameOrder=number.stream().collect(toList()); assertEquals(number,sameOrder);
例5-2 顺序测试不能每次都通过
Set numbers=new HashSet<>(asList(4,9,2,1)); ListsameOrder=numbers.stream().collect(Collectors.toList()); assertEquals(number,sameOrder);
例5-3 顺序
Set numbers=new HashSet<>(asList(4,9,2,1)); ListsameOrder=numbers.stream().sorted().collect(Collectors.toList()); System.out.println(sameOrder);

5.3使用收集器

java.util.stream.Collectors类中静态导入

5.3.1转换成其他集合

例5-5 使用toCollection
List list=new ArrayList(); list.add("a"); list.add("b"); list.stream().collect(toCollection(ArrayList::new)).forEach(n-> System.out.println(n)); //方式二: List listNew = Arrays.asList("a","m"); listNew.stream().collect(toCollection(ArrayList::new)).forEach(n-> System.out.println(n));

5.3.2 转换成值

例5-6 找出成员最多的乐队

import java.util.stream.Stream;

public class Artist {

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

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

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

    public String getName() {
        return name;
    }

    public String getOrigin() {
        return origin;
    }

    public Stream getMembers() {
        return members;
    }

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

    public Boolean getSolo() {
        return isSolo;
    }

    public void setSolo(Boolean solo) {
        isSolo = solo;
    }
}
 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")),false),
                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")),false),
                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")),false),
                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")),false),
                new Artist("Prohori", "Kolkata", Stream.of(new Artist("Pritam", "Kolkata")),true));

以下例子未解决

Function getCount = artist -> artist.getMembers().count();
        Optional maxNumber2=artists.stream().collect(Collectors.maxBy(comparing(getCount)));
        System.out.println(maxNumber2.get());

简单例子

Stream stream=Stream.of(1,2,3,4,5);
        Optional maxNumber=stream.collect(Collectors.maxBy(Comparator.naturalOrder()));
        System.out.println(maxNumber.get());

5.3.3 数据分块

partitioningBy收集器

   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")), true),
                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")), false));
        System.out.println(artists.stream().collect(partitioningBy(artist -> artist.isSolo)));
        System.out.println(artists.stream().collect(partitioningBy(Artist::getSolo)));

使用predicate对象判断一个元素应该属于哪个部分

5.3.4 数据分组

groupingBy收集器

5.3.4 字符串

List G7 = Arrays.asList("USA", "Japan", "France", "Germany", "Italy", "U.K.","Canada"); String G7Countries=G7.stream().map(x->x.toUpperCase()).collect(Collectors.joining(",")); System.out.println(G7Countries);

你可能感兴趣的:(第五章 高级集合类和收集器)