0

1.what is for Collection < ? >

i tested this code:

    Collection<?> l = new ArrayList<String>();
    l.add("s");

i can't add "s" String.So what is for < ? > ? in which situations i will use it?

2.what is the difference between Collection< ? > and Collection< Object >

Sarkhan
  • 1,281
  • 1
  • 11
  • 33

2 Answers2

1

1.what is for Collection < ? >

Collection<?> designates a Collection whose element type is unknown. Because the element type is unknown, you can never know that any particular object is of a type that the collection accepts, therefore it is not type-safe to add anything to it.

On the other hand, you can always assume that any element you obtain from such a collection is an Object, and you can invoke any of the inquiry and element removal methods.

This is useful and appropriate for collection operations that do not depend on the type of the collection elements.

2.what is the difference between Collection< ? > and Collection< Object >

A Collection<Object> is a collection that may have any Object as an element. The two are similar in that you cannot make any assumption about the types of the elements of such a collection, but they are quite different in that it is safe to add any object to a Collection<Object>. It is the reason why you cannot make assumptions about element types that makes all the difference: for Collection<?> it is that you don't know the type constraint, but for Collection<Object> it is that (you know that) there is no constraint.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
0
  1. From Oracle

Collection<?> (pronounced "collection of unknown"), that is, a collection whose element type matches anything. It's called a wildcard type for obvious reasons.

  1. A variable of type Collection<?> is a collection that may contain any type. Your code has no idea what type of of object will be put in that collection. This is subtly different than a Collection<Object>, which is a collection that contains objects of the Object class (it specifies Object, instead of saying that it can except anything). (Which is the parent of every object in java, including String).

You usually don't want to initialize a Collection<?> as you can't add anything to it (as far as I am aware).

However, when you're making a function that can operate on a collection of ANYTHING, that is very useful.

For example, from you could make a function like that example from oracle.

void printCollection(Collection<?> c) {
    for (Object e : c) {
        System.out.println(e);
    }
}

This method will be able to accept a collection of any type.

Where

void printCollection(Collection<Object> c) {
    for (Object e : c) {
        System.out.println(e);
    }
}

Is only able to operate on a Collection<Object>, (which is NOT a Collection<String>, even though String is a subclass of Object)

dustinroepsch
  • 1,122
  • 1
  • 7
  • 24
  • you sayt that Collection< ? > is a collection of anything.Here anything means any object i think.Because of is there any item that we add to collection which is not an object.Everything extends from Object. i confuse this.even we can't add int , we have to add Integer so it also extends from Object – Sarkhan Mar 22 '16 at 04:54
  • Most newbies do not fully comprehend the difference between types, classes, instances, and variables. I think it could be confusing when you say, "A `Collection>` is a collection of anything" because that sounds exactly like a `Collection` (i.e., a collection that can contain any object.) I think your meaning would be more clear if you said that a _variable_ of _type_ `Collection>` may _refer to_ a `Collection` object of any type `T`. – Solomon Slow Mar 22 '16 at 13:38
  • I whole-heartedly agree. I was in a slight hurry when I wrote this, I'm definitely open to suggestions :) – dustinroepsch Mar 22 '16 at 14:05