当前位置:网站首页>[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 function formulas - HLOOKUP function
- The author of open source also has a life problem
- 第六届”蓝帽杯“全国大学生网络安全技能大赛半决赛部分WriteUp
- LeetCode 61. Rotating linked list
- If someone asks you about distributed transactions again, throw this to him
- 【list合并】多个list合并为一个list
- 面试美团被问到了Redis,搞懂这几个问题,让你轻松吊打面试官
- 可视化服务编排在金融APP中的实践
- Excel函数公式大全—LOOKUP函数
- codevs 2370 Small room tree (LCA)
猜你喜欢
解决 idea 单元测试不能使用Scanner
自定义过滤器和拦截器实现ThreadLocal线程封闭
2016,还是到了最后
CLIP还能做分割任务?哥廷根大学提出一个使用文本和图像prompt,能同时作三个分割任务的模型CLIPSeg,榨干CLIP能力...
The 6th "Blue Hat Cup" National College Student Network Security Skills Competition Semi-Final Part WriteUp
Analysis of the implementation principle of UUID from the perspective of source code
IDC第一的背后,阿里云在打造怎样的一朵“视频云”?
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
蚂蚁金服+拼多多+抖音+天猫(技术三面)面经合集助你拿大厂offer
mpf6_Time Series Data_quandl_更正kernel PCA_AIC_BIC_trend_log_return_seasonal_decompose_sARIMAx_ADFull
随机推荐
LeetCode 61. Rotating linked list
Behind IDC's No. 1 position, what kind of "video cloud" is Alibaba Cloud building?
jlink and swd interface definition
LeetCode 369. Plus One Linked List
jlink 与 swd 接口定义
LeetCode50天刷题计划(Day 17—— 下一个序列(14.50-16.30)
Cannot find symbol log because lombok is not found
LeetCode 362. Design Hit Counter(计数器)
孩子自律性不够?猿辅导:计划表要注意“留白”给孩子更多掌控感
search--01
LeetCode 445. 两数相加 II
CURRENT_TIMESTAMP(6) 函数是否存在问题?
Chapter 5 virtual memory
LeetCode 86. 分隔链表
LeetCode 19. Delete the Nth last node of the linked list
【集合】HashSet和ArrayList的查找Contains()时间复杂度
If someone asks you about distributed transactions again, throw this to him
bat脚本——提取多个文件夹到指定路径
LeetCode 92. 反转链表 II
ssm框架搭建过程[通俗易懂]