Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2006

Tip: Looking for answers? Try searching our database.

Generics and use of extends in HashMap

Thread view: 
David Harrigan - 02 Oct 2006 13:51 GMT
Hi,

First the code:

public interface A {
}

public class B implements A {
}

public class C {

   public static void main(String[] args) {
        new C().doIt();
   }

   public void doIt() {
       Map<String, ? extends A> a = new HashMap<String, A>();
       a.put("A Test", new B());
   }
}

This won't compile, specifically at line "a.put..." because it says (in
eclipse):

"The method put(String, capture-of ? extends A) in the type Map<String,
capture-of ? extends A> is not applicable for the argumentes (String,
B)"

Why? I'm confused...

I thought I was saying that I want a map that takes a key with any
value that extends (or implements) A, and since B implements A it
should be happy....

Why doesn't this work?

Thanks

-=david=-
Chris Brat - 02 Oct 2006 14:13 GMT
Hi,

This works in your heirarchy because of normal object conversion ( B to
A ) - you dont need to specify the ? extends clause.

Chris

public void doIt() {
        Map<String, A> a = new HashMap();
        a.put("A Test", new B());
}
Chris Brat - 02 Oct 2006 14:18 GMT
See this article "Using and Programming Generics in J2SE 5.0" at
http://java.sun.com/developer/technicalArticles/J2SE/generics/
Thomas Hawtin - 02 Oct 2006 14:54 GMT
> public interface A {

> public class B implements A {

>     public void doIt() {
>         Map<String, ? extends A> a = new HashMap<String, A>();
>         a.put("A Test", new B());
>     }

From Map<String, ? extends A> a, we know all the values of a extend A.
But there may be further constraints such that not all instances of A
can be values of a.

Suppose class C implements A. Then we could have had:

        Map<String, C> map = new HashMap<String, C>();
        Map<String, ? extends A> a = map;
        a.put("A Test", new B()); // ILLEGAL
        C c = map.get("A Test");

We have assigned a B to a C variable. Oops.

What you can write is:

        Map<String, ? super A> a = new HashMap<String, A>();

With a declared as such, it could either be a Map<String,A> or
Map<String,Object>. So we can definitely add an instance of B
(implements A). However, when we get an object from the map, we only
know that it is some kind of Object.

Tom Hawtin
David Harrigan - 02 Oct 2006 15:30 GMT
Thanks Thomas (and others) for your reply:

I still don't *get* it however.

If C is implmenting A, then has the same type as B, therefore
C and B would be of the same type (A), so why would assigning
B to C cause a problem?

I read also the <? super A> as "anything who's super is A", so
why would this be different that <? extends A> as "anything that
is of type A"?

-=david=-

> > public interface A {
>
[quoted text clipped - 28 lines]
>
> Tom Hawtin
Thomas Hawtin - 02 Oct 2006 16:20 GMT
> If C is implmenting A, then has the same type as B, therefore
> C and B would be of the same type (A), so why would assigning
> B to C cause a problem?

An instance of C has type C and an instance of B has a type B. You can't
have a reference of type C referencing a B, or vice versa. A reference
of type A can reference either B or C. Suppose A was not abstract, then
a reference of type B or C could not reference an instance of type A.

> I read also the <? super A> as "anything who's super is A", so
> why would this be different that <? extends A> as "anything that
> is of type A"?

"Anything that is a super of A." T super MyType, means that T is a
supertype of MyType. T extends MyType means that T is a subtype of
MyType. They aren't the best keywords in the world, but there was a
desire not to add any extra.

Tom Hawtin


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.