当前位置:网站首页>[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));
}
}
边栏推荐
- 吃透Chisel语言.36.Chisel实战之以FIFO为例(一)——FIFO Buffer和Bubble FIFO的Chisel实现
- 用低代码驱动IT现代化
- LeetCode 25. A set of K flipped linked lists
- mpf6_Time Series Data_quandl_更正kernel PCA_AIC_BIC_trend_log_return_seasonal_decompose_sARIMAx_ADFull
- LeetCode 21. 合并两个有序链表
- LT8911EXB MIPI CSI/DSI to EDP signal conversion
- An enhanced dynamic packet buffer management.论文核心部分
- What are some useful performance testing tools recommended? Performance testing report charging standards
- LeetCode 109. Sorted Linked List Conversion Binary Search Tree
- 面试官:你们是如何保证接口的幂等性?
猜你喜欢
How many constants and data types do you remember?
人脸考勤是选择人脸比对1:1还是人脸搜索1:N?
Chapter9 : De Novo Molecular Design with Chemical Language Models
Data Analysis of Time Series (5): Simple Prediction Method
Does your child lack self-discipline?Ape Counseling: Pay attention to "blank" in the schedule to give children more control
22年BATJ大厂必问面试题(复盘):JVM+微服务+多线程+锁+高并发
太香了!自从用了这款接口神器,我的团队效率提升了 60%!
项目部署、
蚂蚁金服+拼多多+抖音+天猫(技术三面)面经合集助你拿大厂offer
郭晶晶家的象棋私教,好家伙是个机器人
随机推荐
第5章 虚拟存储器
堪称神级的阿里巴巴“高并发”教程——基础+实战+源码+面试+架构 全包了
【LeetCode】640. 求解方程
Accumulated and thin hair!Safety Dog has once again obtained the certification of scientific and technological achievements transformation!
HDU 4135:Co-prime (容斥原理)
有哪些好用的性能测试工具推荐?性能测试报告收费标准
如何培养ui设计师的设计思维?
LeetCode 21. Merge two ordered linked lists
CLIP还能做分割任务?哥廷根大学提出一个使用文本和图像prompt,能同时作三个分割任务的模型CLIPSeg,榨干CLIP能力...
Dining (web stream)
three.js blur glass effect
jlink and swd interface definition
配置druid数据源「建议收藏」
解决 idea 单元测试不能使用Scanner
APP automation testing practice based on UiAutomator2+PageObject mode
LeetCode 61. 旋转链表
因为找不到lombok而找不到符号log
dedecms supports one-click import of Word content
【Redis】内存回收策略
LeetCode 25. A set of K flipped linked lists