当前位置:网站首页>[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));
}
}
边栏推荐
- Excel函数公式大全—LOOKUP函数
- LeetCode 86. Delimited Linked List
- 日记16
- 吃透Chisel语言.36.Chisel实战之以FIFO为例(一)——FIFO Buffer和Bubble FIFO的Chisel实现
- LeetCode 83. Remove Duplicate Elements in Sorted List
- IM即时通讯开发WebSocket从入门到精通
- 你是怎么知道数据库 Htap 能力强弱的?怎么能看出来
- 可视化服务编排在金融APP中的实践
- Ssm framework construction process [easy to understand]
- 22年BATJ大厂必问面试题(复盘):JVM+微服务+多线程+锁+高并发
猜你喜欢
APP automation testing practice based on UiAutomator2+PageObject mode
嘉为蓝鲸荣获工信部“数字技术融合创新应用解决方案”
阿里云贾朝辉:云XR平台支持彼真科技呈现国风科幻虚拟演唱会
阿里架构师整理一份企业级SSM架构实战文档,让你熟悉底层原理
2016,还是到了最后
孩子自律性不够?猿辅导:计划表要注意“留白”给孩子更多掌控感
22年BATJ大厂必问面试题(复盘):JVM+微服务+多线程+锁+高并发
16、Pytorch Lightning入门
Behind IDC's No. 1 position, what kind of "video cloud" is Alibaba Cloud building?
Data Analysis of Time Series (5): Simple Prediction Method
随机推荐
爱可可AI前沿推介(8.10)
LeetCode 146. LRU 缓存
Configure druid data source "recommended collection"
制品库是什么?
LeetCode 83. Remove Duplicate Elements in Sorted List
这三个 Go 水平自测题,你手写不出来还是先老实上班吧,过来看看
Golang分布式应用之etcd
蚂蚁金服+拼多多+抖音+天猫(技术三面)面经合集助你拿大厂offer
彩色图和深度图转点云
mpf6_Time Series Data_quandl_correct kernel PCA_AIC_BIC_trend_log_return_seasonal_decompose_sARIMAx_ADFull
正则表达式常用示例
bat脚本——提取多个文件夹到指定路径
配置druid数据源「建议收藏」
解决 idea 单元测试不能使用Scanner
Chapter9 : De Novo Molecular Design with Chemical Language Models
部署项目半途而废后续
国外媒体宣发怎样做才可以把握重点
7. Instant-ngp
日记16
LeetCode 19. Delete the Nth last node of the linked list