gpt4 book ai didi

java - 从java中的两个不同的数组列表对象中找到不常见的,常见的所有元素

转载 作者:太空狗 更新时间:2023-10-29 16:35:46 26 4
gpt4 key购买 nike

我试图从 java 中的两个不同的非有序数组列表对象中找到不常见的、常见的项目。我已经阅读了很多关于这些的帖子,但找不到合适的答案。

第一个数组列表对象存储从服务器获取的数据。第二个数组列表对象存储本地数据库数据。

现在我试图从这两个数组列表中找到常见的、不常见的所有元素。这里的数组列表是由完全不同的两个模型类生成的,但它们具有相似的属性。

当我将条件设置为“!listA.id.equals(listB.id)”时,相等比较确实给出了公共(public)值但无法从两个数组列表中找到不常见的项目。

例如:

for(CustomStation user1 : localStationLists) {
for(CustomStation user2 : serverStationLists) {
if(user1.getStationId().equals(user2.getStationId())) {
*//*if(!user1.getTitle().equals(user2.getTitle())) {
resultList.add(user1);
}*//*
//System.out.println(" EQUAL St ids : " + user1);
resultList.add(user2);
}
else{
resultList1.add(user1);
}
}

所以,想一想你们是否也有同样的问题?

过去三天一直在尝试不同的方法,但多次未能找到解决方案。

最佳答案

这些对我来说就像集合操作:联合、重叠和差异。

看看这个:

Classical set operations for java.util.Collection

非常适合我。这是代码:

import java.util.ArrayList;
import java.util.List;

/**
* Add something descriptive here.
* User: mduffy
* Date: 3/26/2015
* Time: 1:27 PM
* @link https://stackoverflow.com/questions/29284061/find-the-uncommon-common-all-elements-from-two-different-array-list-objects-in/29284162?noredirect=1#comment46767251_29284162
*/
public class SetOperationDemo {

public static void main(String[] args) {
List<String> setOne = new ArrayList<String>() {{
add("A");
add("B");
add("C");
add("D");
add("E");
}};
List<String> setTwo = new ArrayList<String>() {{
add("D");
add("E");
add("F");
add("G");
}};

System.out.println("Set A : " + setOne);
System.out.println("Set B : " + setTwo);
List<String> base = new ArrayList<String>(setOne);
base.retainAll(setTwo);
System.out.println("Intersection A+B: " + base);
base = new ArrayList<String>(setOne);
base.removeAll(setTwo);
System.out.println("Subtraction A-B: " + base);
base = new ArrayList<String>(setTwo);
base.removeAll(setOne);
System.out.println("Subtraction B-A: " + base);
base = new ArrayList<String>(setOne);
base.addAll(setTwo);
System.out.println("Union A union B : " + base);
}
}

这是输出:

Set A           : [A, B, C, D, E]
Set B : [D, E, F, G]
Intersection A+B: [D, E]
Subtraction A-B: [A, B, C]
Subtraction B-A: [F, G]
Union A union B : [A, B, C, D, E, D, E, F, G]

Process finished with exit code 0

如果您的列表包含自定义类,您必须确保它们正确覆盖 equals 和 hashCode,否则它们将不会提供预期的行为。这是我使用自定义类的代码,展示了它是如何完成的。

import java.util.ArrayList;
import java.util.List;

/**
* Add something descriptive here.
* User: mduffy
* Date: 3/26/2015
* Time: 1:27 PM
* @link https://stackoverflow.com/questions/29284061/find-the-uncommon-common-all-elements-from-two-different-array-list-objects-in/29284162?noredirect=1#comment46767251_29284162
*/
public class SetOperationDemo {

public static void main(String[] args) {
List<DemoPerson> setOne = new ArrayList<DemoPerson>() {{
add(new DemoPerson("Andy", "A"));
add(new DemoPerson("Bob", "B"));
add(new DemoPerson("Carl", "C"));
add(new DemoPerson("David", "D"));
add(new DemoPerson("Ernie", "E"));
}};
List<DemoPerson> setTwo = new ArrayList<DemoPerson>() {{
add(new DemoPerson("David", "D"));
add(new DemoPerson("Ernie", "E"));
add(new DemoPerson("Frank", "F"));
add(new DemoPerson("Gary", "G"));
}};

System.out.println("Set A : " + setOne);
System.out.println("Set B : " + setTwo);
List<DemoPerson> base = new ArrayList<DemoPerson>(setOne);
base.retainAll(setTwo);
System.out.println("Intersection A+B: " + base);
base = new ArrayList<DemoPerson>(setOne);
base.removeAll(setTwo);
System.out.println("Subtraction A-B: " + base);
base = new ArrayList<DemoPerson>(setTwo);
base.removeAll(setOne);
System.out.println("Subtraction B-A: " + base);
base = new ArrayList<DemoPerson>(setOne);
base.addAll(setTwo);
System.out.println("Union A union B : " + base);
}
}

class DemoPerson {
private final String firstName;
private final String lastName;

public DemoPerson(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

DemoPerson that = (DemoPerson) o;

return !(firstName != null ? !firstName.equals(that.firstName) : that.firstName != null) && !(lastName != null ? !lastName.equals(that.lastName) : that.lastName != null);

}

@Override
public int hashCode() {
int result = firstName != null ? firstName.hashCode() : 0;
result = 31 * result + (lastName != null ? lastName.hashCode() : 0);
return result;
}

@Override
public String toString() {
final StringBuilder sb = new StringBuilder("{");
sb.append("'").append(firstName).append('\'');
sb.append(" '").append(lastName).append('\'');
sb.append('}');
return sb.toString();
}
}

这是输出,仍然表现得像它应该的那样:

Set A           : [{'Andy' 'A'}, {'Bob' 'B'}, {'Carl' 'C'}, {'David' 'D'}, {'Ernie' 'E'}]
Set B : [{'David' 'D'}, {'Ernie' 'E'}, {'Frank' 'F'}, {'Gary' 'G'}]
Intersection A+B: [{'David' 'D'}, {'Ernie' 'E'}]
Subtraction A-B: [{'Andy' 'A'}, {'Bob' 'B'}, {'Carl' 'C'}]
Subtraction B-A: [{'Frank' 'F'}, {'Gary' 'G'}]
Union A union B : [{'Andy' 'A'}, {'Bob' 'B'}, {'Carl' 'C'}, {'David' 'D'}, {'Ernie' 'E'}, {'David' 'D'}, {'Ernie' 'E'}, {'Frank' 'F'}, {'Gary' 'G'}]

Process finished with exit code 0

关于java - 从java中的两个不同的数组列表对象中找到不常见的,常见的所有元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29284061/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com