当前位置:网站首页>【list合并】多个list合并为一个list
【list合并】多个list合并为一个list
2022-08-10 11:42:00 【檀越剑指大厂】
多个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 369. Plus One Linked List(链表加1)
- std::move()
- 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
- LeetCode 61. Rotating linked list
- Flutter气泡框实现
- Go 事,Gopher 要学的数字类型,变量,常量,运算符 ,第2篇
- 厚积薄发!安全狗再次获得科技成果转化认证!
- You have a Doubaqiong thesaurus, please check it
- Dining (网络流)
猜你喜欢
随机推荐
蚂蚁金服+拼多多+抖音+天猫(技术三面)面经合集助你拿大厂offer
皕杰报表在传参乱码
The 6th "Blue Hat Cup" National College Student Network Security Skills Competition Semi-Final Part WriteUp
Nocalhost - Making development more efficient in the cloud-native era
制品库是什么?
可视化服务编排在金融APP中的实践
被面试官问到消息队列的丢失、重复与积压问题该如何回答
Analysis of the name matching process between the LCD driver and the device (Tiny4412)
嘉为蓝鲸荣获工信部“数字技术融合创新应用解决方案”
中芯CIM国产化项目暂停?上扬软件:未停摆,改为远程开发!
Configure druid data source "recommended collection"
厚积薄发!安全狗再次获得科技成果转化认证!
LeetCode 369. Plus One Linked List
LeetCode 21. Merge two ordered linked lists
LeetCode 146. LRU 缓存
LeetCode 138. 复制带随机指针的链表
mpf6_Time Series Data_quandl_更正kernel PCA_AIC_BIC_trend_log_return_seasonal_decompose_sARIMAx_ADFull
LeetCode 61. Rotating linked list
three.js模糊玻璃效果
CLIP还能做分割任务?哥廷根大学提出一个使用文本和图像prompt,能同时作三个分割任务的模型CLIPSeg,榨干CLIP能力...









