aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generic Collections Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Generic Collections" Watch "Generic Collections" New topic
Author

Generic Collections

Harikrishna Gorrepati
Ranch Hand

Joined: Sep 23, 2010
Posts: 422
Hi,

Could you please explain what's happening at line 2, 3 and 5..



OCPJP 6.0-81% | Preparing for OCWCD
http://www.certpal.com/blogs/cert-articles | http://sites.google.com/site/mostlyjava/scwcd |
Ram Narayan.M
Ranch Hand

Joined: Jul 11, 2010
Posts: 247

Note : Entry is the sub-interface of Map.

In Line 2, Iterator here is used for iterating the set of Map.Entry objects which gives the Collection view of "Integer" key and "String" value. So... It can iterate the eneric Map <Integer,String> EntrySet iterator.

Line 3 leads to compiler error... Since in "Iterator<Entry<Integer, String>>", "Map.Entry" is not specified...

Line 5, Iterator "Map.Entry" gives the Collection view of "Object " key and "Object" value.. When Generic Map <Integer,String> EntrySet iterator is assigned to this Iterator reference, it will lead to type mismatch error...


SCJP 6 [SCJP - Old is Gold]
Harikrishna Gorrepati
Ranch Hand

Joined: Sep 23, 2010
Posts: 422
Thanks Ram. If I see the source code for Entry, I see it defined in Map interface. Do you know why I cannot see in JavaDocs. One more thing is, Line 3 has NO errors.
Ram Narayan.M
Ranch Hand

Joined: Jul 11, 2010
Posts: 247

Please check

http://download.oracle.com/javase/6/docs/api/java/util/Map.html

you can find "Map.Entry" in the JavaDoc...

And then, Line 3 has no error... Ok.... I think you have used "import static java.util.Map.Entry;"... So it will not end up in error since this kind of declaration eases to use static members without the extension of class to which they belong to
Harikrishna Gorrepati
Ranch Hand

Joined: Sep 23, 2010
Posts: 422
Hi Ram, I am looking for Entry. The link you sent has Map.Entry
Ram Narayan.M
Ranch Hand

Joined: Jul 11, 2010
Posts: 247

There is no such Outer interface called "Entry"... Ok... Is Entry used in this code a user-defined Interface?...
Harikrishna Gorrepati
Ranch Hand

Joined: Sep 23, 2010
Posts: 422
Hi Ram,

I imported import java.util.Map.Entry;

It is NOT user defined interface.
On Line 4, Entry is used i.e., Entry<Integer, String>
Iterator<Entry<Integer, String>> i2 = map.entrySet().iterator();

In the Original Map.class interface, I see the following code..Looks like Entry interface is defined in Map interface..All I want to know is to see this "Entry" interface in JavaDocs

interface Entry<K,V> {
/**
* Returns the key corresponding to this entry.
*
* @return the key corresponding to this entry
* @throws IllegalStateException implementations may, but are not
* required to, throw this exception if the entry has been
* removed from the backing map.
*/
Ram Narayan.M
Ranch Hand

Joined: Jul 11, 2010
Posts: 247

Since you have imported too detailed import java.util.Map.Entry;

You can use Entry interface without the extension "Map."... So Line2 and Line3 are both same. Thats all...
Harikrishna Gorrepati
Ranch Hand

Joined: Sep 23, 2010
Posts: 422
Ram Narayan.M wrote:Since you have imported too detailed import java.util.Map.Entry;

You can use Entry interface without the extension "Map."... So Line2 and Line3 are both same. Thats all...


Hi Ram, I got your point. What I am trying to figure out is where I can see "Entry" interface in JavaDocs
Ram Narayan.M
Ranch Hand

Joined: Jul 11, 2010
Posts: 247

http://download.oracle.com/javase/6/docs/api/java/util/Map.Entry.html

This is Entry Interface Document which contains abstract methods...
Harikrishna Gorrepati
Ranch Hand

Joined: Sep 23, 2010
Posts: 422
Ram Narayan.M wrote:http://download.oracle.com/javase/6/docs/api/java/util/Map.Entry.html

This is Entry Interface Document which contains abstract methods...


This is for Map.Entry. I am looking for just Entry alone or something related to just Entry not along with Map. May be my question is dump ??
Ram Narayan.M
Ranch Hand

Joined: Jul 11, 2010
Posts: 247

Hey Dude,

Since you have imported too detailed import java.util.Map.Entry;

You can use Entry interface without the extension "Map."...

I am again insisting you that there is no outer Entry interface not related to Map
Harikrishna Gorrepati
Ranch Hand

Joined: Sep 23, 2010
Posts: 422
Ram Narayan.M wrote:Hey Dude,

Since you have imported too detailed import java.util.Map.Entry;

You can use Entry interface without the extension "Map."...

I am again insisting you that there is no outer Entry interface not related to Map


Hi Ram,

I am not asking on how to use Entry (or) Map.Entry.
I think I got the answer ..Maybe For inner classes/interfaces Sun/Oracle will not provide any documentation.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generic Collections
 
Similar Threads
Generics -really very hard question
re: Iteration in a Map
Iterator in Generics
Why are Generics necessary here?
how to store map value in string array