How check if the lists in a list are not empty
Clash 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?
java list
add a comment |Â
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?
java list
add a comment |Â
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?
java list
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
java list
edited 37 mins ago
asked 46 mins ago
helen
173111
173111
add a comment |Â
add a comment |Â
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
Are you surecontains(null)
will throw an error?
â shmosel
13 mins ago
@shmosel yes, I do
â Eugene
10 mins ago
That's very surprising. Awful design decision, if you ask me.
â shmosel
51 secs ago
add a comment |Â
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
New contributor
almost correct, you have to check againstnull
too
â Eugene
33 mins ago
Q was "are not empty". But check for null not a problem too.
â Mark
27 mins ago
you are right, but the code in the question wasmyList.get(0) != null
...
â 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
22 mins ago
you are right, thank!
â Mark
15 mins ago
add a comment |Â
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 :)
add a comment |Â
up vote
1
down vote
You can do something like this:
boolean isEmpty = false;
for(list in myList)
if(!isEmpty) // do your thing;
Why the downvote?
â Pritam Banerjee
25 mins ago
add a comment |Â
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.
add a comment |Â
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
Are you surecontains(null)
will throw an error?
â shmosel
13 mins ago
@shmosel yes, I do
â Eugene
10 mins ago
That's very surprising. Awful design decision, if you ask me.
â shmosel
51 secs ago
add a comment |Â
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
Are you surecontains(null)
will throw an error?
â shmosel
13 mins ago
@shmosel yes, I do
â Eugene
10 mins ago
That's very surprising. Awful design decision, if you ask me.
â shmosel
51 secs ago
add a comment |Â
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
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
edited 31 mins ago
answered 45 mins ago
Eugene
63.3k987146
63.3k987146
Are you surecontains(null)
will throw an error?
â shmosel
13 mins ago
@shmosel yes, I do
â Eugene
10 mins ago
That's very surprising. Awful design decision, if you ask me.
â shmosel
51 secs ago
add a comment |Â
Are you surecontains(null)
will throw an error?
â shmosel
13 mins ago
@shmosel yes, I do
â Eugene
10 mins ago
That's very surprising. Awful design decision, if you ask me.
â shmosel
51 secs ago
Are you sure
contains(null)
will throw an error?â shmosel
13 mins ago
Are you sure
contains(null)
will throw an error?â shmosel
13 mins ago
@shmosel yes, I do
â Eugene
10 mins ago
@shmosel yes, I do
â Eugene
10 mins ago
That's very surprising. Awful design decision, if you ask me.
â shmosel
51 secs ago
That's very surprising. Awful design decision, if you ask me.
â shmosel
51 secs ago
add a comment |Â
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
New contributor
almost correct, you have to check againstnull
too
â Eugene
33 mins ago
Q was "are not empty". But check for null not a problem too.
â Mark
27 mins ago
you are right, but the code in the question wasmyList.get(0) != null
...
â 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
22 mins ago
you are right, thank!
â Mark
15 mins ago
add a comment |Â
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
New contributor
almost correct, you have to check againstnull
too
â Eugene
33 mins ago
Q was "are not empty". But check for null not a problem too.
â Mark
27 mins ago
you are right, but the code in the question wasmyList.get(0) != null
...
â 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
22 mins ago
you are right, thank!
â Mark
15 mins ago
add a comment |Â
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
New contributor
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
New contributor
edited 6 mins ago
New contributor
answered 41 mins ago
Mark
1542
1542
New contributor
New contributor
almost correct, you have to check againstnull
too
â Eugene
33 mins ago
Q was "are not empty". But check for null not a problem too.
â Mark
27 mins ago
you are right, but the code in the question wasmyList.get(0) != null
...
â 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
22 mins ago
you are right, thank!
â Mark
15 mins ago
add a comment |Â
almost correct, you have to check againstnull
too
â Eugene
33 mins ago
Q was "are not empty". But check for null not a problem too.
â Mark
27 mins ago
you are right, but the code in the question wasmyList.get(0) != null
...
â 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
22 mins ago
you are right, thank!
â Mark
15 mins ago
almost correct, you have to check against
null
tooâ Eugene
33 mins ago
almost correct, you have to check against
null
tooâ Eugene
33 mins ago
Q was "are not empty". But check for null not a problem too.
â Mark
27 mins ago
Q was "are not empty". But check for null not a problem too.
â Mark
27 mins ago
you are right, but the code in the question was
myList.get(0) != null
...â Eugene
26 mins ago
you are right, but the code in the question was
myList.get(0) != null
...â 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
22 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
22 mins ago
you are right, thank!
â Mark
15 mins ago
you are right, thank!
â Mark
15 mins ago
add a comment |Â
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 :)
add a comment |Â
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 :)
add a comment |Â
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 :)
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 :)
answered 23 mins ago
Ibtihaj Uddin
286
286
add a comment |Â
add a comment |Â
up vote
1
down vote
You can do something like this:
boolean isEmpty = false;
for(list in myList)
if(!isEmpty) // do your thing;
Why the downvote?
â Pritam Banerjee
25 mins ago
add a comment |Â
up vote
1
down vote
You can do something like this:
boolean isEmpty = false;
for(list in myList)
if(!isEmpty) // do your thing;
Why the downvote?
â Pritam Banerjee
25 mins ago
add a comment |Â
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;
You can do something like this:
boolean isEmpty = false;
for(list in myList)
if(!isEmpty) // do your thing;
edited 23 mins ago
answered 33 mins ago
Pritam Banerjee
10.1k64262
10.1k64262
Why the downvote?
â Pritam Banerjee
25 mins ago
add a comment |Â
Why the downvote?
â Pritam Banerjee
25 mins ago
Why the downvote?
â Pritam Banerjee
25 mins ago
Why the downvote?
â Pritam Banerjee
25 mins ago
add a comment |Â
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.
add a comment |Â
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.
add a comment |Â
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.
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.
edited 8 mins ago
answered 24 mins ago
Vladimir L.
692615
692615
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password