Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is Collection not simply treated as Collection<?>

Tags:

java

generics

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:

principals.fromRealm(realmName).stream().collect(Collectors.toSet()); 

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

Eclipse:

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

javac:

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.

like image 939
dpr Avatar asked Apr 04 '19 11:04

dpr


People also ask

Why is HashMap not a collection in Java?

Because they are of an incompatible type. List, Set and Queue are a collection of similar kind of objects but just values where a Map is a collection of key and value pairs.

Can you instantiate a Collection?

The Java Collection interface ( java. util. Collection ) is one of the root interfaces of the Java Collection API. Though you do not instantiate a Collection directly, but rather a subtype of Collection, you may often treat these subtypes uniformly as a Collection.

Do you have the procedures in place for effective fee collection?

Many practices don’t have the procedures in place for effective fee collection from patients—especially not in person. Instead, many are willing to simply write off cash collections when the patient doesn’t pay. But with declining insurance reimbursements, successful over-the-counter collections are more important than ever

Why is effective collection so important?

Effective collection keeps you in business, allows you to hire quality employees, and most importantly, enables you to provide exceptional patient care. We’re empathetic, caring, good-natured practitioners, but that doesn’t mean we should let anyone bamboozle us into predicaments where we endanger our license, our practice, or our profession.

Are over-the-counter collections more important than ever?

But with declining insurance reimbursements, successful over-the-counter collections are more important than ever What are the implications of not collecting? In an APTA Podcast, Nancy White says, “studies show that the chance of collecting from a patient drops almost 20% as soon as the patient leaves the office.”

What is our collection?

Our Collection is made with an idea to make every outfit (day or night) interesting with message . Don’t think Why , Think Why Not. LINKS Home Shop About Us FAQ


1 Answers

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.

like image 148
Lino Avatar answered Oct 15 '22 09:10

Lino