HashMap<对象 ,布尔值>字符串问题

发布于 2024-10-29 10:16:39 字数 188 浏览 1 评论 0原文

我有一个 HashMap 类型的 Map。

我试图迭代地图,对于布尔标志设置为 true 的每个条目,我试图打印相应的键值。

我能够实现这一目标。但是,它不是打印字符串“键”值,而是打印字符串对象。我尝试使用 .toString() 函数来转换它。什么也解决不了。

任何帮助将不胜感激。

谢谢,S。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

药祭#氼 2024-11-05 10:16:39

您想要迭代Map的entrySet:

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

这是一个集合,因此您可以迭代它:

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

简化:

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

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());
   }
}
捶死心动 2024-11-05 10:16:39

您的后续建议表明您的 Map 中的值不是 String 类型,也不是覆盖 toString 的类型,这就是为什么当您调用 toString 时,您会得到类似“com.f5.lunaui.emproto.reports”的值。 Device_Sri@334003”。

在Device_Sri 中,您应该重写toString 方法以返回您想要的字符串:

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

当然,您可能希望从Device_Sri 类的字段中计算值“em_device90-36”。

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.

梦初启 2024-11-05 10:16:39

你可能想要这样的东西:

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

You probably want something like this:

for(String key : map.keySet()){
  if(map.get(key)){
    System.out.println(key);
  }
}
凉薄对峙 2024-11-05 10:16:39

这应该有效:

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));
  }
}

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));
  }
}
玩心态 2024-11-05 10:16:39

要添加到其他答案,如果值为空,您将收到空指针异常。

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

这是因为该值是一个 Boolean 并且将被拆箱为 boolean。它相当于e.getValue().booleanValue()

如果你想防止值为空,那么使用

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

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()) { ... }
漆黑的白昼 2024-11-05 10:16:39
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;

仅当相应的布尔值为 true 时,我才尝试打印密钥。就像上面的代码一样。在此,结果值不包含字符串,而是打印对象。
我应该将值打印为“em-device90-36”。但是,我却打印了这个

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文