• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 260
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you getting an Exception?
 
albert kao
Ranch Hand
Posts: 260
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Crossposted: http://www.java-forums.org/new-java/34379-test-all-empty-strings-linkedhashmap-string-arraylist-string.html
 
albert kao
Ranch Hand
Posts: 260
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 27235
87
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 75844
361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for noticing the cross-post, Kevin Workman.
 
albert kao
Ranch Hand
Posts: 260
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 27235
87
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 260
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
reply
    Bookmark Topic Watch Topic
  • New Topic