3

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.

4
  • 3
    You have to use Double. double is a primitive type. Commented Mar 14, 2013 at 11:25
  • You are not allowed to use primitives in generics. Read this: stackoverflow.com/questions/2721546/… Commented Mar 14, 2013 at 11:27
  • Generics cannot take primitive values in Java. (Just highlighting that what you're trying to do is not HashTable specific.) Commented Mar 14, 2013 at 11:29
  • @user714965 what if we use double[] ? We can use double[] but it should be array object of primitive data type. Commented Mar 14, 2013 at 12:22

7 Answers 7

11

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<>();
Sign up to request clarification or add additional context in comments.

Comments

6
  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.

Comments

4

Try using the Double class instead of "native" double

Comments

2
  1. You can't use primitive types in Collections. Collections 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(). E.g.:

    Map model = Collections.synchronizedMap(new HashMap());

Comments

1

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>();

Comments

1
import java.util.*;

public class HashTableStringdouble
{
  private Hashtable<String, Double> model  = new Hashtable<String, Double>();


    public static void main(String args[])
    {
    }
}

Comments

1

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

2 Comments

Why you are asking me in the comments of the question? You seem to know this.
@user714965 wanted to make sure that we are on the same page.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.