java怎样找出两个List中不同的对象
时间: 2023-05-23 07:02:39 浏览: 411
你可以使用 Java 8 中的 Stream API 来找出两个 List 中不同的对象。这里提供一种实现方法:
```
// 假设有两个 List<Integer>
List<Integer> list1 = Arrays.asList(1, 2, 3, 4);
List<Integer> list2 = Arrays.asList(3, 4, 5, 6);
List<Integer> diff = list1.stream()
.filter(n -> !list2.contains(n))
.collect(Collectors.toList());
System.out.println("List1中与List2不同的元素为: " + diff); // 输出 [1,2]
```
这个方法首先通过 `list1.stream()` 将 List 转化成 Stream。接着通过 `filter` 方法过滤掉 `list2` 中存在的元素,最后通过 `collect` 方法将结果收集到一个新的 List 中。
相关问题
java如何将不同的两个List集合中对象数据合并
要将两个不同的List集合中的对象数据合并,需要注意几点:
1. 对象必须具有相同的类型,否则无法合并。
2. 对象必须实现equals()和hashCode()方法,以便正确地去重和比较。
3. 如果两个对象相同,则只保留一个,以避免重复。
下面是一个示例代码,演示如何将两个不同的List集合中的对象数据合并:
```java
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class MergeListDemo {
public static void main(String[] args) {
List<Person> list1 = new ArrayList<>();
list1.add(new Person("Tom", 18));
list1.add(new Person("Lucy", 20));
List<Person> list2 = new ArrayList<>();
list2.add(new Person("Jim", 22));
list2.add(new Person("Lucy", 20));
Set<Person> set = new HashSet<>();
set.addAll(list1);
set.addAll(list2);
List<Person> list3 = new ArrayList<>(set);
System.out.println(list3);
}
}
class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public boolean equals(Object obj) {
if (obj == null) return false;
if (this == obj) return true;
if (!(obj instanceof Person)) return false;
Person other = (Person) obj;
if (name == null) {
if (other.name != null) return false;
} else if (!name.equals(other.name)) return false;
if (age != other.age) return false;
return true;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + age;
return result;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
}
```
在上面的示例代码中,我们创建了两个List集合list1和list2,并向每个集合中添加了两个Person类型的对象。然后,我们创建了一个HashSet集合set,将list1和list2中的所有元素添加到set中。由于HashSet会自动去重,所以我们可以确保最终合并后的集合中不会有重复的对象。最后,我们将set中的元素转换为List集合并输出。输出结果为:
```
[Person [name=Lucy, age=20], Person [name=Jim, age=22], Person [name=Tom, age=18]]
```
注意,输出结果中只保留了一个name为Lucy、age为20的对象,这是因为我们在Person类中实现了equals()和hashCode()方法,确保了对象的唯一性。
java中对比两个list对象,获取两个list对象不同的数据集合
可以在Java中使用Collection的retainAll()方法来比较两个List对象并获取它们的差异集合。具体步骤如下:
1. 先创建两个List对象list1和list2,假设它们的元素类型是String。
2. 调用list1的retainAll(list2)方法,该方法会修改list1,使其只保留list2中也包含的元素,也就是list1和list2的交集。此时list1中只包含两个List对象共有的元素。
3. 调用list2的retainAll(list1)方法,该方法会修改list2,使其只保留list1中也包含的元素,也就是list1和list2的交集。此时list2中只包含两个List对象共有的元素。
4. 调用list1的removeAll(list2)方法,该方法会从list1中删除list2中也包含的元素,也就是list1除去两个List对象共有的元素后剩余的元素,即list1和list2的差集。
5. 调用list2的removeAll(list1)方法,该方法会从list2中删除list1中也包含的元素,也就是list2除去两个List对象共有的元素后剩余的元素,即list2和list1的差集。
下面是示例代码:
```java
List<String> list1 = new ArrayList<>();
list1.add("a");
list1.add("b");
list1.add("c");
List<String> list2 = new ArrayList<>();
list2.add("b");
list2.add("c");
list2.add("d");
List<String> intersection = new ArrayList<>(list1);
intersection.retainAll(list2); // 取交集
List<String> difference1 = new ArrayList<>(list1);
difference1.removeAll(list2); // 取差集
List<String> difference2 = new ArrayList<>(list2);
difference2.removeAll(list1); // 取差集
System.out.println("list1和list2的交集:" + intersection); // 输出 [b, c]
System.out.println("list1和list2的差集:" + difference1); // 输出 [a]
System.out.println("list2和list1的差集:" + difference2); // 输出 [d]
```
以上代码输出的结果为:
```
list1和list2的交集:[b, c]
list1和list2的差集:[a]
list2和list1的差集:[d]
```