简体   繁体   English

如何使用Java流将一组对象转换为该对象的字段映射到一组字段?

[英]How can I convert a Set of objects into a Map of the Object's fields to a set of fields using Java streams?

If I have some class A 如果我有A班

public class A {
   String name;

   String value;
}

I have a Set which I want to convert into a Map<String, Set<String>> , which maps A.name to a set of A.value, using the stream class, since many instances of A will share the same name String, but none will share the same value String. 我有一个要转换为Map<String, Set<String>> ,它使用流类将A.name映射到A.value的集合,因为A的许多实例将共享相同的名称String ,但没有一个共享相同的字符串值。 Is there a way to do this? 有没有办法做到这一点?

To create a Map<String, Set<String>> , which maps A.name to a set of A.value , you need to use the following methods: 要创建Map<String, Set<String>> ,它映射A.name一组A.value ,你需要使用下面的方法:

Map<String, Set<String>> map = set.stream()
        .collect(Collectors.groupingBy(A::getName,
                                       Collectors.mapping(A::getValue,
                                                          Collectors.toSet())));

The code uses method references to getter methods on your A class, though lambda expressions can be used instead, eg A::getName could be replaced with o -> o.name . 该代码使用方法引用来引用A类上的getter方法,尽管可以使用lambda表达式代替,例如A::getName可以替换为o -> o.name

您可以在转换之前实例化一个空的Map,然后将使用匿名函数收集转换,该函数将检查该Map以查看它是否应该放置一个新集合或更新一个现有集合。

Java 8 转换一个List<object> 至 Map<k,v> 使用对象的孩子的领域?<div id="text_translate"><p> 情况很简单:我有一个 object List&lt;ParentClass&gt; list并想将其转换为Map&lt;orderKey, price&gt;</p><p> class 如下所示</p><pre>class ParentClass{ Child1Class a; Child2Class b; }</pre><p> orderKey class 看起来像这样</p><pre>class orderKey{ String id; String itemName; }</pre><p> 儿童班是这样的</p><pre>class Child1Class{ String id; String itemName; Date date; ..... } class Child2Class{ BigDecimal price; .... }</pre><p> 所有的类都有对应每个字段的 getter 和 setter。 所以我本质上是想 map 孩子们的领域。 我怎样才能做到这一点?</p><pre> -----------------------------MY ATTEMPT IS SOMETHING LIKE THIS-------------------------------------- list.stream(). .collect(Collectors.toMap(ParentClass::getA, ParentClass::getB).entrySet().stream()...... then I'm stuck</pre><p> 不知道如何构建临时 object orderKey作为新 map 的密钥。 任何帮助,将不胜感激。</p></div></k,v></object> - Java 8 convert a List<Object> to Map<K,V> using the fields of the object's children?

暂无
暂无

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

相关问题 如何使用Java中的对象字段将对象列表转换为映射 - How to convert List of Objects to map using object's fields in Java 如何检查一组对象是否包含 object,该 object 的特定字段在 Java 中有特定值? - How can I check if a Set of objects contains an object having a specific fields with a specific value in Java? Map 索引集到对象集,使用 Java 流 - Map set of indices to set of Objects, using Java streams Java 8流,将对象列表转换为Map <String, Set<String> &gt; - Java 8 streams, convert List of object to Map<String, Set<String>> Java集中对象的可变字段 - mutable fields for objects in a Java Set Java 8 转换一个List<object> 至 Map<k,v> 使用对象的孩子的领域?<div id="text_translate"><p> 情况很简单:我有一个 object List&lt;ParentClass&gt; list并想将其转换为Map&lt;orderKey, price&gt;</p><p> class 如下所示</p><pre>class ParentClass{ Child1Class a; Child2Class b; }</pre><p> orderKey class 看起来像这样</p><pre>class orderKey{ String id; String itemName; }</pre><p> 儿童班是这样的</p><pre>class Child1Class{ String id; String itemName; Date date; ..... } class Child2Class{ BigDecimal price; .... }</pre><p> 所有的类都有对应每个字段的 getter 和 setter。 所以我本质上是想 map 孩子们的领域。 我怎样才能做到这一点?</p><pre> -----------------------------MY ATTEMPT IS SOMETHING LIKE THIS-------------------------------------- list.stream(). .collect(Collectors.toMap(ParentClass::getA, ParentClass::getB).entrySet().stream()...... then I'm stuck</pre><p> 不知道如何构建临时 object orderKey作为新 map 的密钥。 任何帮助,将不胜感激。</p></div></k,v></object> - Java 8 convert a List<Object> to Map<K,V> using the fields of the object's children? 如何发现对象是否包含设置为0的数字字段? - How can I discover if an object contains numeric fields that are set to 0? 如何转换地图<String, List<String> &gt; 设置<String>使用流? - How to convert Map<String, List<String>> to Set<String> using streams? 如何使用流根据Map字段的条件过滤一组对象? - How do I filter using streams a set of objects based on criteria for a Map field? 使用过滤器和流将Map <String,Object>转换为Map <String,Set <Object >> - Convert Map<String, Object> to Map<String, Set<Object>> with filter and streams
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM