hutool 两个list取并集
时间: 2023-08-21 20:05:10 浏览: 236
你可以使用Hutool提供的CollUtil工具类来实现两个List的并集操作。具体实现方法如下所示:
```java
import cn.hutool.core.collection.CollUtil;
List<Integer> list1 = new ArrayList<>(Arrays.asList(1, 2, 3));
List<Integer> list2 = new ArrayList<>(Arrays.asList(2, 3, 4));
List<Integer> union = CollUtil.union(list1, list2);
```
在上述代码中,我们首先创建了两个List对象list1和list2,分别包含了不同的整数元素。然后使用CollUtil提供的union方法将两个List对象取并集,返回的结果是一个新的List对象,包含了list1和list2中的所有元素,但不包含重复的元素。最后,我们将结果保存在名为union的List对象中。
相关问题
两个list集合取并集
可以使用 Python 中的 `set` 数据类型来实现两个列表的并集操作。具体代码如下:
```python
list1 = [1, 2, 3, 4, 5]
list2 = [4, 5, 6, 7, 8]
set1 = set(list1)
set2 = set(list2)
union_set = set1.union(set2)
result_list = list(union_set)
print(result_list)
```
以上代码中,我们首先将两个列表分别转换成 `set` 集合,然后使用 `union` 方法求两个集合的并集。最后再将结果转换成列表输出。输出结果为 `[1, 2, 3, 4, 5, 6, 7, 8]`。
python取两个list的并集
可以使用set()函数来取两个list的并集,然后再将其转换回list类型。
示例代码:
```
list1 = [1, 2, 3, 4]
list2 = [3, 4, 5, 6]
union = list(set(list1).union(set(list2)))
print(union)
```
输出结果为:
```
[1, 2, 3, 4, 5, 6]
```
其中set()函数用来将list转换为集合类型,然后使用union()函数求并集,最后再将集合类型转换为list类型。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)