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 Sets

Tony Brackins
Tony Brackins
28,674 Points

Set<String> vs HashSet<String>();

I'm following along with Java Sets and I'm still perplexed by the difference between why we can't just say:

HashSet<String> allHashTags = new HashSet<String>();


Set<String> allHashTags = new Set<String>();

3 Answers

Craig Dennis
Craig Dennis
Treehouse Teacher

That's because Set is the interface (or contract). You cannot initialize an interface. You can only initialize something that implements, or agrees to perform, the interface.

Make sense?

Thank you soon much Craig! I think this was the hardest concept to grasp for all of us!

Alright, I said I was a novice. So the HashSet is the object and the Set is the COLLECTIONS INTERFACE. I think I do a little better with this explanation:

Look them up on the java documentation at oracle (a really invaluable resource, get used to using it: http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html) and you'll see that ArrayList is a class object and List is a collections interface. What we're doing is taking the object that is limited in what it can do and placing inside a wrapper, like putting reeses into a wrapper at the factory. Before we use the interface, the array can only be one size and is limited in what it can do in terms of organization. Before we wrap the reeses we can't ship them overseas. When we wrap the ArrayList in the interface, the interface does the resizing and organizing work the ArrayList can't. But it's not made to work without an ArrayList, , just like you wouldn't want a reeses wrapper without a reeses inside. It's not made to hold peanut butter, that''s what the reeses is for. Again, if you have a question as to whether something is an object or a collections interface, check the oracle documentation. They're designed to do very different things.

Alright, I'm going to try to answer this for you, after having taken the whole class and gotten my feet wet working on a project and going through the java documentation, but bear in mind I'm still a novice. I say this because you CAN, from what I know, do the first action of blank Hashset into hashset I believe. Both of those are collection classes, so it's just like initiating a new blank instance, like we do in the constructor all the time. BUT why would you? Placing the collection class into the interface allows us to resize and manipulate it in ways usually not possible, especially with Arrays that can't be resized. But the second example here, Set to Set , makes no sense. Set, unlike HashSet, is an interface. It's mean to encapsulate a collection class so that we can manipulate it in ways we couldn't before. In other words, interfaces are wrappers. You can put an empty candy wrapper into an empty candy wrapper, but there'd still be no candy. I hope that was right and I didn't confuse you too much.

Also, a lot of this is cleared up as you look through the oracle java documentation and start using them on your own.