Home > Software engineering >  How to find duplicates in a list of list of objects in java
How to find duplicates in a list of list of objects in java

Time:12-01

Let's say I have a data in an Array List of objects compositeKeyValues = [["READ", "3GPP_ACCESS"], ["CREATE", "NON_3GPP"], ["READ", "3GPP_ACCESS"], ["MODIFY", "NON_3GPP"]]

List<Object> compositeKeyValues = new ArrayList<>();

I want to identify duplicate list inside that outer list, which contains identical values, like at index 0 and 2 the lists are identical. How can i identify something like that within the list?

CodePudding user response:

Actually, you have:

List<List<String>> compositeKeyValues;

Lists are equal if they have the same elements in the same order - like your example.

Finding duplicate inner Lists is no different finding duplicates of other simpler types.

Here's one way:

List<List<String>> duplicates = compositeKeyValues.stream()
    .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))
    .entrySet().stream()
    .filter(e -> e.getValue().intValue() > 1)
    .map(Map.Entry::getKey)
    .collect(Collectors.toList());

This code will work even if you leave the type of the List as List<Object>, except the result would also have type List<Object>. However, it's recommended, and more useful, to use a more specific type List<List<String>>.

CodePudding user response:

I would start by not making the list of generic Object instances because this will make it so much more difficult to accomplish. Make the list of objects to be of a specific class instead. Then, your custom class must override Object's equal() and hashCode() methods.

The question is now, how is the easiest way to accomplish the actual removal of duplicates. The simple answer is to convert your List to a Set. I think something like this should work.

Set<SomeType> mySet = myList.findByType(type)
        .stream()
        .map(ClassName::getValue)
        .collect(Collectors.toSet());

Keep in mind that the methods I mentioned will still need to be overridden. Most, if not all, modern IDEs have convenience methods to override equals() and hashCode()` methods.

CodePudding user response:

java.util.List<String> list = Arrays.asList("READ", "3GPP_ACCESS","CREATE", "NON_3GPP","READ", "3GPP_ACCESS","MODIFY", "NON_3GPP");
        for (int i = 0; i < list.size(); i  ) {
            for (int j = i 1; j <list.size() ; j  ) {
                if(list.get(i).equals(list.get(j))){
                    System.out.println(list.get(i));
                }
            }
        }

CodePudding user response:

I wanted to contribute an Array based solution even if the original question was about lists.

import java.util.Arrays;

public class Main {

    public static void main(String[] args) {

        String[][] data = { { "READ", "3GPP_ACCESS" }, { "CREATE", "NON_3GPP" }, { "READ", "3GPP_ACCESS" },
                { "MODIFY", "NON_3GPP" } };

        for (int i = 0; i < data.length; i  )
            for (int j = i   1; j < data.length; j  ) {// should not compare with itself and anything previously else
                                                        // there will be double findings like 0,2 and 2,0

                if (Arrays.equals(data[i], data[j])) // check for equality

                    System.out.println("Match found at "   i   " "   Arrays.toString(data[i])   " and "   j   " "
                              Arrays.toString(data[j]));

            }
    }

}
  • Related