1

Hi I have an arraylist of arraylist in this format:

[[val1, val2],[val3,val4],[val1,val2],[val1,val5]]

and would like to get the unique set of arraylists:

[[val1, val2],[val3,val4],[val1,val5]]

I have tried the following:

Set<String> uniques = new HashSet<>();
    for (ArrayList<String> sublist : mappedEntities) {
        uniques.addAll(sublist);
    }

but this merges all the values of the internal arraylist together

5
  • 1
    Share your entire code. Also I think you need Set<ArrayList<String>> instead of Set<String> Commented Dec 31, 2018 at 9:55
  • 1
    As @NicholasK says, the type of uniques needs to be Set<List<String>>. The addAll function takes a Collection and adds each member of the collection to the Set individually -- you just want to add the entire collection as a single item. Commented Dec 31, 2018 at 10:11
  • Doesn't a HashSet only contain unique values like Hashmap? Commented Dec 31, 2018 at 10:12
  • Yes @SamzSakerz -- that's correct. Commented Dec 31, 2018 at 10:15
  • Oh, I forgot ArrayList<String> are unique Objects Commented Dec 31, 2018 at 10:17

5 Answers 5

2

can use Java 8 Collection Stream Distinct,
return in Set datatype :

Set<List<String>> uniques = mappedEntities.stream().distinct().collect(Collectors.toSet());

if you want return in List :

List<List<String>> uniques = mappedEntities.stream().distinct().collect(Collectors.toList());
Sign up to request clarification or add additional context in comments.

1 Comment

While this does filter duplicates, it is an expensive way to do it, as distinct() creates a HashSet and we then create a List. Also we should be producing a data structure which will retain that uniqueness if we add further items to it -- we should create a Set, not a List. Finally I think an answer which uses a completely different concept is less helpful to the OP than one which explains the misconception in their current code.
2

Why not simply put them in a Set like this?

Set<List<String>> uniques = new HashSet<>(mappedEntities);

Your mistake is that you are flattening the inner lists and putting their items in the set separately.

Comments

1

The issue here is that you need a Set of ArrayList Set<ArrayList<String>>, but you are using a Set of Strings Set<String> instead.

Given the list :

List<List<String>> mappedEntities = Arrays.asList(Arrays.asList("val1", "val2"), 
                                                  Arrays.asList("val3", "val4"),
                                                  Arrays.asList("val1", "val2"), 
                                                  Arrays.asList("val1", "val5"));

All you need to do is just declare the set and use the addAll().

Set<List<String>> mySet = new HashSet<>();
mySet.addAll(mappedEntities);

Since a set can hold only unique values, all duplicates will not be added to the set (No need to explicitly check this). You can now print it out :

mySet.forEach(System.out::println);

Or more simply, initialize the HashSet using the list mappedEntities :

Set<List<String>> mySet = new HashSet<>(mappedEntities);

Comments

0

I am beginner on STACKOVERFLOW but i to try solve your problem
I think you want like this..

import java.util.*; 
public class GFG { 
    public static void main(String[] args) 
    { 
        int n = 3; 

        // Here aList is an ArrayList of ArrayLists 
        ArrayList<ArrayList<String> > aList = 
                new ArrayList<ArrayList<String> >(n); 

        // Create n lists one by one and append to the 
        // master list (ArrayList of ArrayList) 
        ArrayList<String> a1 = new ArrayList<String>(); 
        a1.add("1"); 
        a1.add("2"); 
        aList.add(a1); 

        ArrayList<String> a2 = new ArrayList<String>(); 
        a2.add("11"); 
        a2.add("22"); 
        aList.add(a2); 

        ArrayList<String> a3 = new ArrayList<String>(); 
        a3.add("1"); 
        a3.add("2"); 
        aList.add(a3); 


        Set<ArrayList<String>> uniques = new HashSet<ArrayList<String>>();
        for (ArrayList<String> sublist : aList) {
            uniques.add(sublist);
        }
        System.out.println("Your Answer"); 
        for (ArrayList<String> x : uniques) 
            System.out.println(x); 
    }


    } 

1 Comment

Output: Your Answer [11, 22] [1, 2]
0

try this code:

public class B {
    public static void main(String[] args) throws Exception {
       List<List<String>> list= Arrays.asList(
               Arrays.asList("a","b","c"),
               Arrays.asList("a","b","c"),
               Arrays.asList("a","b","c","d"));

       Set<List<String>> uniques = new HashSet<>();
       for (List<String> sublist : list) {
          if(!uniques.contains(sublist))
          uniques.add(sublist);
       }
       System.out.println(uniques);
    }
}

output:

[[a, b, c], [a, b, c, d]]

1 Comment

Since it is a set you wouldn't explicitly need to check for duplicates. This line is not needed if (!uniques.contains(sublist))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.