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.

MdT
  • 871
  • 3
  • 10
  • 15
  • 3
    You have to use `Double`. `double` is a primitive type. – Kai Mar 14 '13 at 11:25
  • You are not allowed to use primitives in generics. Read this: http://stackoverflow.com/questions/2721546/why-dont-generics-support-primitive-types – rbobin Mar 14 '13 at 11:27
  • Generics cannot take primitive values in Java. (Just highlighting that what you're trying to do is not `HashTable` specific.) – Quetzalcoatl Mar 14 '13 at 11:29
  • @user714965 what if we use `double[]` ? We can use double[] but it should be array object of primitive data type. – AmitG Mar 14 '13 at 12:22

7 Answers7

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<>();
assylias
  • 321,522
  • 82
  • 660
  • 783
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.
Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
4

Try using the Double class instead of "native" double

Shmil The Cat
  • 4,548
  • 2
  • 28
  • 37
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());

Community
  • 1
  • 1
Ostap Andrusiv
  • 4,827
  • 1
  • 35
  • 38
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>();
Bikash Rath
  • 159
  • 1
  • 7
1
import java.util.*;

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


    public static void main(String args[])
    {
    }
}
c.pramod
  • 606
  • 1
  • 8
  • 22
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

AmitG
  • 10,365
  • 5
  • 31
  • 52