How check if the lists in a list are not empty

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
7
down vote

favorite












I have a list of lists in Java. Here is the code:



List<List<Integer>> myList = new ArrayList<>();
myList.add(new ArrayList<Integer>());
myList.add(new ArrayList<Integer>());
myList.add(new ArrayList<Integer>());

myList.get(0).add(1);
myList.get(0).add(2);
myList.get(0).add(3);
myList.get(1).add(4);
myList.get(1).add(5);
myList.get(1).add(6);
myList.get(2).add(7);
myList.get(2).add(8);
myList.get(2).add(9);


Now in a part of my code I want to check if all three lists that are located in list are not empty. Should I check each of these lists one by one like if (myList.get(0) != null && !myList.get(0).isEmpty()) do something or is there a better and shorter way to do instead of checking one by one?










share|improve this question



























    up vote
    7
    down vote

    favorite












    I have a list of lists in Java. Here is the code:



    List<List<Integer>> myList = new ArrayList<>();
    myList.add(new ArrayList<Integer>());
    myList.add(new ArrayList<Integer>());
    myList.add(new ArrayList<Integer>());

    myList.get(0).add(1);
    myList.get(0).add(2);
    myList.get(0).add(3);
    myList.get(1).add(4);
    myList.get(1).add(5);
    myList.get(1).add(6);
    myList.get(2).add(7);
    myList.get(2).add(8);
    myList.get(2).add(9);


    Now in a part of my code I want to check if all three lists that are located in list are not empty. Should I check each of these lists one by one like if (myList.get(0) != null && !myList.get(0).isEmpty()) do something or is there a better and shorter way to do instead of checking one by one?










    share|improve this question

























      up vote
      7
      down vote

      favorite









      up vote
      7
      down vote

      favorite











      I have a list of lists in Java. Here is the code:



      List<List<Integer>> myList = new ArrayList<>();
      myList.add(new ArrayList<Integer>());
      myList.add(new ArrayList<Integer>());
      myList.add(new ArrayList<Integer>());

      myList.get(0).add(1);
      myList.get(0).add(2);
      myList.get(0).add(3);
      myList.get(1).add(4);
      myList.get(1).add(5);
      myList.get(1).add(6);
      myList.get(2).add(7);
      myList.get(2).add(8);
      myList.get(2).add(9);


      Now in a part of my code I want to check if all three lists that are located in list are not empty. Should I check each of these lists one by one like if (myList.get(0) != null && !myList.get(0).isEmpty()) do something or is there a better and shorter way to do instead of checking one by one?










      share|improve this question















      I have a list of lists in Java. Here is the code:



      List<List<Integer>> myList = new ArrayList<>();
      myList.add(new ArrayList<Integer>());
      myList.add(new ArrayList<Integer>());
      myList.add(new ArrayList<Integer>());

      myList.get(0).add(1);
      myList.get(0).add(2);
      myList.get(0).add(3);
      myList.get(1).add(4);
      myList.get(1).add(5);
      myList.get(1).add(6);
      myList.get(2).add(7);
      myList.get(2).add(8);
      myList.get(2).add(9);


      Now in a part of my code I want to check if all three lists that are located in list are not empty. Should I check each of these lists one by one like if (myList.get(0) != null && !myList.get(0).isEmpty()) do something or is there a better and shorter way to do instead of checking one by one?







      java list






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 41 mins ago

























      asked 51 mins ago









      helen

      173111




      173111






















          5 Answers
          5






          active

          oldest

          votes

















          up vote
          7
          down vote













          You can use stream api for this, but also a plain loop too:



           boolean allNonEmptyOrNull = myList.stream()
          .allMatch(x -> x != null && !x.isEmpty());


          Or you can check if null is contained or an an empty List for example, via:



          System.out.println(myList.contains(null) || myList.contains(Collections.<Integer> emptyList()));


          But this last option will break with java-9 immutable collections, for example:



          List.of(1, 2, 3).contains(null); 


          will throw a NullPointerException






          share|improve this answer






















          • Are you sure contains(null) will throw an error?
            – shmosel
            18 mins ago










          • @shmosel yes, I do
            – Eugene
            14 mins ago










          • That's very surprising. Awful design decision, if you ask me.
            – shmosel
            5 mins ago

















          up vote
          3
          down vote













          Just check your collection not contains empty list



          if (!L.contains(Collections.EMPTY_LIST)) do something 


          or empty and null check (be care with NullPointerException !!!)



          if (!L.contains(Collections.EMPTY_LIST) && !L.contains(null)) do something 





          share|improve this answer










          New contributor




          Mark is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.

















          • almost correct, you have to check against null too
            – Eugene
            38 mins ago










          • Q was "are not empty". But check for null not a problem too.
            – Mark
            31 mins ago










          • you are right, but the code in the question was myList.get(0) != null...
            – Eugene
            31 mins ago










          • and now that you have written it - it might fail if the main list is an immutable list, like java-9 has (did not downvote btw)
            – Eugene
            26 mins ago











          • you are right, thank!
            – Mark
            20 mins ago

















          up vote
          1
          down vote













          int temp = 0;
          for(int i=0;i<L.size();i++)
          if (L.get(i).isEmpty())
          temp++;


          if (temp == L.size())
          //do what you want, all the lists inside L are empty



          This is all i can think of right now :)






          share|improve this answer



























            up vote
            1
            down vote













            You can do something like this:



            boolean isEmpty = false;
            for(list in myList)

            if(!isEmpty) // do your thing;





            share|improve this answer






















            • Why the downvote?
              – Pritam Banerjee
              30 mins ago

















            up vote
            1
            down vote













            Using Java 7 and below this is the classical way to approach that:



            for (List<Integer> list : myList) 
            if (list != null && !list.isEmpty())
            // do something with not empty list




            With Java 8 and above you can use forEach:



            myList.forEach(list -> 
            if (list != null && !list.isEmpty())
            // do something with not empty list

            );


            or, as already mentioned by Eugene add stream API touch to it:



            myList.stream()
            .filter(list -> (list != null && !list.isEmpty()))
            .forEach(list ->
            // do something with not empty list
            );


            Note: all these 3 examples imply that you have initialized myList variable and it is not null, otherwise NullPointerException will be thrown in all snippets above.



            Standard JDK does not have a quick way to check that collection is not null and not empty. But if you are using Apache commons-collections library, they offer such a method: CollectionUtils.isNotEmpty(). However, I would not recommend, to add this dependency into your project just for the sake of this single function.






            share|improve this answer






















              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',
              convertImagesToLinks: true,
              noModals: false,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              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%2f52768755%2fhow-check-if-the-lists-in-a-list-are-not-empty%23new-answer', 'question_page');

              );

              Post as a guest






























              5 Answers
              5






              active

              oldest

              votes








              5 Answers
              5






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              7
              down vote













              You can use stream api for this, but also a plain loop too:



               boolean allNonEmptyOrNull = myList.stream()
              .allMatch(x -> x != null && !x.isEmpty());


              Or you can check if null is contained or an an empty List for example, via:



              System.out.println(myList.contains(null) || myList.contains(Collections.<Integer> emptyList()));


              But this last option will break with java-9 immutable collections, for example:



              List.of(1, 2, 3).contains(null); 


              will throw a NullPointerException






              share|improve this answer






















              • Are you sure contains(null) will throw an error?
                – shmosel
                18 mins ago










              • @shmosel yes, I do
                – Eugene
                14 mins ago










              • That's very surprising. Awful design decision, if you ask me.
                – shmosel
                5 mins ago














              up vote
              7
              down vote













              You can use stream api for this, but also a plain loop too:



               boolean allNonEmptyOrNull = myList.stream()
              .allMatch(x -> x != null && !x.isEmpty());


              Or you can check if null is contained or an an empty List for example, via:



              System.out.println(myList.contains(null) || myList.contains(Collections.<Integer> emptyList()));


              But this last option will break with java-9 immutable collections, for example:



              List.of(1, 2, 3).contains(null); 


              will throw a NullPointerException






              share|improve this answer






















              • Are you sure contains(null) will throw an error?
                – shmosel
                18 mins ago










              • @shmosel yes, I do
                – Eugene
                14 mins ago










              • That's very surprising. Awful design decision, if you ask me.
                – shmosel
                5 mins ago












              up vote
              7
              down vote










              up vote
              7
              down vote









              You can use stream api for this, but also a plain loop too:



               boolean allNonEmptyOrNull = myList.stream()
              .allMatch(x -> x != null && !x.isEmpty());


              Or you can check if null is contained or an an empty List for example, via:



              System.out.println(myList.contains(null) || myList.contains(Collections.<Integer> emptyList()));


              But this last option will break with java-9 immutable collections, for example:



              List.of(1, 2, 3).contains(null); 


              will throw a NullPointerException






              share|improve this answer














              You can use stream api for this, but also a plain loop too:



               boolean allNonEmptyOrNull = myList.stream()
              .allMatch(x -> x != null && !x.isEmpty());


              Or you can check if null is contained or an an empty List for example, via:



              System.out.println(myList.contains(null) || myList.contains(Collections.<Integer> emptyList()));


              But this last option will break with java-9 immutable collections, for example:



              List.of(1, 2, 3).contains(null); 


              will throw a NullPointerException







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited 36 mins ago

























              answered 49 mins ago









              Eugene

              63.3k987146




              63.3k987146











              • Are you sure contains(null) will throw an error?
                – shmosel
                18 mins ago










              • @shmosel yes, I do
                – Eugene
                14 mins ago










              • That's very surprising. Awful design decision, if you ask me.
                – shmosel
                5 mins ago
















              • Are you sure contains(null) will throw an error?
                – shmosel
                18 mins ago










              • @shmosel yes, I do
                – Eugene
                14 mins ago










              • That's very surprising. Awful design decision, if you ask me.
                – shmosel
                5 mins ago















              Are you sure contains(null) will throw an error?
              – shmosel
              18 mins ago




              Are you sure contains(null) will throw an error?
              – shmosel
              18 mins ago












              @shmosel yes, I do
              – Eugene
              14 mins ago




              @shmosel yes, I do
              – Eugene
              14 mins ago












              That's very surprising. Awful design decision, if you ask me.
              – shmosel
              5 mins ago




              That's very surprising. Awful design decision, if you ask me.
              – shmosel
              5 mins ago












              up vote
              3
              down vote













              Just check your collection not contains empty list



              if (!L.contains(Collections.EMPTY_LIST)) do something 


              or empty and null check (be care with NullPointerException !!!)



              if (!L.contains(Collections.EMPTY_LIST) && !L.contains(null)) do something 





              share|improve this answer










              New contributor




              Mark is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.

















              • almost correct, you have to check against null too
                – Eugene
                38 mins ago










              • Q was "are not empty". But check for null not a problem too.
                – Mark
                31 mins ago










              • you are right, but the code in the question was myList.get(0) != null...
                – Eugene
                31 mins ago










              • and now that you have written it - it might fail if the main list is an immutable list, like java-9 has (did not downvote btw)
                – Eugene
                26 mins ago











              • you are right, thank!
                – Mark
                20 mins ago














              up vote
              3
              down vote













              Just check your collection not contains empty list



              if (!L.contains(Collections.EMPTY_LIST)) do something 


              or empty and null check (be care with NullPointerException !!!)



              if (!L.contains(Collections.EMPTY_LIST) && !L.contains(null)) do something 





              share|improve this answer










              New contributor




              Mark is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.

















              • almost correct, you have to check against null too
                – Eugene
                38 mins ago










              • Q was "are not empty". But check for null not a problem too.
                – Mark
                31 mins ago










              • you are right, but the code in the question was myList.get(0) != null...
                – Eugene
                31 mins ago










              • and now that you have written it - it might fail if the main list is an immutable list, like java-9 has (did not downvote btw)
                – Eugene
                26 mins ago











              • you are right, thank!
                – Mark
                20 mins ago












              up vote
              3
              down vote










              up vote
              3
              down vote









              Just check your collection not contains empty list



              if (!L.contains(Collections.EMPTY_LIST)) do something 


              or empty and null check (be care with NullPointerException !!!)



              if (!L.contains(Collections.EMPTY_LIST) && !L.contains(null)) do something 





              share|improve this answer










              New contributor




              Mark is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.









              Just check your collection not contains empty list



              if (!L.contains(Collections.EMPTY_LIST)) do something 


              or empty and null check (be care with NullPointerException !!!)



              if (!L.contains(Collections.EMPTY_LIST) && !L.contains(null)) do something 






              share|improve this answer










              New contributor




              Mark is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.









              share|improve this answer



              share|improve this answer








              edited 10 mins ago





















              New contributor




              Mark is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.









              answered 45 mins ago









              Mark

              1542




              1542




              New contributor




              Mark is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.





              New contributor





              Mark is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.






              Mark is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.











              • almost correct, you have to check against null too
                – Eugene
                38 mins ago










              • Q was "are not empty". But check for null not a problem too.
                – Mark
                31 mins ago










              • you are right, but the code in the question was myList.get(0) != null...
                – Eugene
                31 mins ago










              • and now that you have written it - it might fail if the main list is an immutable list, like java-9 has (did not downvote btw)
                – Eugene
                26 mins ago











              • you are right, thank!
                – Mark
                20 mins ago
















              • almost correct, you have to check against null too
                – Eugene
                38 mins ago










              • Q was "are not empty". But check for null not a problem too.
                – Mark
                31 mins ago










              • you are right, but the code in the question was myList.get(0) != null...
                – Eugene
                31 mins ago










              • and now that you have written it - it might fail if the main list is an immutable list, like java-9 has (did not downvote btw)
                – Eugene
                26 mins ago











              • you are right, thank!
                – Mark
                20 mins ago















              almost correct, you have to check against null too
              – Eugene
              38 mins ago




              almost correct, you have to check against null too
              – Eugene
              38 mins ago












              Q was "are not empty". But check for null not a problem too.
              – Mark
              31 mins ago




              Q was "are not empty". But check for null not a problem too.
              – Mark
              31 mins ago












              you are right, but the code in the question was myList.get(0) != null...
              – Eugene
              31 mins ago




              you are right, but the code in the question was myList.get(0) != null...
              – Eugene
              31 mins ago












              and now that you have written it - it might fail if the main list is an immutable list, like java-9 has (did not downvote btw)
              – Eugene
              26 mins ago





              and now that you have written it - it might fail if the main list is an immutable list, like java-9 has (did not downvote btw)
              – Eugene
              26 mins ago













              you are right, thank!
              – Mark
              20 mins ago




              you are right, thank!
              – Mark
              20 mins ago










              up vote
              1
              down vote













              int temp = 0;
              for(int i=0;i<L.size();i++)
              if (L.get(i).isEmpty())
              temp++;


              if (temp == L.size())
              //do what you want, all the lists inside L are empty



              This is all i can think of right now :)






              share|improve this answer
























                up vote
                1
                down vote













                int temp = 0;
                for(int i=0;i<L.size();i++)
                if (L.get(i).isEmpty())
                temp++;


                if (temp == L.size())
                //do what you want, all the lists inside L are empty



                This is all i can think of right now :)






                share|improve this answer






















                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  int temp = 0;
                  for(int i=0;i<L.size();i++)
                  if (L.get(i).isEmpty())
                  temp++;


                  if (temp == L.size())
                  //do what you want, all the lists inside L are empty



                  This is all i can think of right now :)






                  share|improve this answer












                  int temp = 0;
                  for(int i=0;i<L.size();i++)
                  if (L.get(i).isEmpty())
                  temp++;


                  if (temp == L.size())
                  //do what you want, all the lists inside L are empty



                  This is all i can think of right now :)







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 28 mins ago









                  Ibtihaj Uddin

                  286




                  286




















                      up vote
                      1
                      down vote













                      You can do something like this:



                      boolean isEmpty = false;
                      for(list in myList)

                      if(!isEmpty) // do your thing;





                      share|improve this answer






















                      • Why the downvote?
                        – Pritam Banerjee
                        30 mins ago














                      up vote
                      1
                      down vote













                      You can do something like this:



                      boolean isEmpty = false;
                      for(list in myList)

                      if(!isEmpty) // do your thing;





                      share|improve this answer






















                      • Why the downvote?
                        – Pritam Banerjee
                        30 mins ago












                      up vote
                      1
                      down vote










                      up vote
                      1
                      down vote









                      You can do something like this:



                      boolean isEmpty = false;
                      for(list in myList)

                      if(!isEmpty) // do your thing;





                      share|improve this answer














                      You can do something like this:



                      boolean isEmpty = false;
                      for(list in myList)

                      if(!isEmpty) // do your thing;






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited 28 mins ago

























                      answered 37 mins ago









                      Pritam Banerjee

                      10.1k64262




                      10.1k64262











                      • Why the downvote?
                        – Pritam Banerjee
                        30 mins ago
















                      • Why the downvote?
                        – Pritam Banerjee
                        30 mins ago















                      Why the downvote?
                      – Pritam Banerjee
                      30 mins ago




                      Why the downvote?
                      – Pritam Banerjee
                      30 mins ago










                      up vote
                      1
                      down vote













                      Using Java 7 and below this is the classical way to approach that:



                      for (List<Integer> list : myList) 
                      if (list != null && !list.isEmpty())
                      // do something with not empty list




                      With Java 8 and above you can use forEach:



                      myList.forEach(list -> 
                      if (list != null && !list.isEmpty())
                      // do something with not empty list

                      );


                      or, as already mentioned by Eugene add stream API touch to it:



                      myList.stream()
                      .filter(list -> (list != null && !list.isEmpty()))
                      .forEach(list ->
                      // do something with not empty list
                      );


                      Note: all these 3 examples imply that you have initialized myList variable and it is not null, otherwise NullPointerException will be thrown in all snippets above.



                      Standard JDK does not have a quick way to check that collection is not null and not empty. But if you are using Apache commons-collections library, they offer such a method: CollectionUtils.isNotEmpty(). However, I would not recommend, to add this dependency into your project just for the sake of this single function.






                      share|improve this answer


























                        up vote
                        1
                        down vote













                        Using Java 7 and below this is the classical way to approach that:



                        for (List<Integer> list : myList) 
                        if (list != null && !list.isEmpty())
                        // do something with not empty list




                        With Java 8 and above you can use forEach:



                        myList.forEach(list -> 
                        if (list != null && !list.isEmpty())
                        // do something with not empty list

                        );


                        or, as already mentioned by Eugene add stream API touch to it:



                        myList.stream()
                        .filter(list -> (list != null && !list.isEmpty()))
                        .forEach(list ->
                        // do something with not empty list
                        );


                        Note: all these 3 examples imply that you have initialized myList variable and it is not null, otherwise NullPointerException will be thrown in all snippets above.



                        Standard JDK does not have a quick way to check that collection is not null and not empty. But if you are using Apache commons-collections library, they offer such a method: CollectionUtils.isNotEmpty(). However, I would not recommend, to add this dependency into your project just for the sake of this single function.






                        share|improve this answer
























                          up vote
                          1
                          down vote










                          up vote
                          1
                          down vote









                          Using Java 7 and below this is the classical way to approach that:



                          for (List<Integer> list : myList) 
                          if (list != null && !list.isEmpty())
                          // do something with not empty list




                          With Java 8 and above you can use forEach:



                          myList.forEach(list -> 
                          if (list != null && !list.isEmpty())
                          // do something with not empty list

                          );


                          or, as already mentioned by Eugene add stream API touch to it:



                          myList.stream()
                          .filter(list -> (list != null && !list.isEmpty()))
                          .forEach(list ->
                          // do something with not empty list
                          );


                          Note: all these 3 examples imply that you have initialized myList variable and it is not null, otherwise NullPointerException will be thrown in all snippets above.



                          Standard JDK does not have a quick way to check that collection is not null and not empty. But if you are using Apache commons-collections library, they offer such a method: CollectionUtils.isNotEmpty(). However, I would not recommend, to add this dependency into your project just for the sake of this single function.






                          share|improve this answer














                          Using Java 7 and below this is the classical way to approach that:



                          for (List<Integer> list : myList) 
                          if (list != null && !list.isEmpty())
                          // do something with not empty list




                          With Java 8 and above you can use forEach:



                          myList.forEach(list -> 
                          if (list != null && !list.isEmpty())
                          // do something with not empty list

                          );


                          or, as already mentioned by Eugene add stream API touch to it:



                          myList.stream()
                          .filter(list -> (list != null && !list.isEmpty()))
                          .forEach(list ->
                          // do something with not empty list
                          );


                          Note: all these 3 examples imply that you have initialized myList variable and it is not null, otherwise NullPointerException will be thrown in all snippets above.



                          Standard JDK does not have a quick way to check that collection is not null and not empty. But if you are using Apache commons-collections library, they offer such a method: CollectionUtils.isNotEmpty(). However, I would not recommend, to add this dependency into your project just for the sake of this single function.







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited 13 mins ago

























                          answered 29 mins ago









                          Vladimir L.

                          692615




                          692615



























                               

                              draft saved


                              draft discarded















































                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52768755%2fhow-check-if-the-lists-in-a-list-are-not-empty%23new-answer', 'question_page');

                              );

                              Post as a guest













































































                              Popular posts from this blog

                              How to check contact read email or not when send email to Individual?

                              Displaying single band from multi-band raster using QGIS

                              How many registers does an x86_64 CPU actually have?