zoukankan      html  css  js  c++  java
  • An Interesting Fact Of Java Stream

    import java.util.ArrayList;
    import java.util.List;
    import java.util.Map;
    import java.util.concurrent.ConcurrentHashMap;
    import java.util.function.Function;
    import java.util.function.Predicate;
    import java.util.stream.Collectors;
    
    public class MainTest {
        public static void main(String[] args) {
    
            List<TestObject> testList = new ArrayList<>();
            testList.add(new TestObject("1"));
            testList.add(new TestObject("2"));
            testList.add(new TestObject("3"));
            testList.add(new TestObject("3"));
            testList.add(new TestObject("4"));
    
            List<String> list = testList.stream()
                    .filter(distinctFilter(TestObject::getVal))
                    .map(TestObject::getVal)
                    .collect(Collectors.toList());
    
            System.out.println(list);
    
        }
    
        private static <T> Predicate<T> distinctFilter(Function<? super T, Object> function) {
            Map<Object,Boolean> valuesHaveBeenSeen = new ConcurrentHashMap<>();
            System.out.println("called once");
            return t -> {
                System.out.println(valuesHaveBeenSeen);
                return valuesHaveBeenSeen.putIfAbsent(function.apply(t), Boolean.TRUE) == null;
            };
        }
    }
    
    class TestObject {
        String val;
    
        public TestObject() {
        }
    
        public TestObject(String val) {
            this.val = val;
        }
    
        String getVal() {
            return this.val;
        }
    }

    Output:

    called once
    {}
    {1=true}
    {1=true, 2=true}
    {1=true, 2=true, 3=true}
    {1=true, 2=true, 3=true}
    [1, 2, 3, 4]

    It seems the local map valuesHaveBeenSeen is just created once and used throughout the iteration.

  • 相关阅读:
    网络协议 7
    网络协议 6
    PHP 扩展管理
    网络协议 5
    什么是DevOps?
    C# Web API Modify Post Data Size Limit
    Redis 数据变化通知服务实践
    .net 相关性能计数器丢失问题解决方案
    为什么要DevOps?
    分布式服务发现的几种模型
  • 原文地址:https://www.cnblogs.com/codingforum/p/8318549.html
Copyright © 2011-2022 走看看