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 ?

3
  • Please reformat. It doesn't show up properly. Commented Aug 13, 2012 at 22:04
  • Don't use the <> around ? extends Action...and you'll be closer to being right. Commented Aug 13, 2012 at 22:06
  • 1
    The error is not in Java Generics. Commented Aug 13, 2012 at 22:12

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

You cannot instantiate a type with a wildcard argument.
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.

Comments

-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 {}

5 Comments

You cannot instantiate a type with a wildcard argument.
Why ? super Action? Isn't that different from the variable the OP's trying to assign to?
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...
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.
Yeah agreed probably need more to the context to really understand if a wildcard is even necessary here.

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.