Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why are Generics necessary here?

 
Matthew Alesi
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Matthew Alesi
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bump....please answer
 
Paul Clapham
Sheriff
Posts: 21297
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 557
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 206
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 557
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Megha,

You can refer to generics chapter in Sun java 1.5 book.
its very good.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic