java求两集合中没有的值_java 两个List集合各种情况对比处理
时间: 2024-03-26 17:34:42 浏览: 23
您好!感谢您的提问。针对您的问题,可以使用 Java 中的 Collection 工具类来求两个 List 集合中的没有的值,具体实现方式如下:
```java
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<Integer> list1 = new ArrayList<Integer>();
List<Integer> list2 = new ArrayList<Integer>();
list1.add(1);
list1.add(2);
list1.add(3);
list2.add(2);
list2.add(3);
list2.add(4);
// 求list1中没有的值
List<Integer> diff1 = new ArrayList<Integer>(list1);
diff1.removeAll(list2);
System.out.println("list1中没有的值:" + diff1);
// 求list2中没有的值
List<Integer> diff2 = new ArrayList<Integer>(list2);
diff2.removeAll(list1);
System.out.println("list2中没有的值:" + diff2);
// 求两个集合中都没有的值
List<Integer> diff3 = new ArrayList<Integer>();
Collection<Integer> intersection = new ArrayList<Integer>(list1);
intersection.retainAll(list2);
diff3.addAll(intersection);
System.out.println("两个集合中都没有的值:" + diff3);
}
}
```
输出结果如下:
```
list1中没有的值:[1]
list2中没有的值:[4]
两个集合中都没有的值:[2, 3]
```
上述代码中,我们先创建了两个 List 集合 list1 和 list2,然后分别求出 list1 中没有的值、list2 中没有的值和两个集合中都没有的值。具体实现方式如下:
- 求 list1 中没有的值:
```java
List<Integer> diff1 = new ArrayList<Integer>(list1);
diff1.removeAll(list2);
```
首先,我们新建一个 ArrayList 对象 diff1,并将 list1 的所有元素添加进去。然后,使用 removeAll() 方法,将 list2 中的所有元素从 diff1 中删除,最终得到 list1 中没有的值。
- 求 list2 中没有的值:
```java
List<Integer> diff2 = new ArrayList<Integer>(list2);
diff2.removeAll(list1);
```
类似地,我们新建一个 ArrayList 对象 diff2,并将 list2 的所有元素添加进去。然后,使用 removeAll() 方法,将 list1 中的所有元素从 diff2 中删除,最终得到 list2 中没有的值。
- 求两个集合中都没有的值:
```java
List<Integer> diff3 = new ArrayList<Integer>();
Collection<Integer> intersection = new ArrayList<Integer>(list1);
intersection.retainAll(list2);
diff3.addAll(intersection);
```
首先,我们新建一个 ArrayList 对象 diff3,用于存放两个集合中都没有的值。然后,新建一个 ArrayList 对象 intersection,将 list1 的所有元素添加进去。使用 retainAll() 方法,将 intersection 中不在 list2 中的元素删除,最终得到两个集合中都有的元素。最后,将 intersection 中的元素添加到 diff3 中即可。
希望这些代码能够对您有所帮助。如果您有任何问题,请随时问我。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)
![](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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)