高级集合——收集器

集合收集器

1、有时候你想把流转成某一种集合进行使用

package org.java8.collector;

import static java.util.stream.Collectors.toCollection;
import static java.util.stream.Collectors.toList;

import java.util.TreeSet;
import java.util.stream.Stream;

public class TranslatCollector {

    public static void main(String[] args) {
        /**collect: 将流数据收集起来,并存入某一种数据结构中**/
        Stream.of("1","2","2")
            .collect(toList())
            .forEach(System.out::println);
        
        System.out.println("==黄金分割线==");
        
        Stream.of("2","1","1")
            .collect(toCollection(TreeSet::new))
            //等同于
            //.collect(toCollection(()->{return new TreeSet();}))
            .forEach(System.out::println);
    }
}

2、打印信息

1
2
2
==黄金分割线==
1
2

你可能感兴趣的:(高级集合——收集器)