public class MergeListsExample {
public static void main(String[] args) {
// 假设你有两个包含相同实体类对象的 List 集合
List<YourObject> list1 = List.of(
new YourObject("value1"),
new YourObject("value2")
);
List<YourObject> list2 = List.of(
new YourObject("value3"),
new YourObject("value4")
);
// 使用 Stream API 的 concat 方法合并两个 List 集合
List<YourObject> mergedList = Stream.concat(list1.stream(), list2.stream())
.collect(Collectors.toList());
System.out.println(mergedList);
}
}
Stream 流实现两个相同实体类对象list合并成一个list
最新推荐文章于 2024-04-29 13:45:57 发布