我试图筛选,并通过这样做,减少List<Map<String, Object>>
到List<String>
与Java8新的lambda表达式:减少多阵列列表
List<Map<String, Object>> myObjects = new ArrayList<>();
myObjects.stream()
.filter(myObject-> myObject.get("some integer value").equals(expectedValue))
// myObject.get("some attribute") == ["some string", "maybe another string"]
.map(myObject-> myObject.get("some attribute"))
.collect(Collectors.toList());
结果是一个列表,但我想所有的字符串内结合数组到结果List<String>
。
要澄清,这是目前我得到的结果是:
ArrayList{["some string"], ["another string"]}
但我想这一点:
ArrayList{"some string", "another string"}
有人可以给我一个提示,其中一部分,我不得不降低String[]
到String
?我猜想它在.map()
部分,但我不知道我会在那里改变。
编辑:
那怎么List<Map<String, Object>> myObjects
可用于测试目的而产生的:
List<Map<String, Object>> myObjects = new ArrayList<>();
Map<String, Object> myObject = new HashMap<>();
myObject.put("some integer value", 1);
String[] theStringIWant = new String[1];
theStringIWant[0] = "Some important information I want";
myObject.put("some attribute", theStringIWant);
myObjects.add(myObject);
,看起来像这样:
List<MyObject{"some attribute": 1}>
注:这只是一个例子来自我的单元测试。该列表通常包含多个元素,每个地图都有更多的属性,而不仅仅是some attribute
。
很难说没有看到你原来'Map',但你可能需要'.flatmap(myObject->((List)myObject.get(“list”))。stream())' –
如果你给我几分钟时间,我可以提供一个更好的例子。 :) – Peter
如果我尝试这个我得到一个'java.lang.ClassCastException:[Ljava.lang.String;不能转换为java.util.List' – Peter