Java 比较两个列表对象
我有两份清单如下:Java 比较两个列表对象,java,android,list,compare,contains,Java,Android,List,Compare,Contains,我有两份清单如下: List<Category> categories; List<Category> selectedCategories; 但我想比较两个列表: selectedCategories:[{id=3, catTitle='first', catId=17},{id=4, catTitle='second', catId=18}] categories: [{id=null, catTitle='first', catId=17} 得到{id=3,ca
List<Category> categories;
List<Category> selectedCategories;
但我想比较两个列表:
selectedCategories:[{id=3, catTitle='first', catId=17},{id=4, catTitle='second', catId=18}]
categories: [{id=null, catTitle='first', catId=17}
得到{id=3,catTitle='first',catId=17}
但是当id为null时,如何得到{id=3,catTitle='first',catId=17} 如果使用模型类,则需要手动匹配
但您将数据存储在json中,然后存储在
List<String> categories;
列出类别;
并将数据存储为Json.string()
,然后轻松匹配记录
pubic class Category {
Integer id;
int catId;
String catTitle;
..........................
@Override
public boolean equals(Object other) {
return (other instanceOf Category) && equate((Category) other);
}
private boolean equate(Category other) {
return other != null &&
catId == other.catId &&
equateIds(id, other.id) &&
equateTitles (catTitle, other.catTitle);
}
// Ids are considered equal, if equal, or at least one is null
private static bool equateIds(Integer id1, Integer id2) {
return (id1==null) || (id2==null) ||
id1.intValue() == id2.intValue();
}
// Titles are considered equal, if equal, or both null
private static bool equateTitles(String title1, Integer title2) {
return (title1==null) ? (title2 == null) : title1.equals(title2);
}
}
更新:为了保持一致性,您还需要hashCode忽略id:
@Override
public int hashCode() {
return catId + ((catTitle == null) ? 0 : catTitle.hashCode());
}
要获取公共元素,请执行以下操作:
List<Category> selectedElements = (new List<Category>(originalList)).retainAll(lookForList);
List selectedElements=(新列表(原始列表)).retainal(查找列表);
我是用这种方法做这件事的,希望这能对你有所帮助
for (Questions questionsListx : questionsList) {
// Loop arrayList1 items
boolean found = false;
for (Questions answeredlistx : answeredQuestionList) {
if (answeredlistx.getIdQuestion() == questionsListx.getIdQuestion()) {
found = true;
}
}
if (!found) {
unAnsweredQuestionList.add(questionsListx);
}
}
您可以使用Arrays.deepEquals()。为了正确使用它,您需要重写hashCode()和equals()。下面是示例代码
class Category {
Integer id;
int catId;
String catTitle;
public Category(Integer id, int catId, String catTitle) {
this.id = id;
this.catId = catId;
this.catTitle = catTitle;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + catId;
result = prime * result + ((catTitle == null) ? 0 : catTitle.hashCode());
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Category other = (Category) obj;
if (catId != other.catId)
return false;
if (catTitle == null) {
if (other.catTitle != null)
return false;
} else if (!catTitle.equals(other.catTitle))
return false;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
}
}
现在可以使用array.deepEquals()作为
List list1=Arrays.asList(新类别(11001,“第一”),新类别(21001,“第二”),
新类别(31001,第三)、新类别(41001,第四);
//与列表1的内容相同
list2=Arrays.asList(新类别(11001,“第一”),新类别(21001,“第二”),
新类别(31001,第三)、新类别(41001,第四);
//卡特尔的变化
listList3=Arrays.asList(新类别(1100,“list3first”)、新类别(21001,“list3second”)、新类别(31001,“list3third”)、新类别(41001,“list3”);
//仅当重写hashCode()和equals()时返回true,否则将返回false
System.out.println(“list1==list2:+Arrays.deepEquals(list1.toArray(),list2.toArray()));
System.out.println(“list1==list3:+Arrays.deepEquals(list1.toArray(),list3.toArray()));
输出
--------
list1==list2:true
list1==list3:false
当类别
具有id==null
时,是否需要selectCategories
列表中的第一项?是的。因为selectedCategories是从数据库加载的。除了id==null
之外,您是否有任何现有代码适用于其他情况?事实上,我希望在比较两个列表后获得公共对象?您认为呢是指普通人吗?在本例中,一个对象的id为null,但另一个对象的id为3。它们并不常见……您能解释一下在比较列表时使用这些方法吗?列表中的元素使用存在于任何对象中的equals()函数进行比较(请参见Java对象类的定义:)。通过覆盖'equals',即使id-s不完全匹配,我们也可以将项目视为相等。那么~“then you easy match record”是什么意思?你能在回答中给出一个代码说明吗。
class Category {
Integer id;
int catId;
String catTitle;
public Category(Integer id, int catId, String catTitle) {
this.id = id;
this.catId = catId;
this.catTitle = catTitle;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + catId;
result = prime * result + ((catTitle == null) ? 0 : catTitle.hashCode());
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Category other = (Category) obj;
if (catId != other.catId)
return false;
if (catTitle == null) {
if (other.catTitle != null)
return false;
} else if (!catTitle.equals(other.catTitle))
return false;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
}
}
List<Category> list1 = Arrays.asList(new Category(1, 1001, "first"), new Category(2, 1001, "second"),
new Category(3, 1001, "third"), new Category(4, 1001, "four"));
//same as content of list1
List<Category> list2 = Arrays.asList(new Category(1, 1001, "first"), new Category(2, 1001, "second"),
new Category(3, 1001, "third"), new Category(4, 1001, "four"));
//change in catTitle
List<Category> list3 = Arrays.asList(new Category(1, 100, "list3first"),new Category(2, 1001, "list3second"),new Category(3, 1001, "list3third"),new Category(4, 1001, "list3"));
//returns true only if you override hashCode() and equals() otherwise you will get false
System.out.println("list1==list2 : "+Arrays.deepEquals(list1.toArray(), list2.toArray()));
System.out.println("list1==list3 : "+Arrays.deepEquals(list1.toArray(), list3.toArray()));
output
--------
list1==list2 : true
list1==list3 : false