aspose file tools*
The moose likes Beginning Java and the fly likes Casting HashMap.keySet() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Casting HashMap.keySet()" Watch "Casting HashMap.keySet()" New topic
Author

Casting HashMap.keySet()

Rob Keefer
Greenhorn

Joined: Feb 18, 2002
Posts: 26
The following code compiles, but I get a runtime error:
Exception in thread "main" java.lang.ClassCastException: java.util.HashMap$1
at Statistics.main(Statistics.java:27)
Line 27 is the fourth from the bottom where I try to cast the keySet into a HashSet.
Will someone explain to me what I am doing wrong?

Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Rob,
The keySet method of HashMap returns a Set not HashSet. Because HashSet is a subclass of Set a HashSet is a Set but a Set is not a HashSet. However, there is a constructor in the HashSet that takes a Collection as an argument. Since the Set interface inherits from Collection a Set is a Collection. So something like this should work:
HashSet nSet = new HashSet(hm.keySet());
hope that helps
[ April 12, 2002: Message edited by: Dave Vick ]

Dave
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Errr... why make a HashSet at all? You've already got a perfectly good Set - why spend processor time to make another? Just use
Set nSet = hm.keySet();


"I'm not back." - Bill Harding, Twister
Rob Keefer
Greenhorn

Joined: Feb 18, 2002
Posts: 26
Originally posted by Jim Yingst:
Errr... why make a HashSet at all? You've already got a perfectly good Set - why spend processor time to make another? Just use
Set nSet = hm.keySet();


Ok, this is great. However, I have another question then...
I thought an interface was simply a definition, and didn't have any functionality behind it. I would have never thought to even try this. In the API documentation Set is an interface. So, can you explain why using Set works in this case? Is this a feature of the Util package, or am I missing something about interfaces in general?
Thanks for the help.
- Rob
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61437
    
  67

Rob,
Yes, you are missing one of the fundamental uses of interfaces. You are correct in that an interface merely defines a set of functionality that a class can "implement". So something like:

makes no sense.
However, in the case

the right-hand-side of the assignment is referencing an object that has declared that it implements the Set interface. Since that declaration is a contract that the class has entered into to implement all methods declared by the interface, it can be treated as such.
Therefore, in the latter example nSet can be treated as an "instance" of the interface because it has declared that it implements that interface.
This is why the interface concept is so useful and valuable. With interfaces, many disparate object types can be treated as a common interface that they all implement (without having to resort to using inheritance for this purpose). The actual details of the implementation is hidden.
I frequently think of the Duck analogy: it looks like a Duck, acts like a Duck, and quacks like a Duck, so treat it like a Duck.
hth,
bear
[ April 15, 2002: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Rob Keefer
Greenhorn

Joined: Feb 18, 2002
Posts: 26
It helps, thanks Bear.
- Rob
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Casting HashMap.keySet()