aspose file tools*
The moose likes Beginning Java and the fly likes Please explan how set.iterator() gets called when set is of type interface? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Please explan how set.iterator() gets called when set is of type interface?" Watch "Please explan how set.iterator() gets called when set is of type interface?" New topic
Author

Please explan how set.iterator() gets called when set is of type interface?

M Zia
Greenhorn

Joined: Nov 24, 2011
Posts: 13
In the following code set.iterator() gets called and there is no class shown implementing the Set Interface.
How and why is this happening. can anyone explain?

import java.util.*;
class HashMapDemo {
public static void main(String args[]) {
// Create a hash map
HashMap hm = new HashMap();
// Put elements to the map
hm.put("John Doe", new Double(3434.34));
hm.put("Tom Smith", new Double(123.22));
hm.put("Jane Baker", new Double(1378.00));
hm.put("Todd Hall", new Double(99.22));
hm.put("Ralph Smith", new Double(-19.08));
// Get a set of the entries
Set set = hm.entrySet();
// Get an iterator
Iterator i = set.iterator();

// Display elements
while(i.hasNext()) {
Map.Entry me = (Map.Entry)i.next();
System.out.print(me.getKey() + ": ");
System.out.println(me.getValue());
}
System.out.println();
// Deposit 1000 into John Doe's account
double balance = ((Double)hm.get("John Doe")).doubleValue();
hm.put("John Doe", new Double(balance + 1000));
System.out.println("John Doe's new balance: " +
hm.get("John Doe"));
}
}
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

i'm guessing HashMap implements it. i had a similar situation with AudioClip, where the Applet did the same thing.


SCJP
Visit my download page
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61432
    
  67

HashMap returns an instance of an object that implements the Set interface. What actual class is being used for that object is of little interest. That's the beauty of interfaces. All we care about is that the interface is implemented. We don't need to know anything else about the implementing class.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3678
    
  16
Randall Twede wrote:i'm guessing HashMap implements it.

No. The object returned from Hashmap.entrySet() implements it. What the type of this object is is irrelevant - all you need to know is that it implements the Set interface and that the Set will contain Map.Entry<K,V> objects, where K is the key type of the HashMap and V is the value type.


Joanne
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3678
    
  16
Slightly off topic. but you should learn about generics. This will allow the compiler to do better type checking and also mean you get rid of those casts when iterating over the Set
M Zia
Greenhorn

Joined: Nov 24, 2011
Posts: 13
Bear, Joanne,
If hashmap.entrySet() returns an object which is an instance of a class that implements the Set interface, how would one go about finding that class that is implementing the interface. Without knowing that class, the code i pasted does not make sense to me since we do not know that class listing.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61432
    
  67

M Zia wrote:Bear, Joanne,
If hashmap.entrySet() returns an object which is an instance of a class that implements the Set interface, how would one go about finding that class that is implementing the interface. Without knowing that class, the code i pasted does not make sense to me since we do not know that class listing.


Why do you need to know the specific class? Everything that you need to know is part of the Set interface. Of what additional use would it be to know what the implementing class is?
M Zia
Greenhorn

Joined: Nov 24, 2011
Posts: 13
I did a getClass().getName on the "set" interface implementing object and this is what came back:
name of class: java.util.HashMap$EntrySet

What does it mean???
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61432
    
  67

It means that the implementing class is java.util.HashMap$EntrySet. But, again, this is not useful knowledge to your code. It doesn't need to know this. All it needs to know is that the Set interface is implemented.

(If it's the $ that's confusing you, that means that EntrySet is an inner class of HashMap.)
M Zia
Greenhorn

Joined: Nov 24, 2011
Posts: 13
Bear,
I need to know because otherwise i am living in a vacuum. I just need to know what class that object that is returned belongs to and read the class implementation code. I would like to know what the iterator() method in the set interface's implementation looks like. Without having access to the class , it appears that the object is coming out of nowhere.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61432
    
  67

Well that's exactly what interfaces are for! You don't know or care where the class comes from, or even what it is.

You may want to know for academic purposes, but the code doesn't need to know, and shouldn't know!
M Zia
Greenhorn

