Compare List<String>

I have two List<String[]>'s (string array lists), and I'd like to match the contents with each other. Obviously == doesn't do the trick, but .equals () doesn't seem to do it either. So how should I check whether the contents of the string array lists are the same?

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

EDIT: Ok... for some reason only x.equals(y) works, and not y.equals(x). Odd...

Answers


Perhaps the easiest solution would be to use two List<List<String>>s instead. Assuming the List implementations used extend 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 have the same size, and all corresponding pairs of elements in the 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 a String[] in a thin List<String> implementation that extends AbstractList by using Arrays.asList.

EDIT: Here's 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

Need Your Help

Infinite while loop

python

Why is this while loop not ending when the proper input is entered (a number between 0 and 100)

How to write a cookbook with external cookbook_files in Chef?

chef

How do you create a general cookbook where you allow users to specify a location of a cookbook_file that will be copied? I'd imagine this file to be an overridable attribute.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.