• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Casting HashMap.keySet()

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?

 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It helps, thanks Bear.
- Rob
 
reply
    Bookmark Topic Watch Topic
  • New Topic