记一次List转map遇到的问题(java)

旧城冷巷雨未停 提交于 2020-03-19 14:52:06

3 月,跳不动了?>>>

Java8使用stream的List转map遇到的问题。
首先简单介绍一下Java8的新特性,Lambda表达式。一句话总结一下,就是一个匿名类函数或者叫闭包。他带来的好处是可以动态的去操作我们需要操作的数据结构。 平时开发的时候使用比较多的就是List转Map,比如:

    public static void main(String\[\] arg){  
        List<Test> testList = new ArrayList<>();  
        Map<Integer, Test> testMap = testList.stream().collect(Collectors.toMap(Test::getId, test -> test));  
    }

    [@Data](https://my.oschina.net/difrik)  
    private static class Test {  
        private Integer id;  
        private String name;  
    }  

我创建了一个Test的对象,将Test对象的集合转换成map。但从逻辑上来说,这是一点问题都没有的。
但是测试过后就会发现不对劲,让我们添加几组数据进行测试。

    public static void main(String\[\] arg){  
        List<Test> testList = new ArrayList<>();  
        testList.add(new Test(1, "test"));  
        testList.add(new Test(2, "test"));  
        testList.add(new Test(3, "test"));  
        testList.add(new Test(1, "test"));  
        Map<Integer, Test> testMap = testList.stream().collect(Collectors.toMap(Test::getId, test -> test));  
    }

    [@Data](https://my.oschina.net/difrik)  
    @AllArgsConstructor  
    private static class Test {  
        private Integer id;  
        private String name;  
    }  

编译发现,会出现一个很典型的错误。

Exception in thread "main" java.lang.IllegalStateException: Duplicate key FtpUtil.Test(id=1, name=test)  

异常说的很清楚,就是转换map的时候出现键值重复,所以需要解决这样一个异常。
直接上代码:

        Map<Integer, Test> testMap = testList.stream().collect(Collectors.toMap(Test::getId, Function.identity(),   
                (test1, test2) -> test1));  
    /**  
     \* Returns a function that always returns its input argument.  
     *  
     \* [@param](https://my.oschina.net/u/2303379) <T> the type of the input and output objects to the function  
     \* [@return](https://my.oschina.net/u/556800) a function that always returns its input argument  
     */  
    static <T> Function<T, T> identity() {  
        return t -> t;  
    }  

我们用到了Function这个接口, 官方说明的很清楚,返回一个需要使用的数据。

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!