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

2018-05-16  本文已影响0人  phoebe_gyq

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<Integer> number=asList(1,2,3,4); List<Integer>sameOrder=number.stream().collect(toList()); assertEquals(number,sameOrder);
例5-2 顺序测试不能每次都通过
Set<Integer> numbers=new HashSet<>(asList(4,9,2,1)); List<Integer>sameOrder=numbers.stream().collect(Collectors.toList()); assertEquals(number,sameOrder);
例5-3 顺序
Set<Integer> numbers=new HashSet<>(asList(4,9,2,1)); List<Integer>sameOrder=numbers.stream().sorted().collect(Collectors.toList()); System.out.println(sameOrder);

5.3使用收集器

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

5.3.1转换成其他集合

例5-5 使用toCollection
List<String> list=new ArrayList<String>(); list.add("a"); list.add("b"); list.stream().collect(toCollection(ArrayList::new)).forEach(n-> System.out.println(n)); //方式二: List<String> 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<Artist> 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<Artist> 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<Artist> getMembers() {
        return members;
    }

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

    public Boolean getSolo() {
        return isSolo;
    }

    public void setSolo(Boolean solo) {
        isSolo = solo;
    }
}
 List<Artist> 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<Artist,Long> getCount = artist -> artist.getMembers().count();
        Optional<Artist> maxNumber2=artists.stream().collect(Collectors.maxBy(comparing(getCount)));
        System.out.println(maxNumber2.get());

简单例子

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

5.3.3 数据分块

partitioningBy收集器

   List<Artist> 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<String> 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);

上一篇下一篇

猜你喜欢

热点阅读