Joined: Nov 24, 2011
Posts: 13
Where is the documentation that states that EntrySet is an inner class of HashMap class that implements the Set interface and the iterator method??? How can i make progress when it becomes impossible to get documentation?? In the documenation for the entrySet() method for HashMap it does not state that an object instance of EntrySet inner class is being returned. It just states Set is being returned. Where do we find this??? any book/docs???
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18660
    
    8

M Zia wrote:In the documenation for the entrySet() method for HashMap it does not state that an object instance of EntrySet inner class is being returned. It just states Set is being returned.


Yes. That's the documentation you need. Am I the fourth or fifth person to mention that you only need to know that you are getting something which implements the Set interface? You don't seem to have been paying any attention to those other people so probably you won't pay any attention to me either. But really, if you're going to ask a question on a forum, you should spend some time reading the answers. So far all you have done is to reject them.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61432
    
  67

I'll give it one more try before moving on:

  • The documentation for the Set interface is all the documenatation you need.
  • The fact that the Set is implemented by a non-public inner class of HashMap is not something you need to know.
  • The documentation for the HashSet$EntrySet class is not something you need.


  • You have yet to answer the question: why do you think you need documentation on anything beyond the Set interface?
    Mike Simmons
    Ranch Hand

    Joined: Mar 05, 2008
    Posts: 3018
        
      10
    If you want to learn how things work internally, just to satisfy your own curiosity, then you can usually find these sorts of answers by looking at the source code. There should be a file in the top level of your JDK installation, titled either src.zip, or perhaps src.jar. This contains the source code used to implement most of the standard library classes you will use, including java.util.HashSet and the other classes it relies on. You can learn many things about how things work, by examining this code.

    However, I do agree very much with the others when they say you should not need to look at this, in order to write code that uses HashSet or most other library classes. The documentation specifically avoids such details (usually) because they don't want you beecomse too dependend on details that might change in future implementations. It can be fun or edfucationsal to learn those implementation details, but you really should avoid thinking you need them in order to write code that uses those classes. Use the javadoc, and that should be enough.

    (Now if you're working outside the standard JDK libraries, you may well find some code that is very poorly documented. In this case, consulting the source code can be your first and best way to understand what the code does. But that is gnerally not true for JDK code.)
    Joanne Neal
    Rancher

    Joined: Aug 05, 2005
    Posts: 3678
        
      16
    M Zia wrote:In the documenation for the entrySet() method for HashMap it does not state that an object instance of EntrySet inner class is being returned. It just states Set is being returned. Where do we find this??? any book/docs???

    The reason it doesn't tell you the type of the class is that it gives the Java developers the freedom to change what class is returned. Just because the version of Java that you are running returns a java.util.HashMap$EntrySet, it doesn't mean that every version of Java will return an instance of that class. As long as the class that is returned implements the Set interface, that is all that matters. As Bear said, that is the beauty of interfaces. You don't need to know what the class type is and you should absolutely not rely on it being a particular type because there is nothing to stop it being changed to a different type in another version of Java.
    M Zia
    Greenhorn

    Joined: Nov 24, 2011
    Posts: 13
    ALL, Thanks for your support on this forum. It means a lot.
    So for future reference, if a method's return type is an interface, we know that it is returning an object that is an instance of a class that implements that interface and we can assume all the methods listed in the interface are implemented in the class which can be accessed via the object instance invocation, set.iterator() in this particular case. This is was my source of confusion which caused a while(1) loop to spew forth from my end. Sorry for that.
    Rob Spoor
    Sheriff

    Joined: Oct 27, 2005
    Posts: 19720
        
      20

    There is one thing you should be careful of. Although all methods are implemented, not all methods will work. Calling add on a Set or Collection is allowed to throw an UnsupportedOperationException; this is mentioned in the Javadocs. For instance, you can't add anything directly to the Set returned by entrySet().That means that your code may still throw an exception even though it compiles. The documentation should inform you that certain operations (adding, removing, etc) are not allowed.


    SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
    How To Ask Questions How To Answer Questions
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Please explan how set.iterator() gets called when set is of type interface?