Unique set from ArrayList of ArrayList












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










share|improve this question


















  • 1





    Share your entire code. Also I think you need Set<ArrayList<String>> instead of Set<String>

    – Nicholas K
    Dec 31 '18 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.

    – tgdavies
    Dec 31 '18 at 10:11











  • Doesn't a HashSet only contain unique values like Hashmap?

    – SamzSakerz
    Dec 31 '18 at 10:12











  • Yes @SamzSakerz -- that's correct.

    – tgdavies
    Dec 31 '18 at 10:15











  • Oh, I forgot ArrayList<String> are unique Objects

    – SamzSakerz
    Dec 31 '18 at 10:17
















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










share|improve this question


















  • 1





    Share your entire code. Also I think you need Set<ArrayList<String>> instead of Set<String>

    – Nicholas K
    Dec 31 '18 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.

    – tgdavies
    Dec 31 '18 at 10:11











  • Doesn't a HashSet only contain unique values like Hashmap?

    – SamzSakerz
    Dec 31 '18 at 10:12











  • Yes @SamzSakerz -- that's correct.

    – tgdavies
    Dec 31 '18 at 10:15











  • Oh, I forgot ArrayList<String> are unique Objects

    – SamzSakerz
    Dec 31 '18 at 10:17














1












1








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










share|improve this question














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







java arraylist set






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Dec 31 '18 at 9:50









MarthaMartha

395




395








  • 1





    Share your entire code. Also I think you need Set<ArrayList<String>> instead of Set<String>

    – Nicholas K
    Dec 31 '18 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.

    – tgdavies
    Dec 31 '18 at 10:11











  • Doesn't a HashSet only contain unique values like Hashmap?

    – SamzSakerz
    Dec 31 '18 at 10:12











  • Yes @SamzSakerz -- that's correct.

    – tgdavies
    Dec 31 '18 at 10:15











  • Oh, I forgot ArrayList<String> are unique Objects

    – SamzSakerz
    Dec 31 '18 at 10:17














  • 1





    Share your entire code. Also I think you need Set<ArrayList<String>> instead of Set<String>

    – Nicholas K
    Dec 31 '18 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.

    – tgdavies
    Dec 31 '18 at 10:11











  • Doesn't a HashSet only contain unique values like Hashmap?

    – SamzSakerz
    Dec 31 '18 at 10:12











  • Yes @SamzSakerz -- that's correct.

    – tgdavies
    Dec 31 '18 at 10:15











  • Oh, I forgot ArrayList<String> are unique Objects

    – SamzSakerz
    Dec 31 '18 at 10:17








1




1





Share your entire code. Also I think you need Set<ArrayList<String>> instead of Set<String>

– Nicholas K
Dec 31 '18 at 9:55





Share your entire code. Also I think you need Set<ArrayList<String>> instead of Set<String>

– Nicholas K
Dec 31 '18 at 9:55




1




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.

– tgdavies
Dec 31 '18 at 10:11





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.

– tgdavies
Dec 31 '18 at 10:11













Doesn't a HashSet only contain unique values like Hashmap?

– SamzSakerz
Dec 31 '18 at 10:12





Doesn't a HashSet only contain unique values like Hashmap?

– SamzSakerz
Dec 31 '18 at 10:12













Yes @SamzSakerz -- that's correct.

– tgdavies
Dec 31 '18 at 10:15





Yes @SamzSakerz -- that's correct.

– tgdavies
Dec 31 '18 at 10:15













Oh, I forgot ArrayList<String> are unique Objects

– SamzSakerz
Dec 31 '18 at 10:17





Oh, I forgot ArrayList<String> are unique Objects

– SamzSakerz
Dec 31 '18 at 10:17












5 Answers
5






active

oldest

votes


















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





share|improve this answer





















  • 2





    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.

    – tgdavies
    Dec 31 '18 at 10:17



















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.






share|improve this answer

































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





    share|improve this answer

































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


      }





      share|improve this answer
























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

        – Ajay Kumar
        Dec 31 '18 at 10:51



















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





      share|improve this answer


























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

        – Nicholas K
        Dec 31 '18 at 10:43











      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53986002%2funique-set-from-arraylist-of-arraylist%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      5 Answers
      5






      active

      oldest

      votes








      5 Answers
      5






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









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





      share|improve this answer





















      • 2





        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.

        – tgdavies
        Dec 31 '18 at 10:17
















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





      share|improve this answer





















      • 2





        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.

        – tgdavies
        Dec 31 '18 at 10:17














      2












      2








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





      share|improve this answer















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






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Dec 31 '18 at 10:43









      Nicholas K

      6,89761133




      6,89761133










      answered Dec 31 '18 at 10:05









      m fauzan abdim fauzan abdi

      34129




      34129








      • 2





        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.

        – tgdavies
        Dec 31 '18 at 10:17














      • 2





        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.

        – tgdavies
        Dec 31 '18 at 10:17








      2




      2





      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.

      – tgdavies
      Dec 31 '18 at 10:17





      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.

      – tgdavies
      Dec 31 '18 at 10:17













      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.






      share|improve this answer






























        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.






        share|improve this answer




























          2












          2








          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.






          share|improve this answer















          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.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Dec 31 '18 at 10:52

























          answered Dec 31 '18 at 10:44









          jbxjbx

          11.3k1058111




          11.3k1058111























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





              share|improve this answer






























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





                share|improve this answer




























                  1












                  1








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





                  share|improve this answer















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






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Dec 31 '18 at 14:07

























                  answered Dec 31 '18 at 10:38









                  Nicholas KNicholas K

                  6,89761133




                  6,89761133























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


                      }





                      share|improve this answer
























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

                        – Ajay Kumar
                        Dec 31 '18 at 10:51
















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


                      }





                      share|improve this answer
























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

                        – Ajay Kumar
                        Dec 31 '18 at 10:51














                      0












                      0








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


                      }





                      share|improve this answer













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


                      }






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Dec 31 '18 at 10:49









                      Ajay KumarAjay Kumar

                      12




                      12













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

                        – Ajay Kumar
                        Dec 31 '18 at 10:51



















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

                        – Ajay Kumar
                        Dec 31 '18 at 10:51

















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

                      – Ajay Kumar
                      Dec 31 '18 at 10:51





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

                      – Ajay Kumar
                      Dec 31 '18 at 10:51











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





                      share|improve this answer


























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

                        – Nicholas K
                        Dec 31 '18 at 10:43
















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





                      share|improve this answer


























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

                        – Nicholas K
                        Dec 31 '18 at 10:43














                      0












                      0








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





                      share|improve this answer















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






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Dec 31 '18 at 11:39

























                      answered Dec 31 '18 at 10:15









                      ZhaoGangZhaoGang

                      1,9641116




                      1,9641116













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

                        – Nicholas K
                        Dec 31 '18 at 10:43



















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

                        – Nicholas K
                        Dec 31 '18 at 10:43

















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

                      – Nicholas K
                      Dec 31 '18 at 10:43





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

                      – Nicholas K
                      Dec 31 '18 at 10:43


















                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53986002%2funique-set-from-arraylist-of-arraylist%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Monofisismo

                      Angular Downloading a file using contenturl with Basic Authentication

                      Olmecas