Martha
Martha

Reputation: 111

Unique set from ArrayList of ArrayList

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

Upvotes: 1

Views: 326

Answers (5)

Nicholas K
Nicholas K

Reputation: 15423

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);

Upvotes: 1

ZhaoGang
ZhaoGang

Reputation: 4915

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]]

Upvotes: 0

jbx
jbx

Reputation: 22148

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.

Upvotes: 2

Ajay Kumar
Ajay Kumar

Reputation: 5

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); 
    }


    } 

Upvotes: 0

m fauzan abdi
m fauzan abdi

Reputation: 436

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());

Upvotes: 2

Related Questions