Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

how to avoid empty map val in a list?  RSS feed

 
Ranch Hand
Posts: 214
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a HashMap with multiple values at runtime. In that map one key has empty value , how to avoid this value to add in a list. some code sample is below:


I do not want to add the empty value in a list please suggest me in a right way.



result in map



result in List



thanks!!
 
Marshal
Posts: 4350
280
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What Type are the values in your Map? Unless this is deliberately Java 1.4 code then you should parameterize your Collections which means you define your Map as Map<String, String> or with whatever Types you actually have in there.
 
Rd Dari
Ranch Hand
Posts: 214
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim ,
Thanks for replying me.

I used the isEmpty() to avoid the empty value in a map itself. Its adding only keys those have values.

Thanks.
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if map contains string values

Why can't you try like this


list.removeAll(Arrays.asList("", null));

removes empty values and null values from list
 
Marshal
Posts: 58345
178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

sreenu sree wrote:if map contains string values

Why can't you try like this


list.removeAll(Arrays.asList("", null));

removes empty values and null values from list

What list?
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!