GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Why are Generics necessary here? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Why are Generics necessary here?" Watch "Why are Generics necessary here?" New topic
Author

Why are Generics necessary here?

Matthew Alesi
Ranch Hand

Joined: Sep 13, 2006
Posts: 38
The following was my test part of a WhizLabs practice question.

import java.util.*;
class Tester {
public static void main(String[] args) {
Map<Integer, String> map = new HashMap<Integer, String>();
map.put(1, "Matt");
/*1*/Map.Entry m = map.entrySet().iterator().next();
/*2 Iterator<Map.Entry> it = map.entrySet().iterator(); */
}
}

In short, why does line 2 not compile, seeing especially as line 1 does compile, as it should? I was under the impression that it is okay to use raw types if you want, you just get warnings when you do. That's what you're doing here I think, creating an Iterator over the raw type Map.Entry, which should accept a Map.Entry<Some generic type>. I think. The only way it compiles is if you add generic parameters to Map.Entry inside the Iterator declaration. Where is the flaw in my logic?


-Matt
Current CS undergrad
SCJP 5.0
Matthew Alesi
Ranch Hand

Joined: Sep 13, 2006
Posts: 38
And while we're at it, I'd like to put this out there:



Why do all four add() calls compile? I would think that you can't add a One to an ArrayList<Comparable<Two>>....what is going on? I really thought I understood generics until this popped up.
Matthew Alesi
Ranch Hand

Joined: Sep 13, 2006
Posts: 38
Bump....please answer
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Originally posted by Matthew Alesi:
I would think that you can't add a One to an ArrayList<Comparable<Two>>...
Which of those four lines do you think is trying to add a One to an ArrayList<Comparable<Two>>?
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
Hi Matt,

Good question Matt.

Why first one compliles and second line fails?
import java.util.*;
class Tester {
public static void main(String[] args) {
Map<Integer, String> map = new HashMap<Integer, String>();
map.put(1, "Matt");
/*1*/Map.Entry m = map.entrySet().iterator().next();
/*2 Iterator<Map.Entry> it = map.entrySet().iterator(); */

}
}

1)Well first line worked because map.entrySet().iterator() returns iterator<E>
and iterator<E>.next() gives First EnumerationElement<K,V> which can be assigned to Map.Entry<k,v> or to support legacy code can also be assigned to Map.Entry with no type checking.

2)/*2 Iterator<Map.Entry> it = map.entrySet().iterator();
Second line fails. because map.entrySet().iterator() returns Iterator<E> where E is of type Map.Entry<Integer,String>. But the compile checking Iterator<Map.Entry> doesn't suits to Iterator<Map.Entry<Integer,String>>.
Hence it wont compile.

Lets Rock


Thanks & Regards, T.Srinivasan
SCWCD 1.4(89%), SCJP 5.0(75%)
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Originally posted by Matthew Alesi:
Bump....please answer


Consider this example.



In your example, where you wanted to know why a One object could be added to an ArrayList<Comparable<Two>>, it is something like line one in this example. You are basically assigning a reference of type Comparable<Two> to an object of raw type Comparable. This is allowed, but generates a compiler-warning because the compiler can't guarantee type safety.

In the other example, you can't play around much with what is in the angle brackets. You can see this in the other lines in the example. Even though you can assign a reference of type Comparable<Two> to an object of raw type Comparable, you cannot assign a reference of type ArrayList<Comparable<Two>> to an object of type ArrayList<Comparable>, nor can you assign a reference of type ArrayList<Comparable> to an object of type ArrayList<Comparable<Two>>.
megha joshi
Ranch Hand

Joined: Feb 20, 2007
Posts: 206
Hi,

My concepts of Generics and collections arent very strong.
Any good reference material that I can look into so that some day I can give answers to questions like this...
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
Hi Megha,

You can refer to generics chapter in Sun java 1.5 book.
its very good.
 
GeeCON Prague 2014
 
subject: Why are Generics necessary here?