[英]Trying to create a hash table in java using string as key & double as value
In the following program: 在以下程序中:
import java.util.*;
public class HashTableStringdouble
{
// private Hashtable<String, double[]> model = new Hashtable<String, double[]>();;
private Hashtable<String, double> model = new Hashtable<String, double>();; // this does not work
public static void main(String args[])
{
}
}
having double[] works but not double. 有双[]但不是双倍。 It gives the following error:
它给出以下错误:
HashTableStringdouble.java:7: error: unexpected type private Hashtable model = new Hashtable();; HashTableStringdouble.java:7:错误:意外类型private Hashtable model = new Hashtable();; // this does not work ^ required: reference found: double HashTableStringdouble.java:7: error: unexpected type private Hashtable model = new Hashtable();;
//这不起作用^ required:reference found:double HashTableStringdouble.java:7:error:unexpected type private Hashtable model = new Hashtable();; // this does not work ^ required: reference found: double 2 errors
//这不起作用^必需:引用找到:double 2错误
I am not sure what am I doing wrong here. 我不确定我在这里做错了什么。 Please explain how does a Hashtable works.
请解释一下Hashtable的工作原理。
You can't use a primitive as a key or value in a Hashtable, you need to use an object. 您不能将原语用作Hashtable中的键或值,您需要使用对象。 It would work with
Double
instead of double
for example. 例如,它可以使用
Double
而不是double
。 The reason why it works with double[]
is that arrays are objects in Java. 它与
double[]
一起使用的原因是数组是Java中的对象。
Also, Hashtable is somewhat obsolete and HashMap is preferred in most situations: 此外,Hashtable有点过时,在大多数情况下首选HashMap :
private Map<String, Double> model = new HashMap<String, Double>();
//or if you use Java 7+
private Map<String, Double> model = new HashMap<>();
Hashtable
; Hashtable
; use HashMap
. HashMap
。 Hashtable
is a leftover from Java 1.0, before the times of the Collections Framework. Hashtable
是Java 1.0的遗留物。 尝试使用Double类而不是“native”double
You can't use primitive types in Collection
s. 您不能在
Collection
使用原始类型。 Collection
s can contain only descendants of Object
type. Collection
s只能包含Object
类型的后代。 If you need collections with primitives, you should look at this question: Most efficient Java primitive collections library . 如果您需要具有基元的集合,您应该看看这个问题: 最有效的Java原始集合库 。
Use HashMap
, not Hashtable
. 使用
HashMap
,而不是Hashtable
。 If you are sure, you need a synchronized collection, have a look at Collections.synchronizedMap()
. 如果您确定需要同步集合,请查看
Collections.synchronizedMap()
。 Eg: 例如:
Map model = Collections.synchronizedMap(new HashMap()); Map model = Collections.synchronizedMap(new HashMap());
Use wrapper classes. 使用包装类。 That's one of the reasons why the are invented in the first place.
这就是为什么首先发明它的原因之一。
private Hashtable<String, Double> model = new Hashtable<String, Double>();
import java.util.*;
public class HashTableStringdouble
{
private Hashtable<String, Double> model = new Hashtable<String, Double>();
public static void main(String args[])
{
}
}
use Double
instead of double
. 使用
Double
而不是double
。 In Generic primitive data types are not allowed 在通用原始数据类型中是不允许的
and don't forget to mark correct answer which you have accepted. 并且不要忘记标记您已接受的正确答案。 welcome to stackoverflow.
欢迎来到stackoverflow。 If you use
double[]
then it means this is double array object
(only objects can be there in generic) and when you use double
it means primitive double 如果你使用
double[]
那么这意味着这是双数组object
(只有对象可以存在于泛型中),当你使用double
它意味着原始的double
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.