java Hashtable<String, Object> 没有找到字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2029242/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hashtable<String, Object> not finding strings
提问by Kirk
I have a Hashtable of type Hashtable
我有一个 Hashtable 类型的 Hashtable
I've loaded several strings as keys, one of which is "ABCD"
我加载了几个字符串作为键,其中之一是“ABCD”
However, later when I go to look up "ABCD", the Hashtable returns null instead of the associated object. Further the keyset contains "ABCD", but a request to containsKey("ABCD") returns false.
但是,稍后当我查找“ABCD”时,Hashtable 返回 null 而不是关联对象。此外,密钥集包含“ABCD”,但对 containsKey("ABCD") 的请求返回 false。
Is this because String objects are inherently different objects?
这是因为 String 对象本质上是不同的对象吗?
If so, what is the write way to store information in a Hashtable if I want to use Strings as keys?
如果是这样,如果我想使用字符串作为键,在哈希表中存储信息的写入方式是什么?
public class Field {
private String name;
private DataType dataType;
public Field(String name, DataType dataType) {
this.name = name;
this.dataType = dataType;
}
public String getName() {
return name;
}
public DataType getDataType() {
return dataType;
}
public String toString() {
return name;
}
}
public class Record {
private Hashtable<String, Data> content;
public Record(Field[] fieldList) {
this.fieldList = fieldList;
content = new Hashtable<String, Data>();
System.out.println(fieldList.length);
for(Field f : fieldList) {
content.put(f.getName(), new Data());
}
}
public void add(String field, String s) {
// ERROR OCCURS HERE IN THIS METHOD !!!
System.out.println(field);
for(String ss : content.keySet()) {
System.out.print(" [ " + ss + " ] ");
}
System.out.println();
System.out.println(content.containsKey(field));
System.out.println(content.get(field));
content.get(field).add(s);
}
}
public class Data {
private Vector<String> lines;
private int index;
public Data() {
lines = new Vector<String>();
index = 0;
}
public void add(String s) {
System.out.println("adding");
lines.add(s);
}
public String nextLine() {
try {
return lines.elementAt(index++);
} catch (ArrayIndexOutOfBoundsException aioobe) {
return null;
}
}
}
回答by Tom Hawtin - tackline
Works for me!
对我有用!
import java.util.Hashtable;
public class StrMap {
public static void main(String[] args) {
Hashtable<String,Object> map = new Hashtable<String,Object>();
map.put("ABCD", "value");
System.err.println(map.containsKey("ABCD"));
}
}
Yo have probably made some other error. Reduce the problem to the smallest complete compilable program that still demonstrates the problem. You'll probably find the problem straight away. If you don't, at least you will have a question that we can answer.
你可能犯了一些其他错误。将问题简化为仍能演示问题的最小完整的可编译程序。您可能会立即发现问题。如果你不这样做,至少你会有一个我们可以回答的问题。
(Also Mapand HashMapis that way to go. Hashtableis useful if you are using a pre-Java 2 API (Java 2 is comfortably over a decade old now!).)
(也Map并HashMap是的路要走。Hashtable是,如果你使用的是预的Java 2 API有用(的Java 2是舒服了十多年的老吧!)。)
回答by duffymo
Hashtable is a Java 1.0 data structure. I wonder why you're not using a Map?
哈希表是 Java 1.0 的数据结构。我想知道你为什么不使用地图?
If java.lang.String is the key type, I'd say you're being hosed by something else that's impossible to guess without posting code.
如果 java.lang.String 是关键类型,我会说你正在被其他一些不发布代码就无法猜测的东西所困扰。
回答by BalusC
It's hard to pinpoint the root cause without an SSCCEfrom your side.
如果没有您身边的SSCCE,就很难查明根本原因。
At least, the common causes are:
至少,常见的原因是:
- You're not using the
Hashtableyou think you're using.System.out.println()it to verify. - The
Stringis actually in a different case, e.g."ABcD"instead of"ABCD". - The
Stringis surrounded with some whitespace which you needs totrim()first.
- 你没有使用
Hashtable你认为你正在使用的。System.out.println()它来验证。 - 该
String实际上是在不同的情况下,例如"ABcD"代替"ABCD"。 - 在
String被包围,你需要一些空白trim()第一。
That said (and unrelated to the actual problem), I strongly recommend to use the improved HashMapinstead of the legacy Hashtable. Here's a Sun tutorial about maps.
也就是说(与实际问题无关),我强烈建议使用改进的HashMap而不是遗留的Hashtable. 这是关于地图的 Sun 教程。
回答by richs
Can you also post the exact output you get from the following method when field is "ABCD"?
当字段为“ABCD”时,您还可以发布从以下方法获得的确切输出吗?
public void add(String field, String s) {
// ERROR OCCURS HERE IN THIS METHOD !!!
System.out.println(field);
for(String ss : content.keySet()) {
System.out.print(" [ " + ss + " ] ");
}
System.out.println();
System.out.println(content.containsKey(field));
System.out.println(content.get(field));
content.get(field).add(s);
}

