• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Casting HashMap.keySet()

 
Rob Keefer
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();
 
Rob Keefer
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65229
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Rob Keefer
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It helps, thanks Bear.
- Rob
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic