Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用于地图列表的Java 8 groupingBy_Java_Java 8_Hashmap_Java Stream_Groupingby - Fatal编程技术网

用于地图列表的Java 8 groupingBy

用于地图列表的Java 8 groupingBy,java,java-8,hashmap,java-stream,groupingby,Java,Java 8,Hashmap,Java Stream,Groupingby,我有一个地图列表,我想按名称对它们进行分组 List<Map> allObjs = new ArrayList<Map>(); Map<String, Object> src = new HashMap(); src.put("name", "asset1"); src.put("description", "desc1" ); src.put("definition", "def1" ); Map<String, Object> src2 =

我有一个地图列表,我想按名称对它们进行分组

List<Map> allObjs = new ArrayList<Map>();
Map<String, Object> src = new HashMap();
src.put("name", "asset1");
src.put("description", "desc1" );
src.put("definition", "def1" );

Map<String, Object> src2 = new HashMap();
src2.put("name", "asset1");
src2.put("description", "desc2" );
src2.put("definition", "def2" );
allObjs.add(src);
allObjs.add(src2);
我希望将所有其他键分组到一个列表中 但我得到的是

{asset1=[
            {name=asset1, description=desc1, definition=def2},
            {name=asset1, description=desc2, definition=def2}
        ]
}

如何将除name之外的所有其他键分组到如上所述的列表中?

groupingBy
将根据键对项目进行分组。它不会合并两个贴图。这是实现你想要的最丑陋的方式之一

给定

List<Map<String, String>> allObjs = new ArrayList<>();
Map<String, String> src = new HashMap();
src.put("name", "asset1");
src.put("description", "desc1" );
src.put("definition", "def1" );

Map<String, String> src2 = new HashMap();
src2.put("name", "asset1");
src2.put("description", "desc2" );
src2.put("definition", "def2" );
allObjs.add(src);
allObjs.add(src2);
List allObjs=new ArrayList();
Map src=new HashMap();
src.put(“名称”、“资产1”);
src.put(“说明”、“说明1”);
src.put(“定义”、“定义1”);
Map src2=新的HashMap();
src2.put(“名称”、“资产1”);
src2.put(“说明”、“说明2”);
src2.put(“定义”、“定义2”);
allObjs.add(src);
allObjs.add(src2);
一种方法是这样做。创建两个类,分别表示现有结构和最终输出结构

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class SomeClass {
    private String name;
    private String description;
    private String definition;
}

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class NewClass {
    private String name;
    private List<String> description;
    private List<String> definition;
}
@Getter
@塞特
@AllArgsConstructor
@诺尔格构装师
@托斯特林
公共类{
私有字符串名称;
私有字符串描述;
私有字符串定义;
}
@吸气剂
@塞特
@AllArgsConstructor
@诺尔格构装师
@托斯特林
公共类新类{
私有字符串名称;
私有列表描述;
私人名单定义;
}
然后这个代码应该得到所需的输出u

final List<NewClass> collect = allObjs.stream()
        .map(entry -> new SomeClass(entry.get("name"), entry.get("description"), entry.get("definition")))
        .collect(Collectors.toList())
        .stream()
        .collect(Collectors.groupingBy(SomeClass::getName))
        .entrySet()
        .stream()
        .map(entry -> {
            final List<String> descriptions = entry.getValue().stream().map(SomeClass::getDescription).collect(Collectors.toList());
            final List<String> definitions = entry.getValue().stream().map(SomeClass::getDefinition).collect(Collectors.toList());
            return new NewClass(entry.getKey(), descriptions, definitions);
        }).collect(Collectors.toList());
final List collect=allObjs.stream()
.map(entry->newsomeclass(entry.get(“name”)、entry.get(“description”)、entry.get(“定义”))
.collect(收集器.toList())
.stream()
.collect(收集器.groupingBy(SomeClass::getName))
.entrySet()
.stream()
.map(条目->{
最终列表描述=entry.getValue().stream().map(SomeClass::getDescription).collect(Collectors.toList());
最终列表定义=entry.getValue().stream().map(SomeClass::getDefinition).collect(Collectors.toList());
返回新的NewClass(entry.getKey()、说明、定义);
}).collect(Collectors.toList());

最终结果将是NewClass类型的列表。列表中的每个条目都将具有所需的输出,并具有唯一的键。由于这是我们正在处理的映射,您需要合并大量空检查。

您可以使用
groupBy
mapping
来获得所需的结果,只是值只能是单一类型

Map<String, Set<String>> collect1 = allObjs.stream()
                .flatMap(m -> m.entrySet().stream())
                .collect(groupingBy(Map.Entry::getKey, mapping(Map.Entry::getValue, toSet())));

groupingBy将根据键对列表中的项目进行分组。它不会合并两个贴图
final List<NewClass> collect = allObjs.stream()
        .map(entry -> new SomeClass(entry.get("name"), entry.get("description"), entry.get("definition")))
        .collect(Collectors.toList())
        .stream()
        .collect(Collectors.groupingBy(SomeClass::getName))
        .entrySet()
        .stream()
        .map(entry -> {
            final List<String> descriptions = entry.getValue().stream().map(SomeClass::getDescription).collect(Collectors.toList());
            final List<String> definitions = entry.getValue().stream().map(SomeClass::getDefinition).collect(Collectors.toList());
            return new NewClass(entry.getKey(), descriptions, definitions);
        }).collect(Collectors.toList());
Map<String, Set<String>> collect1 = allObjs.stream()
                .flatMap(m -> m.entrySet().stream())
                .collect(groupingBy(Map.Entry::getKey, mapping(Map.Entry::getValue, toSet())));
{
 name=[asset1],
 description=[desc1,desc2],
 definition=[def1,def2]
}