简体   繁体   中英

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();; // this does not work ^ 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

I am not sure what am I doing wrong here. Please explain how does a Hashtable works.

You can't use a primitive as a key or value in a Hashtable, you need to use an object. It would work with Double instead of double for example. The reason why it works with double[] is that arrays are objects in Java.

Also, Hashtable is somewhat obsolete and HashMap is preferred in most situations:

private Map<String, Double> model  = new HashMap<String, Double>();
//or if you use Java 7+
private Map<String, Double> model  = new HashMap<>();
  1. Don't use Hashtable ; use HashMap . Hashtable is a leftover from Java 1.0, before the times of the Collections Framework.
  2. This is not about how maps work in Java, but how Java works in general. You cannot substitute a primitive type for a reference type anywhere.

尝试使用Double类而不是“native”double

  1. You can't use primitive types in Collection s. Collection s can contain only descendants of Object type. If you need collections with primitives, you should look at this question: Most efficient Java primitive collections library .

  2. Use HashMap , not Hashtable . If you are sure, you need a synchronized collection, have a look at Collections.synchronizedMap() . Eg:

    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 . In Generic primitive data types are not allowed
and don't forget to mark correct answer which you have accepted. welcome to 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM