reg_frenzy
reg_frenzy

Reputation: 81

HashMap<Object, Boolean> toString problem

I am having a Map of type HashMap.

I am trying to iterate over the map and for every entry, for which the boolean flag is set to true, I am trying to print the corresponding key value.

I am able to achieve this. However, instead of printing the String "key" values, it prints String objects. I tried casting it, using the .toString() function. Nothing solved it.

Any help would be greatly appreciated.

Thanks,S.

Upvotes: 0

Views: 8472

Answers (6)

reg_frenzy
reg_frenzy

Reputation: 81

private Map<String, Boolean> deviceChecked = new HashMap<String, Boolean>();
deviceChecked = checkMap;

Set entry = deviceChecked.entrySet();
    Iterator i = entry.iterator();
while(i.hasNext()){
    Map.Entry ent = (Map.Entry)i.next();
    if(ent.getValue()){
        result = result + (String)ent.getKey() + ", ";
    }
}
System.out.println("result is " +result);
return result;

I am trying to print the key only if the corresopnding boolean value is true. Something like the code above. In this, the result value does not contain the string and it turn prints the object. I should get the value to be printed as "em-device90-36". But, instead I get this printed

com.f5.lunaui.emproto.reports.Device_Sri@334003

Upvotes: 0

drew
drew

Reputation: 3157

Your followup suggests that the values in your Map are not of type String and are not of a type that has overridden toString, which is why, when you call toString, you get a value like "com.f5.lunaui.emproto.reports.Device_Sri@334003".

In Device_Sri, you should override the toString method to return the String you want:

@Override
public String toString() {
    return "em_device90-36";
}

Of course, you'll probably want to calculate the value "em_device90-36" from the fields of the Device_Sri class.

Upvotes: 2

Steve Kuo
Steve Kuo

Reputation: 63064

To add to the other answers, you'll get a null pointer exception if the value is null.

if (e.getValue()) { ... }

This is because the value is a Boolean and will be unboxed to a boolean. It's equivalent to e.getValue().booleanValue().

If you want to guard against the value being null, then use

if (Boolean.TRUE.equals(e.getValue()) { ... }

Upvotes: 0

tpdi
tpdi

Reputation: 35141

You want to iterate over the Map's entrySet:

Set< Map.Entry<String, Boolean> > es = map.entrySet();

That's a set, so you can iterate over it:

for( Map.Entry<String, Boolean> v : es ) {
   if( v.getValue() ) { // auto-unboxing
       System.out.println(v.getKey());
   }
}

Simplifying:

for( Map.Entry<String, Boolean> v : map.entrySet() ) {
   if( v.getValue() ) {
       System.out.println(v.getKey());
   }
}

Upvotes: 5

CarlosZ
CarlosZ

Reputation: 8669

This should work:

Map<String, Boolean> myMap = new HashMap<String, Boolean>();
myMap.put("one", true);
myMap.put("second", false);

for (String key : myMap.keySet()) {
  if (myMap.get(key)) {
    System.out.println(key + " --> " + myMap.get(key));
  }
}

Upvotes: 1

Michael McGowan
Michael McGowan

Reputation: 6608

You probably want something like this:

for(String key : map.keySet()){
  if(map.get(key)){
    System.out.println(key);
  }
}

Upvotes: 2

Related Questions