0

I have a class with the following constructor:

public UniqueField(Collection<Object> items) {
      this.items=items;
}

The idea behind the Collection<Object> is that I would be able to use Collection<OtherType>.

When doing:

Collection<OtherType> collection=... 
new UniqueField(collection);

I getting a compile error of invalid argument. How can I fix this?

skaffman
  • 398,947
  • 96
  • 818
  • 769
Naor
  • 23,465
  • 48
  • 152
  • 268

2 Answers2

4

You have to use this instead

public UniqueField(Collection<? extends Object> items) {
      this.items=items;
}

or ? because it is equal to "? extends Object"

public UniqueField(Collection<?> items) {
      this.items=items;
}

You can see here for the reason

Community
  • 1
  • 1
Simon LG
  • 2,907
  • 1
  • 17
  • 16
0

you can use either:

public UniqueField(Collection<?> items) {
      this.items=items;
}

or:

public UniqueField(Collection<? super OtherType> items) {
      this.items=items;
}

or simply:

public UniqueField(Collection<OtherType> items) {
  this.items=items;
}