当前位置:网站首页>[List merge] Combine multiple lists into one list
[List merge] Combine multiple lists into one list
2022-08-10 12:32:00 【Tan Yue sword refers to the big factory】
多个list合并为一个list
/** * 多个list合并 * * @author : kwan * @version : 1.0.0 * @date : 2022/8/9 10:33 */
public class Java8_48_stream_merge_01 {
public static void main(String[] args) {
List<Person> personList = new ArrayList<>();
personList.add(new Person("Sherry", 9000, 24, "female", "New York"));
personList.add(new Person("Tom", 8900, 22, "male", "Washington"));
personList.add(new Person("Jack", 9000, 25, "male", "Washington"));
personList.add(new Person("Lily", 8800, 26, "male", "New York"));
personList.add(new Person("Alisa", 9000, 26, "female", "New York"));
List<Person> personList2 = new ArrayList<>();
personList2.add(new Person("Sherry", 9000, 24, "female", "New York"));
personList2.add(new Person("Tom", 8900, 22, "male", "Washington"));
personList2.add(new Person("Jack", 9000, 25, "male", "Washington"));
personList2.add(new Person("Lily", 8800, 26, "male", "New York"));
personList2.add(new Person("Alisa", 9000, 26, "female", "New York"));
List<List<Person>> list = new ArrayList<>();
list.add(personList);
list.add(personList2);
List<Person> persons = merge(list);
System.out.println(JSONArray.toJSONString(persons));
}
private static List<Person> merge(List<List<Person>> list) {
List<Person> persons = new ArrayList<>();
for (List<Person> people : list) {
for (Person person : people) {
persons.add(person);
}
}
return persons;
}
}
/** * 多个list合并 * * @author : kwan * @version : 1.0.0 * @date : 2022/8/9 10:33 */
public class Java8_48_stream_merge_02 {
public static void main(String[] args) {
List<Integer> list = ImmutableList.of(1, 3, 5);
list = list.stream().flatMap(l -> {
List<Integer> list1 = new ArrayList<>();
list1.add(l + 1);
list1.add(l + 2);
return list1.stream();
}).collect(Collectors.toList());
System.out.println(list);// [2, 3, 4, 5, 6, 7]
}
}
/** * 多个list合并 * * @author : kwan * @version : 1.0.0 * @date : 2022/8/9 10:33 */
public class Java8_48_stream_merge_03 {
public static void main(String[] args) {
List<List<Map<String, Object>>> lists = ImmutableList.of(
ImmutableList.of(
ImmutableMap.of("a", 1, "b", 2), ImmutableMap.of("a", 2, "b", 3)
),
ImmutableList.of(
ImmutableMap.of("a", 3, "b", 4), ImmutableMap.of("a", 4, "b", 5)
),
ImmutableList.of(
ImmutableMap.of("a", 5, "b", 6), ImmutableMap.of("a", 6, "b", 7)
)
);
// 将多个list合并为一个list
List<Map<String, Object>> list = lists.stream().flatMap(Collection::stream).collect(Collectors.toList());
System.out.println(JSONArray.toJSONString(list));
}
}
边栏推荐
- LeetCode 362. Design Hit Counter(计数器)
- Does face attendance choose face comparison 1:1 or face search 1:N?
- Data Analysis of Time Series (5): Simple Prediction Method
- 【list合并】多个list合并为一个list
- If someone asks you about distributed transactions again, throw this to him
- Color map and depth map to point cloud
- 可视化服务编排在金融APP中的实践
- search--09
- LeetCode 369. Plus One Linked List
- 一文详解 implementation api embed
猜你喜欢

郭晶晶家的象棋私教,好家伙是个机器人

LT8911EXB MIPI CSI/DSI转EDP信号转换

IDC第一的背后,阿里云在打造怎样的一朵“视频云”?

16. Getting Started with Pytorch Lightning

自定义过滤器和拦截器实现ThreadLocal线程封闭

一文详解 implementation api embed

Analysis of the implementation principle of UUID from the perspective of source code

面试美团被问到了Redis,搞懂这几个问题,让你轻松吊打面试官

How many constants and data types do you remember?

Can CLIP also do segmentation tasks?The University of Göttingen proposed a model CLIPSeg that uses text and image prompts to perform three segmentation tasks at the same time, draining CLIP capabiliti
随机推荐
一文读懂NFT数字藏品为何风靡全球?
LeetCode 19. 删除链表的倒数第 N 个结点
Configure druid data source "recommended collection"
HDU 4372:Count the Buildings (Stirling数)
LeetCode 83. 删除排序链表中的重复元素
Servlet---解决post请求中中文乱码问题
用低代码驱动IT现代化
时间序列的数据分析(五):简单预测法
搜索--09
解决 idea 单元测试不能使用Scanner
IM即时通讯开发WebSocket从入门到精通
LT8911EXB MIPI CSI/DSI to EDP signal conversion
国外媒体宣发怎样做才可以把握重点
LeetCode 19. Delete the Nth last node of the linked list
这三个 Go 水平自测题,你手写不出来还是先老实上班吧,过来看看
迈矽科推出高性能77GHz毫米波雷达芯片,尚未量产就已获数万颗订单
太香了!自从用了这款接口神器,我的团队效率提升了 60%!
If someone asks you about distributed transactions again, throw this to him
爱可可AI前沿推介(8.10)
Network sockets (UDP and TCP programming)