0

I tried the line below:

HashMap <Temp,<? extends Action> > p= new HashMap<Temp,  <? extends Action>>()

Eclispe is giving me an error

Multiple markers at this line - Syntax error on token ",", Type expected after this token - Syntax error on token ",", Type expected after this token

Any reasons why i am getting this error and how to remove this ?

Carl Manaster
  • 39,912
  • 17
  • 102
  • 155
abhig
  • 840
  • 2
  • 12
  • 22

3 Answers3

4

Have this:

HashMap <Temp, ? extends Action > p= new HashMap<Temp,  *>();

You need two types for a map. Type for key, and a type for value. You cannot surround the second (value) type with < & > because it is just not allowed.

I also recommend programming to an interface, note the declared type is an interface:

Map<Temp, ? extends Action > p = new HashMap<Temp, ? extends Action>();

EDIT

As pointed out, you cannot instantiate a variable with a wildcard generic type. Please change * to the type that you need.

jn1kk
  • 5,012
  • 2
  • 45
  • 72
2

This will compile :

Map<Temp, ? extends Action> p = new HashMap<Temp, Action>();

Or with Java 7:

Map<Temp, ? extends Action> p = new HashMap<>();

You had extra < and > in the value type parameter, causing a syntax error. Past that, you can't instantiate a generic type using a wildcard as a type parameter.

You'll also notice I changed the variable to Map instead of HashMap - it's good practice to program to interfaces instead of implementations, like winged's answer mentions.

Community
  • 1
  • 1
Paul Bellora
  • 54,340
  • 18
  • 130
  • 181
-4

Program to interfaces!

Map<Temp, ? extends Action> p = new HashMap<Temp, ? extends Action>();

Its better practice.

I noticed that perhaps this wasn't answered correctly. The problem is explained in detail here. What will work for you is perhaps

Map<Temp, ? super Action> p = new HashMap<Temp, Action>();

Hope that helps.

Third edit, I'm new to writing comments so note sure what the process is anyhow here is an example:

public void doSomething ()
{
    Map<A, ? super A> p = new HashMap<A, A>();

    A a1 = new A();
    A a2 = new A();

    // AS A CONSUMER
    // Not a problem because at runtime we'll know we can accept A.
    p.put(a1, new B());
    p.put(a2, new C());

    // AS A PRODUCER
    // what do I cast to? No way of knowing if its B or C.
    for (Object a : p.values())
    {
    }

    // Same deal as above is it B or C?
    p.get(a1);
}

class A{}

class B extends A {}

class C extends B {}
Community
  • 1
  • 1
ramsinb
  • 1,985
  • 12
  • 17
  • 6
    You cannot instantiate a type with a wildcard argument. – Jeffrey Aug 13 '12 at 23:11
  • Why `? super Action`? Isn't that different from the variable the OP's trying to assign to? – Paul Bellora Aug 14 '12 at 19:15
  • It basically comes down to what you want to do with the map. If you want to add items (list is a producer) to it then at runtime with extends Action> you cannot at runtime know the specific class you are adding therefore you cannot add. If you want to read from the list (list is a consumer) the super Action> will guarantee that Action can only be allowed in the list. A much better explanation is available in the link I provided earlier... – ramsinb Aug 14 '12 at 21:21
  • 1
    I think we're on the same page about PECS. But really, why does the OP use a wildcard at all? The question's too opaque to answer anything beyond the syntax errors. Anyway +1 for correcting and adding to your answer. It's unfortunate you got stuck with downvotes. – Paul Bellora Aug 16 '12 at 01:59
  • Yeah agreed probably need more to the context to really understand if a wildcard is even necessary here. – ramsinb Aug 16 '12 at 03:08