Comparison List <String []>

I have two List<String[]> (string arrays) and I would like to combine the contents with each other. Obviously == does not do the trick, but .equals () does not do this either. So, how do I check if the contents of lists of string arrays are the same?

By the way, in both cases, I got an exception with a null message.

EDIT: Okay ... for some reason, only x.equals(y) works, not y.equals(x) . Strange ...

+6
source share
5 answers

It’s just that .equals() works fine, but somehow only in one direction.

I was going to accept the first answer to this question, but apparently the guy deleted his answer.

0
source

Perhaps the easiest solution would be to use two List<List<String>> . Assuming the List implementations used are extended by AbstractList , using equals will give you the desired behavior. From the documentation for AbstractList.equals :

Compares the specified object with this list for equality. Returns true if and only if the specified object is also a list, both lists are the same size, and all the corresponding pairs of elements in two lists are equal. (Two elements e1 and e2 are equal if (e1==null ? e2==null : e1.equals(e2)) .) In other words, two lists are defined to be equal if they contain the same elements in the same order .

You can easily wrap String[] in a thin List<String> implementation that extends AbstractList using Arrays.asList .

EDIT: Here is an example:

 String[] array1 = {"1", "2", "3"}; String[] array2 = {"4", "7"}; String[] array3 = {"1", "2", "3"}; String[] array4 = {"4", "7"}; List<List<String>> lst1 = new ArrayList<>(); lst1.add(Arrays.asList(array1)); lst1.add(Arrays.asList(array2)); List<List<String>> lst2 = new ArrayList<>(); lst2.add(Arrays.asList(array3)); lst2.add(Arrays.asList(array4)); System.out.println(lst1.equals(lst2)); //prints true 
+5
source

You should usually avoid using arrays. they are ugly and lead to such problems. If possible, use List<List<String>> , then you can usually use .equals() .

if you insist, you can use the usual equal implementation, as shown below. the key is to use Arrays.equals()

 public class DemoEquals { List<String[]> listOne = (List<String[]>) Arrays.asList(new String[]{"one1", "one2"}, new String[]{"two1"}); List<String[]> listOneOne = (List<String[]>) Arrays.asList(new String[]{"one1", "one2"}, new String[]{"two1"}); List<String[]> listTwo = (List<String[]>) Arrays.asList(new String[]{"2one1", "2one2"}, new String[]{"2two1"}); private boolean isEqual(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; } @SuppressWarnings("unchecked") private void isEqual() { //prints true System.out.println(isEqual(Collections.EMPTY_LIST, Collections.EMPTY_LIST)); //prints true System.out.println(isEqual(listOne, listOne)); //prints true System.out.println(isEqual(listOne, listOneOne)); //prints false System.out.println(isEqual(listOne, listTwo)); //prints false System.out.println(isEqual(listOne, Collections.EMPTY_LIST)); } public static void main(String[] args) { new DemoEquals().isEqual(); } } 
+3
source

You can use INTERFACE Comparable. Enter the required method and you can use the List1.compare (List2) function.

0
source

Using Jon Skeet's answer on how to convert a list to a string (using Guava), another option is to convert 2 lists to 2 strings, and then compare using equals . No cycles.

-1
source

Source: https://habr.com/ru/post/924874/


All Articles