问题
I want to compare two ArrayList of string arrays.
List<String[]> list1 = new ArrayList<String[]>;
List<String[]> list2 = new ArrayList<String[]>;
list1.equals(list2);
This will return false because equals method in ArrayList will do equals on the element.
ListIterator<E> e1 = listIterator();
ListIterator<?> e2 = ((List<?>) o).listIterator();
while (e1.hasNext() && e2.hasNext()) {
E o1 = e1.next();
Object o2 = e2.next();
if (!(o1==null ? o2==null : o1.equals(o2)))
return false;
}
return !(e1.hasNext() || e2.hasNext());
If you do equals on array, it will check reference equality. Is there anyway we can use list1.equals(list2) instead of checking each element in array list.
回答1:
You can not do it by using equals. Instead of doing equals, you can do retainAll(). You can just write a small function, say isEqual() and use this where you are using equals. You need to convert your array
as list and pass it to this function.
{
ListIterator<E> e1 = listIterator();
ListIterator<?> e2 = ((List<?>) o).listIterator();
while (e1.hasNext() && e2.hasNext()) {
E o1 = e1.next();
Object o2 = e2.next();
if (!(o1==null ? o2==null : isEqual(Arrays.alList(o1),Arrays.asList(o2))))
return false;
}
return !(e1.hasNext() || e2.hasNext());
}
boolean isEqual(List list1, List list2){
int originalSize = list1.size();
list1.retainAll(list2);
// list1 will retain all the elements that are present in list2
// if list1 has all the elements that are present in list2, present list1 size will be equal to `original size`
if(list1.size() == originlaSize){
returns true;
}else{
return false;
}
}
回答2:
You can use list1.equals(list2) if you change from List<String[]>
to List<List<String>>
.
public static void main(String[] args) throws Exception {
List<List<String>> list1 = new ArrayList() {{
add(new ArrayList(Arrays.asList("a", "b", "c")));
add(new ArrayList(Arrays.asList("d", "e", "f")));
add(new ArrayList(Arrays.asList("g", "h", "i")));
}};
List<List<String>> list2 = new ArrayList() {{
add(new ArrayList(Arrays.asList("a", "b", "c")));
add(new ArrayList(Arrays.asList("d", "e", "f")));
add(new ArrayList(Arrays.asList("g", "h", "i")));
}};
System.out.println(list1);
System.out.println(list2);
System.out.println(list1.equals(list2));
}
Results:
[[a, b, c], [d, e, f], [g, h, i]]
[[a, b, c], [d, e, f], [g, h, i]]
true
Otherwise, you're probably looking something along the lines of:
public static void main(String[] args) throws Exception {
List<String[]> list1 = new ArrayList() {{
add(new String[] {"a", "b", "c"});
add(new String[] {"d", "e", "f"});
add(new String[] {"g", "h", "i"});
}};
List<String[]> list2 = new ArrayList() {{
add(new String[] {"a", "b", "c"});
add(new String[] {"d", "e", "f"});
add(new String[] {"g", "h", "i"});
}};
System.out.println(listsEqual(list1, list2));
}
public static boolean listsEqual(List<String[]> list1, List<String[]> list2) {
if (list1.size() != list2.size()) {
return false;
}
for (int i = 0; i < list1.size(); i++) {
if (!Arrays.equals(list1.get(i), list2.get(i))){
return false;
}
}
return true;
}
Results:
true
回答3:
Make a simple wrapper for the array class.
private String[] array;
public ArrayWrapper(String[] array) {
this.array = array;
}
Override equals(Object obj)
to use Arrays.equals(array, (String[]) obj)
and just hashCode()
is just array.hashCode()
.
来源:https://stackoverflow.com/questions/31644416/comparing-array-list-of-string-array