gpt4 book ai didi

java - 循环遍历对象的 HashSet Java

转载 作者:行者123 更新时间:2023-12-01 22:12:22 27 4
gpt4 key购买 nike

我正在尝试循环遍历 Java 8 中枚举对象的 HashSet,以查看某个值是否等于特定字符串。我已经尝试了 equals== 但它没有返回 true。

哈希集看起来像:

enter image description here

我尝试过以下方法:

for(Object g : groups){
if(g.equals("group_certification_staff")){
//Do stuff
}

for(Object g : groups){
if(g == "group_certification_staff"){
//Do stuff
}

但它一直返回 false

我错过了什么?

最佳答案

字符串不等于枚举。您可以编写类似g.name().equals("group_certification_staff")的内容来查看它是否具有相同的名称。

关于java - 循环遍历对象的 HashSet Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58649623/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com