Java LinkedHashSet.contains()在应返回false时返回true
WTF?如果DEF_id不包含profile0,为什么它会说它包含?错误不在您发布的代码中。下面将生成预期的结果Java LinkedHashSet.contains()在应返回false时返回true,java,android,linkedhashset,Java,Android,Linkedhashset,WTF?如果DEF_id不包含profile0,为什么它会说它包含?错误不在您发布的代码中。下面将生成预期的结果 isdefault(profile0) = true 执行下面的代码,它给了我正确的结果。 问题似乎是您使用的是静态LinkedHashSet,它将保留以前的值,直到您不明确地清除它为止,即只初始化一次 添加更多细节或结束您的问题,因为它一点也不清楚,并且没有提供关于您如何准确使用此代码的完整上下文 isDefault(def) = true isDefault(def) = tr
isdefault(profile0) = true
执行下面的代码,它给了我正确的结果。 问题似乎是您使用的是静态LinkedHashSet,它将保留以前的值,直到您不明确地清除它为止,即只初始化一次 添加更多细节或结束您的问题,因为它一点也不清楚,并且没有提供关于您如何准确使用此代码的完整上下文
isDefault(def) = true
isDefault(def) = true
isDefault(NOT) = false
输出:-isdefaultprofile0=false
profile0是一个变量还是profile0?这意味着有人在调用isDefault之前把它放在这里。原因很可能是DEF_id是静态的,并且在类之间共享。我刚刚测试了您的代码并进行了测试。一定是其他地方出了问题。可能是什么东西在修改集合的内容。@dotvav没有变量配置文件0。我只是懒得写isdefault\+currentId+\=
public static final String DEFAULT_ID = "def";
public static final LinkedHashSet<String> DEF_IDS = new LinkedHashSet<>(Arrays.asList(DEFAULT_ID));
private static boolean isDefault(String currentId) {
return DEF_IDS.contains(currentId);
}
private void test(String def) {
System.out.println("isDefault(" + def + ") = " + isDefault(def));
}
public void test() {
test("def");
test(DEFAULT_ID);
test("NOT");
}
isDefault(def) = true
isDefault(def) = true
isDefault(NOT) = false
public static final String DEFAULT_ID = "def";
public static final LinkedHashSet<String> DEF_IDS = new LinkedHashSet<>(Arrays.asList(DEFAULT_ID));
public static void main(String[] args){
isDefault("profile0");
}
private static boolean isDefault(String currentId) {
System.out.println("isdefault("+currentId+") = " + DEF_IDS.contains(currentId));
return DEF_IDS.contains(currentId);
}