Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - best way to compare two List<List<String>>?

Tags:

java

list

compare

What's the best way to compare the String elements of two List<List<String>>...

At the end, I want to know if they contain the same elements (true) or not (false)

This two lists I want to compare:

ObservableList<List<String>> fnlData = FXCollections.observableList(new LinkedList<>());;

List<List<String>> fnlDataTMP = new LinkedList<>();

I searched for already answered questions in the forum, but nothing helped me ..

like image 359
InfernoVol Avatar asked Jul 29 '15 10:07

InfernoVol


People also ask

How do you compare two lists of strings in Java?

Java provides a method for comparing two Array List. The ArrayList. equals() is the method used for comparing two Array List. It compares the Array lists as, both Array lists should have the same size, and all corresponding pairs of elements in the two Array lists are equal.

How do I compare two lists in Java and get differences?

Using the Java List API. We can create a copy of one list and then remove all the elements common with the other using the List method removeAll(): List<String> differences = new ArrayList<>(listOne); differences. removeAll(listTwo); assertEquals(2, differences.

How do you check if two lists of strings are equal in Java?

You can compare two array lists using the equals() method of the ArrayList class, this method accepts a list object as a parameter, compares it with the current object, in case of the match it returns true and if not it returns false.

How do I compare two lists of strings?

Use sort() method and == operator to compare lists The sorted list and the == operator are used to compare the list, element by element.


2 Answers

Try fnlData.equals(fnlDataTMP) if both list are in order

or if order does not matter, try creating hash set and then compare using equals

new HashSet(fnlData).equals(new HashSet(fnlDataTMP))
like image 113
Nitesh Virani Avatar answered Oct 31 '22 02:10

Nitesh Virani


I don't think there is a way that let's you achieve that out of the box.

You can do something like the functional java List.join method to quickly generate 2 Lists and compare these:

List<String> joinedFnlData = jf.data.List.join(fnlData);
List<String> joinedFnlDataTMP = jf.data.List.join(fnlDataTMP);

CollectionUtils.isEqualCollection(joinedFnlData, joinedFnlDataTMP);

Things to note:

  • This is probably not the cheapest operation - so it should not be invoked too often in a time critical scenario (e.g. UI thread)
  • It does not do a "real" equals - for that you would have to do a nested loop like in the above answer. This checks that both joined lists have the same elements with the same cardinality: e.g. if fnlData has 2 lists with "1" and "2" as the only elements and fnlDataTMP has 1 list with "1", "2" as the elements, this would mark both as equal. Depending on your scenario this might be irrelevant - if this is relevant I don't see a way around nested loops.
like image 20
nutfox Avatar answered Oct 31 '22 03:10

nutfox