Java Java Data Structures Exploring the Java Collection Framework Sets

Tony Brackins
Tony Brackins
26,244 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>();

or

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

3 Answers

Nicolas Hampton
Nicolas Hampton
42,763 Points

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.

Nicolas Hampton
Nicolas Hampton
42,763 Points

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

Nicolas Hampton
Nicolas Hampton
42,763 Points

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.

Craig Dennis
STAFF
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?

Nicolas Hampton
Nicolas Hampton
42,763 Points

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