list<lsit<String>> name 转换成 list<string> name,使用stream流怎么处理
时间: 2023-12-14 16:02:26 浏览: 35
可以使用flatMap方法将嵌套的list展平,然后再使用collect方法将所有元素收集到一个新的list中。下面是示例代码:
```java
List<List<String>> nestedList = new ArrayList<>();
nestedList.add(Arrays.asList("Alice", "Bob"));
nestedList.add(Arrays.asList("Charlie", "David", "Emily"));
nestedList.add(Arrays.asList("Frank"));
List<String> flatList = nestedList.stream()
.flatMap(Collection::stream)
.collect(Collectors.toList());
System.out.println(flatList);
```
输出结果为:
```
[Alice, Bob, Charlie, David, Emily, Frank]
```
相关问题
stream流对List<Map<String,String>>某个字段去重
你好,关于你的问题,可以使用Java 8中的stream流的distinct()方法对List<Map<String,String>>某个字段进行去重。具体代码演示可以参考以下示例:
```
List<Map<String,String>> list = new ArrayList<>();
list.add(new HashMap<String,String>(){{put("id","1"); put("name","John");}});
list.add(new HashMap<String,String>(){{put("id","2"); put("name","Peter");}});
list.add(new HashMap<String,String>(){{put("id","3"); put("name","John");}});
list.add(new HashMap<String,String>(){{put("id","4"); put("name","Mary");}});
List<Map<String,String>> distinctList = list.stream().filter(
distinctByKey(map -> map.get("name"))
).collect(Collectors.toList());
System.out.println(distinctList);
// Output: [{id=1, name=John}, {id=2, name=Peter}, {id=4, name=Mary}]
public static <T> Predicate<T> distinctByKey(Function<? super T, Object> keyExtractor) {
Map<Object, Boolean> seen = new ConcurrentHashMap<>();
return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
}
```
希望能帮到你,如果你有其他问题可以继续提出。
使用stream流对List<Map<String,Object>>进行排序
可以使用Java 8的流式API和Comparator来对List<Map<String, Object>>进行排序。以下是示例代码:
```java
List<Map<String, Object>> list = new ArrayList<>();
// 添加Map数据
Map<String, Object> map1 = new HashMap<>();
map1.put("name", "张三");
map1.put("age", 20);
list.add(map1);
Map<String, Object> map2 = new HashMap<>();
map2.put("name", "李四");
map2.put("age", 18);
list.add(map2);
Map<String, Object> map3 = new HashMap<>();
map3.put("name", "王五");
map3.put("age", 22);
list.add(map3);
// 按照年龄从小到大排序
List<Map<String, Object>> sortedList = list.stream()
.sorted(Comparator.comparingInt(map -> (int) map.get("age")))
.collect(Collectors.toList());
```
在代码中,我们首先创建了一个List<Map<String, Object>>,其中包含了三个Map对象。然后,我们使用stream()方法将List转换为Stream,接着使用sorted()方法和Comparator.comparingInt()方法按照年龄从小到大排序。最后,我们使用collect()方法将Stream转换为List并将结果保存在sortedList中。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)