Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Test for all empty Strings in LinkedHashMap<String,ArrayList<String>

 
albert kao
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For the LinkedHashMap<String,ArrayList<String> data type, how to find out whether all the Strings in ArrayList are empty/null?

I tried the following code:


Output is:
productList {PC=[], Mac=[]} size 2
st 2
st2 size 2
st2 [[], []]



Why the last line "st2 null " is not displayed?
What should be the code to find out whether all the Strings in st2 are empty/null?
 
Kevin Workman
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you getting an Exception?
 
albert kao
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kevin Workman wrote:Are you getting an Exception?


I think so.
 
Kevin Workman
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
albert kao wrote:
Kevin Workman wrote:Are you getting an Exception?


I think so.


...what's the Exception?
 
Kevin Workman
Ranch Hand
Posts: 151
 
albert kao
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kevin Workman wrote:
albert kao wrote:
Kevin Workman wrote:Are you getting an Exception?


I think so.


...what's the Exception?


I need to add a try catch block to find out.
My main concern is how to test for all empty Strings in LinkedHashMap<String,ArrayList><String>?
 
Kevin Workman
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
albert kao wrote:I need to add a try catch block to find out.

What? Why? Just look at the stack trace.

albert kao wrote:My main concern is how to test for all empty Strings in LinkedHashMap<String,ArrayList><String>?

Okay, that's good to know.

The simplest way seems to be looping through and checking each one, no?
 
Paul Clapham
Sheriff
Posts: 21318
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
albert kao wrote:For the LinkedHashMap<String,ArrayList<String> data type, how to find out whether all the Strings in ArrayList are empty/null?


For those of us who aren't familiar with your problem, could you clarify whether you just want to find out whether all of the Strings in all of the ArrayList are empty or null? Or do you want to find out separately for each of the ArrayLists whether all of the Strings in it are empty or null?

And probably you should clarify what "empty or null" means; people often use those terms loosely. Even the code you posted suggested you were only interested in nulls and not in empty strings.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49776
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for noticing the cross-post, Kevin Workman.
 
albert kao
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:
albert kao wrote:For the LinkedHashMap<String,ArrayList<String> data type, how to find out whether all the Strings in ArrayList are empty/null?


For those of us who aren't familiar with your problem, could you clarify whether you just want to find out whether all of the Strings in all of the ArrayList are empty or null? Or do you want to find out separately for each of the ArrayLists whether all of the Strings in it are empty or null?

And probably you should clarify what "empty or null" means; people often use those terms loosely. Even the code you posted suggested you were only interested in nulls and not in empty strings.


Both.
I want to find out whether all of the Strings in all of the ArrayList are empty or null.
If all of the Strings in all of the ArrayList are not empty or null, then I want to find out separately for each of the ArrayLists whether all of the Strings in it are empty or null, else I do not need to do any more tests.
Empty string means the string is set to "".
Null string means the string is set to null.
 
Kevin Workman
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
albert kao wrote:Both.

I repeat: The simplest way seems to be looping through and checking each one, no?
 
Paul Clapham
Sheriff
Posts: 21318
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kevin Workman wrote:
albert kao wrote:Both.

I repeat: The simplest way seems to be looping through and checking each one, no?


Since Albert is looking for the presence of two different values (empty and null), there isn't going to be any clever way using the Collections API. The boring solution with loops or iterators is going to be the answer.
 
albert kao
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:
Kevin Workman wrote:
albert kao wrote:Both.

I repeat: The simplest way seems to be looping through and checking each one, no?


Since Albert is looking for the presence of two different values (empty and null), there isn't going to be any clever way using the Collections API. The boring solution with loops or iterators is going to be the answer.


Yes, you are right.
The code should be like this:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic