[英]How to convert with Java stream List<Map<String,Object>> to List<String> removing part of each Stream
I have a method :我有一个方法:
public List<Map<String, Object>> getAllListsExcept() {
String query = "SELECT Name FROM " + account.getName()
+ " WHERE NOT (Name LIKE '%1234567%')";
return SQLHelper.getByQuery(query);
}
return list:退货清单:
[{name=AQA Chief Officer}, {name=AQA Internal Accounts}, {name=AQA Interactions}, {name=AQA Legacy Planner}, {name=AQA Principal Facilitator}, {name=AQA Regional Program}, {name=cd_AQA Cassattatque}, {name=cd_AQA Kandinskyfugiat}, {name=cd_AQA Raphaelquitester}, {name=cd_AQA Rembrandtanimi}, {name=cd_AQA Seuratest}]
I want to return me List only names without these part: 'name=' Like this:我想返回我只列出没有这些部分的名字:'name=' 像这样:
[{AQA Chief Officer}, {AQA Internal Accounts}, {AQA Interactions}, {AQA Legacy Planner}, {AQA Principal Facilitator}, {AQA Regional Program}, {AQA Cassattatque}, {AQA Kandinskyfugiat}, {AQA Raphaelquitester}, {AQA Rembrandtanimi}, {AQA Seuratest}]
Is it possible not to use a substring?是否可以不使用子字符串?
Thanks谢谢
You can use a map from stream to perform this task, it should look like something like this in case your Map
has exactly one value您可以使用来自流的地图来执行此任务,如果您的Map
只有一个值,它应该看起来像这样
getAllListsExcept().stream().map(hmap->hmap.values().iterator().next())
.collect(Collectors.toList());
You can get the values from Map
and use flatMap
您可以从Map
获取值并使用flatMap
List<Object> result = getAllListsExcept().stream()
.map(Map::values)
.flatMap(Collection::stream)
.collect(Collectors.toList());
Is it possible not to use a substring?是否可以不使用子字符串?
Yes, you don't have to use a substring solution.是的,您不必使用子字符串解决方案。 In fact, using a substring solution would not be the best.事实上,使用子串解决方案并不是最好的。 You can use a stream
.您可以使用stream
。
List<Map<String, Object>> list = List.of(
Map.of("name", "AQA Chief Officer"),
Map.of("name", "AQA Internal Accounts"),
Map.of("name", "AQA Legacy Planner"),
Map.of("name",
"AQA Principal Facilitator"));
List<String> result = list.stream()
.flatMap(map -> map.values().stream())
.map(Object::toString)
.collect(Collectors.toList());
result.forEach(System.out::println);
It prints the following.它打印以下内容。
AQA Chief Officer AQA 首席官
AQA Internal Accounts AQA内部账户
AQA Legacy Planner AQA 遗产规划师
AQA Principal Facilitator AQA首席协调员
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.