Since Set is a collection interface, HashSet--among other sets--extends from the Set class. So it basically inherits all the information that the Set class has. Because of that, you don't have to write HashSet<String> set = new HashSet<String>(); out completely. Instead, you can just write Set<String> set = new HashSet<String>(); and it will produce the same results. Since Set is an interface, you can not create a new object of said interface. The different types of Sets are HashSet, Sorted Set, and LinkedHashSet. Look at this website as it explains in further detail what a Set is: https://docs.oracle.com/javase/tutorial/collections/interfaces/set.html.
I completely understand now. Had I waited just a little longer I'd have been able to answer my own question. I can't believe that I've only managed to get to level 14 since I asked that question 6 months ago. I'm only leveling up like once a month. Curse real life getting in my way!
Hey, Oliver, imagine this: deserted island, somewhere in the pacific or near the indian coast, non - stopping superspeed wi-fi, all-you-can-eat buffet, palmshade (or un umbrella, let's not get too cheeky :D ), top-notch gaming laptop, no cellphones, no one else on the island.... and CodeGym all day long. Hell yeah! :D
good to hear that things get sorted out as you progress in level. sometimes I get worried when I don't understand something right away, but I just have to remember that more will be revealed as I progress.
Yeah, you've just got to stick with it. There's been a number of times when I've felt like I've just hit a wall and I walk away for days, or sometimes weeks, at a time. But it always seems to click in the end. If I'm not on CodeGym I'm still reading a little bit in one of those books they recommended early on so I'm never really "done" with JAVA. I at least try to keep the skills I've learned fresh in my head.
Justin SmithLevel 41, Greenfield, USA, United States
12 July 2021
"Instead, you can just write Set<String> set = new HashSet<String>(); and it will produce the same results."
Except that it will fail validation for this reason alone (at least for the first task). In order to pass validation, you must use HashSet on both sides of the = sign.
It is costing more and more dark matter to unlock lessons. Are we going to ever run into a situation where we've completed the tasks we've been given, but we don't have the dark matter to keep going?
No, this situation won't happen. As you reach higher levels, you are rewarded with the bigger amount of the Dark Matter units. Therefore, the bigger amount it takes to unlock the next lesson.
For the HashSet of plants, don't declare and initialise the HashSet variable as described on the previous page, like this:
Set <String> plants = new HashSet<String>();
This won't pass validation.
You need to declare the variable as a HashSet:
HashSet<String> plants = new HashSet<String>();
It's a pity the validation and lesson don't agree.
Justin SmithLevel 41, Greenfield, USA, United States
12 July 2021
Yep, that was the only thing I had "wrong". I would say this should be considered a bug, since it's not wrong and following the lesson example will fail you.
That's sad. You SHOULD declare using narrowest type possible, so you can change the implementing class down the road and nothing else.
Set<String> set = new HashSet<>(); // good to go if we switch to new MyFancyHashSet as long as it implements Set interface.
HashSet<String> set = new HashSet<>(); // not good, we are stuck with only HashSet and its descendants. This is actually a bad default declaration method to teach imo, and ignores a huge part of java's type management.
I actually did get much from the previous lessons about HashMaps and in general, about collections. Is this normal? we cannot figure out how to create a simple HashMap without checking the other sources.
For those who got stuck:
You will be returning name of the cat & name of the cat in upper case (x10).
This is one of the few times that it is really difficult to understand what is being asked of you.
Good luck!
GO TO FULL VERSION