Reputation: 3728
How to check wheather specific kay have an empty value in a LinkedhashMap
Map<String,List<String>>resultTable = new LinkedHashMap<String, List<String>>();
following are the resultTable
Key = Sr.ID
and values are {660316, 660320, 590089, 661725, 661865, 661864, 661862, 660300}
Key = Docket
and values are {0150, 0150b1, 142747, 1725.186589, 2708.5, 2738.56, 45rdfg, 56t}
Key = Rec No
and values are{,8821893, , , , , , , }
how can we check if particular key (i.e Sr.ID or Docket or Rec No.)have an empty Value in the List (Rec No, Key have an Empty Value)
how can we check if particular key (i.e Sr.ID or Docket or Rec No.)don't have an empty Value in the List Docket Key Don't have an empty Value
Upvotes: 0
Views: 2706
Reputation: 48404
Your values are of type List<String>
.
To check that an element of the list is empty for any given key, you can simply:
// iterate map entries
for (String key: resultTable.keySet()) {
// TODO null check!
if (resultTable.get(key).contains("")) {
// list has an empty element
System.out.printf("Found an empty value for %s%n", key);
}
}
Note
And here's a Java 8 syntax with a BiConsumer<String,List<String>>
lambda:
resultTable.forEach(
(s,l) -> {
// TODO null check!
if (l.contains("")) System.out.printf("Found an empty value for %s%n", s);
}
);
Upvotes: 3
Reputation: 1680
This is a simple method for checking an empty value for a particular key:
private boolean checkEmptyVal(String key){
boolean result = false;
List<String> val = resultTable.get(key);
if(val != null && val.contains("")) {
result = true;
}
return result;
}
Upvotes: 2
Reputation: 4527
According to your example, an empty value is not a null
but an empty string.
To check for an empty value in a list, you can do list.contains("")
.
Translated to your context, this is:
if (resultTable.get(key).contains("")) {
// empty value here
}
Mena suggested checking for all keys one after the other:
// iterate map values
for (List<String> value: resultTable.values()) {
if (value.contains("")) {
// list has an empty element
}
}
You can also add code to remove all empty values from a list:
while (list.remove("")) {
log.debug("Empty value removed");
}
Upvotes: 0