views:

37

answers:

1

I have created a TreeMap like so:

TreeMap<Integer, ArrayList<MyClass>> wrap = new TreeMap<Integer, ArrayList<MyClass>>();

I have created a constructor like so:

public foo (TreeMap<Integer, Collection<Spot> > objects) {
     this.field = objects;
}

However, eclipse gives me a red squigly when I use the constructor, with my wrap variable as the single parameter:

The constructor foo(TreeMap<Integer,ArrayList<Spot>>) is undefined

An ArrayList is a type of Collection...yes? So why is this not working?

+2  A: 

Generics don't work as you think they do in this case.

What you need is something similar to:

public foo (TreeMap<Integer, ? extends Collection<Spot> > objects) {
     this.field = objects;
}

The ? is called a wild card. It will allow you to pass in a Collection, or anything that extends/implements Collection.

The line ? extends Collection<Spot> reads like:

Something that extends a Collection.

jjnguy