Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Data Structures Exploring the Java Collection Framework Maps

Chris Yoon
Chris Yoon
2,363 Points

Return type of keySet. Is it a hash, tree, or linked set?

What type of set is returned (if any). If it doesn't return a implementation, but instead an interface, how is it so because I thought that when declaring something like Set<String> allAcronyms = acronyms.ketSet(), the right side needs to return an implementation and not an interface.

Also is an implementation of an interface a class?

Thanks! (:

Chris Yoon
Chris Yoon
2,363 Points

Or does it depend on the map's implementation that keySet() is called on?

1 Answer

Enrique Munguía
Enrique Munguía
14,311 Points

The documentantion does not explicitly states what implementation is returned because it does not matter, what matters is that a Set is returned. Also check out this note from the docs:

The set supports element removal, ... , via the Iterator.remove, Set.remove, removeAll, retainAll, and clear operations. It does not support the add or addAll operations.

If you need an arbitrary type of Set you can create it yourself:

Set<String> set = map.keySet();
HashSet<String> hashSet = new HashSet<String>(set);