简体   繁体   English

Java 8:如何从从相同 class (empID) 和值作为对象(Employee) 本身获取 Key 的列表中创建 map?

[英]Java 8: How to create map from list where Key is fetched from same class (empID) and value as object(Employee) itself?

How to convert from the list to map in Java 8 were in form of empID from within class and value as an object itself.如何从列表转换为 Java 8 中的 map 是 class 中的empID形式,其值为 ZA2668CFEB89 本身。

List<CompanyEntity> result = 
             ifEntityManager.createNamedQuery("findByEmployeeId", CompanyEntity.class)
             .getResultList();
Map<String, CompanyEntity> result1 = result.stream()
        .collect(Collectors.toMap(CompanyEntity::getEmployeeId, CompanyEntity::this)); ??

I want the second parameter in toMap as object itself.我希望toMap中的第二个参数作为 object 本身。 Could anyone suggest how to achieve the same?任何人都可以建议如何实现相同的目标吗?

I tried doing Collectors.toMap(CompanyEntity::getEmployeeId, this) but not able to get away from compile errors.我尝试做Collectors.toMap(CompanyEntity::getEmployeeId, this)但无法摆脱编译错误。

You can use Function.identity() from java.util.function :您可以使用 java.util.function 中的java.util.function Function.identity()

Map<String, CompanyEntity> result1 = result.stream()
    .collect(Collectors.toMap(CompanyEntity::getEmployeeId, Function.identity()));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从两个具有相同键和值的列表创建对象 - Create map from two lists with key and value as same object 如何从 Map 获取值,其中 Object 是密钥对列表? - How to get values from Map where Object is a list of key pairs? Java 8 Stream convert list to map, where map key is an object derived from two fields of the list? - Java 8 Stream convert list to map, where map key is an object derived from two fields of the list? Map&lt;&gt; 中的 Map&lt;&gt; 作为值 -&gt; 如何从 Map&lt;&gt; 创建 object,其中 value 是另一个 Map&lt;&gt;? - Map<> in Map<> as value -> How to create object from the Map<> where value is another Map<>? 从 Java 中的 Map 中删除 Class object 密钥 - Removal of Class object key from Map in Java 使用 JAVA 8 Streams 从地图值是列表的地图中仅创建 1 个列表 - Create only 1 list from a map where map value is list using JAVA 8 Streams Java如何通过键对象从Map中获取值 - Java how to get value from Map by key-object 如何从列表中删除键/值对<Map<String, Object> &gt; - How to remove the key/value pair from a List<Map<String, Object>> 在Java 8中优雅地创建具有对象字段的映射作为来自对象流的键/值 - Elegantly create map with object fields as key/value from object stream in Java 8 从列表创建映射,其中键是内部和外部对象的一部分 - Creating map from list, where key is in part of inner and outer object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM