Why is Collection not simply treated as Collection<?>

collections java
collections python
qualitative research: data collection, analysis, and management
data collection methods in qualitative research pdf
python namedtuple
java collections framework list
collection interface enables you to work with groups of objects.
collections.counter python 3

Consider the following API method taken from Shiro's org.apache.shiro.subject.PrincipalCollection interface but probably present in other libraries as well:

Collection fromRealm(String realmName);

Yes even nowadays there are still libraries that are using raw-types, probably to preserve pre Java 1.5 compatibility?!

If I now want to use this method together with streams or optionals like this:


I get a warning about unchecked conversion and using raw types and that I should prefer using parameterized types.


Type safety: The method collect(Collector) belongs to the raw type Stream. References to generic type Stream<T> should be parameterized


Note: GenericsTest.java uses unchecked or unsafe operations.

As I can't change the API method's signature to get rid of this warning I can either annotate with @SuppressWarnings("unchecked") or simply cast to Collection<?> like this:

((Collection<?>) principals.fromRealm(realmName)).stream().collect(Collectors.toSet());

As this cast of course always works I'm wondering why the compilers are not simply treating Collection as Collection<?> but warn about this situation. Adding the annotation or the cast doesn't improve the code a single bit, but decreases readability or might even shadow actual valid warnings about usage of unparameterized types.

The reason is quite simple:

You may read Objects from a Collection<?> the same way as from Collection. But you can't add Objects to a Collection<?> (The compiler forbids this) whereas to a Collection you can.

If after the release of Java 5 the compiler had translated every Collection to Collection<?>, then previously written code would not compile anymore and thus would destroy the backward compatibility.

Java collections framework, In this article, we review some principles of the collection, analysis, and When being reflexive, researchers should not try to simply ignore or avoid their own in which they were treated in hospital, such as “not being listened to” or “lack of� As this cast of course always works I'm wondering why the compilers are not simply treating Collection as Collection<?> but warn about this situation. Adding the annotation or the cast doesn't improve the code a single bit, but decreases readability or might even shadow actual valid warnings about usage of unparameterized types.

The major difference between raw type and unbounded wildcard <?> is that the latter is type safe, that is, on a compile level, it checks whether the items in the collection are of the same type. Compiler won't allow you to add string and integer to the collection of wildcard type, but it will allow you to do this:

List raw = new ArrayList();

Actually, in case of unbounded wildcard collections (List<?> wildcard = new ArrayList<String>()), you can't add anything at all to the list but null (from Oracle docs):

Since we don't know what the element type of c stands for, we cannot add objects to it. The add() method takes arguments of type E, the element type of the collection. When the actual type parameter is ?, it stands for some unknown type. Any parameter we pass to add would have to be a subtype of this unknown type. Since we don't know what type that is, we cannot pass anything in. The sole exception is null, which is a member of every type.

Qualitative Research: Data Collection, Analysis, and Management, The NCRS does not explicitly collect data on TARGIT treatment. However, such treatment is simple to identify by selecting cases that have both surgery and� Typically any upload to a collection will automatically inherit the media type of the collection (texts, movies, audio, software, image, etc.). If your item is not intended to be the media type of the collection, you may upload it to a public collection and then send a request to us to move it at info@archive.org.

A Collection<?> screams:

Please don't add anything to me. I have a strict content type, ... well uh, I just forgot what type it is.

While a Collection says:

It's all cool ! You can add whatever you like, I have no restrictions.

So, why shouldn't the compiler translate Collection to Collection<?> ? Because it would put up a lot of restrictions.

Method of data collection for patients treated with TARGIT using the , It is a collection where elements are stored as dictionary keys and their counts are stored as dictionary values. This class method is not implemented for Counter objects. All remaining arguments are treated the same as if they were passed to the Instead, simply create a new named tuple type from the _fields attribute:. Collection companies can seel the debt to a debt buyer,as can any company that is owed money from customers. These debt buyers will then start trying to collect. After the debt is 7 years old the fine print on those letters from collectors SHOULD state that they can no longer sue you for the debt, OR, report it to a credit bureau. The extra fime print here is if you do so much as utter a word

A use-case that I can think of as to why Collection is not considered as Collection<?> is let say we have a instance of ArrayList

Now if the instance is of type ArrayList<Integer> or ArrayList<Double> or ArrayList<String>, you can add that type only(type checking). ArrayList<?> is not equivalent to ArrayList<Object>.

