我试图从java中的两个不同的非有序数组列表对象中找到不常见的常用项。我已经阅读了很多关于这些的帖子,但找不到合适的答案。
第一个数组列表对象存储从服务器获取的数据。第二个数组列表对象存储本地数据库数据。
现在我试图从这两个数组列表中找到常见的,不常见的所有元素。这里的数组列表完全由两个不同的模型类生成,但它们具有相似的属性。
当我将条件设置为"!listA.id.equals(listB.id)&#34时,相等的比较确实给出了共同的值,但是无法从两个数组列表中找到不常见的项目;
例如:
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);
}
}
那么,想一想你们是否也有同样的问题?
过去三天一直在尝试不同的方法,但反复失败以获得解决方案。
答案 0 :(得分:5)
这些对我来说就像设置操作:联合,重叠和差异。
看看这个:
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
答案 1 :(得分:0)
我还没有测试过这段代码,但你可以试试这个:
for(CustomStation user1 : localStationLists)
{
boolean flag = false;
for(CustomStation user2 : serverStationLists)
{
if(user1.getStationId().equals(user2.getStationId()))
{
if(!user1.getTitle().equals(user2.getTitle()))
{
resultList.add(user1);
}
resultList.add(user2);
flag = true;
break;
}
}
if(flag == false)
{
resultList1.add(user1);
}
}