V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
• 请不要在回答技术问题时复制粘贴 AI 生成的内容
gino86
V2EX  ›  程序员

怎样理解 Stream 的 collect 方法

  •  
  •   gino86 · May 27, 2017 · 3508 views
    This topic created in 3262 days ago, the information mentioned may be changed or developed.

    collect 的第三个参数是干什么用的,即使是一个空代码块运行结果还是不变

    public class Stream2Coll {
    
        public static void main(String[] args) {
            List<String> stupidList = new ArrayList<>();
    
            stupidList.add(" hello ");
            stupidList.add("world ");
    
            LinkedList<Object> collect = stupidList.stream().map(String::trim).collect(
                    LinkedList::new,
                    LinkedList::add,
                    (listOne, listTwo) ->{
                        //有没有这句结果一样
                        listOne.addAll(listTwo);
                    });
    
            collect.forEach(System.out::println);
        }
    }
    
    2 replies    2017-05-27 00:29:15 +08:00
    orcusfox
        1
    orcusfox  
       May 27, 2017
    看文档啊 最后一个是 combiner
    gino86
        2
    gino86  
    OP
       May 27, 2017
    @napsterwu 谢谢,根据你的关键字 combiner 搜索到结果。貌似第三个参数只有在使用 parallelStream 的前提下才有用。因为我这里使用的是串行流,所有结果都在一个线程中处理,所以并不需要进行合并操作。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1096 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 17:36 · PVG 01:36 · LAX 10:36 · JFK 13:36
    ♥ Do have faith in what you're doing.