问题
I have two list as below:
List<Category> categories;
List<Category> selectedCategories;
and model of Category:
(id,catTitle,catId)
but I want to compare two list when:
selectedCategories:[{id=3, catTitle='first', catId=17},{id=4, catTitle='second', catId=18}]
categories: [{id=null, catTitle='first', catId=17}
and get {id=3,catTitle='first',catId=17}
but when id is null how to have {id=3,catTitle='first',catId=17} as result?!!!
回答1:
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);
}
}
Update: for consistency you also need hashCode ignoring id:
@Override
public int hashCode() {
return catId + ((catTitle == null) ? 0 : catTitle.hashCode());
}
To get common elements:
List<Category> selectedElements = (new List<Category>(originalList)).retainAll(lookForList);
回答2:
If you Use Model class then you need to match manually
but you store you data in json and then store in
List<String> categories;
and store data as Json.string()
then you easy match record..
回答3:
I did this using this method Hope this will help you.!
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);
}
}
回答4:
you can use Arrays.deepEquals(). in order to do properly use it you need to override hashCode() and equals(). here is sample code.
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;
}
}
now you can use Arrays.deepEquals() as
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
来源:https://stackoverflow.com/questions/41437771/compare-two-list-objects