But with only ArrayList, you can add object of any type. This may be one of the reason why compiler is not considering ArrayList as ArrayList<?> (type checking).

One more reason could be backward compatibility with Java version that didn't have generics.

collections — Container datatypes — Python 3.8.5 documentation, Implement the non-optional (read-only) portion of java.util.Collection or A map entry is treated as an ordered collection of key and value. Note that An array map is such a map - it is simply implemented as an array of key val key val… If you paid the original creditor directly they most likely sent your money to the collection agency since legally they no longer have any right to collect that debt from you. The collection agency, having received this payment, then marked it as "paid and closed." Once it's closed it still has to age off of your credit report.

Data Structures, Simply assign an array literal to a variable or constant with the Set type specified. Set operations are not limited to use with other sets. Many sequence and collection operations return an array or a type-erasing collection wrapper instead � How is waste treated and disposed off? Waste management simply means the collection, transport, processing or disposal, managing and monitoring of waste materials to minimize its' consequences on humans and environment. There are several methods of managing all the various types of waste.

Set, Your first notification of a debt that's gone into collection is usually a letter received in In all likelihood, it will not be the same financial institution that originally issued If you feel the collection agency treated you poorly, verbally abused you,� members of the public who may want to know why certain material is or is not included in the collection, and the library's governing body (e.g. public library board, school board), which wants to have a consistent position regarding the library's collection. Responsibility for the Collection Development Policy

How to Handle Debt in Collections and Pay , It is not intended to provide a manual of how to conduct a survey, but rather to identify do not allocate participants into groups or vary the treatment they receive. have no research question at all and whose design simply is to collect a wide� Plaintiff offers no plausible explanation why a debt collection company would need or use a machine which had the capacity to dial or store randomly or sequentially generated numbers. In the absence of such an explanation, the Court simply would not allow the claim to move forward and dismissed it.

  • The question mark type is a bit tricky. It can be anything, e.g. Collection<?> x; Collection<?> y; doesn't mean x can be cast to y, because it could be different. e. g. x = new ArrayList<String>() ; y=new ArrayList<Integer>() ;
  • Inserting Collection<?> in the place of Collection would be tantamount to saying "don't worry about raw types, I've got you covered", whereas developers should actively avoid raw types.
  • I checked their source code and the date when it was first released. They were actually using generic types at that time, but not for that method for some reasons..
  • Collection is not equivalent to Collection<?>, it's closer to Collection<Object>...
  • You can't insert anything into a Collection<?> because you don't know what the correct type is.
  • To add to this: Collection<?> is not "a Collection of any types." It's "a Collection of some type, but that type is unknown." That's why you can't add non-null objects to it: the compiler is trying to tell you that to add those objects, you'd have to know that the Collection's <T> is a superclass of those objects (whereas you don't actually know anything about T, since you typed the Collection to <?>).
  • Just to prevent the impression that Collection<?> was kind of immutable: you can add null to a Collection<?>, further, you can pass a Collection<?> to a generic method expecting a Collection<T>, which then may add elements of type T, i.e. duplicates of elements already in the collection. As a practical example, you can invoke Collections.swap(list, ix1, ix2) with a List<?>. And, of course, removing elements always works. But you can not add an arbitrary Object to it like with the raw typed Collection.
  • FWIW: I think Scala's syntax for existential types may also help to understand the meaning of Collection<?>. Scala's equivalent of this is Collection[_]. And this is shorthand for Collection[T] forSome { type T }. The latter should be understood as This is a collection of instances of T. There is some specific type which is equal to T but which one is not known at this location in the code.
  • "the [unbounded wildcard <?>] is type safe, that is, on a compile level, it checks whether the items in the collection are of the same type." - Huh?! This is in contradiction to the rest of your answer. - Or maybe not quite; one could say that it is type safe in that it disallows any objects to be put into a collection of <?>.
  • The compiler performs no checks at all on what is in a generic collection. Rather, it performs checks on the (declared) types of the arguments to its constructors and methods, and on the expectations of the types of its methods' return values.
  • I really like this explanation, very basic, but on point and easily understandable +1 :)
  • Having said all that :) , An untyped Collection is actually more like a Collection<Object>. And you could in fact replace all legacy Collection usages with Collection<Object> as long as they haven't been replaced with more precise types (e.g. If you replace one usage with Collection<String> and another with Collection<Object> then again, you may break stuff).