CodeGym /Courses /New Java Syntax /Practice with collections

Practice with collections

New Java Syntax
Level 14 , Lesson 3
Available

"Hi, Amigo. I'd like to give you a couple of tasks about collections:"

14
Task
New Java Syntax, level 14, lesson 3
Locked
Set of plants
Create a HashSet with String elements. Add the following strings to it: watermelon banana cherry pear cantaloupe blackberry ginseng strawberry iris potato Display the contents of the collection, each element on a new line. Watch how the order of the entries has changed.
14
Task
New Java Syntax, level 14, lesson 3
Locked
Map of 10 pairs
Create a HashMap, and add 10 string pairs: watermelon - melon, banana - fruit, cherry - fruit, pear - fruit, cantaloupe - melon, blackberry - fruit, ginseng - root, strawberry - fruit, iris - flower, potato - tuber. Display the contents of the collection, each element on a new line. Example output:
14
Task
New Java Syntax, level 14, lesson 3
Locked
Values on the screen!
There is a HashMap. 10 different strings have been added to it. Display a list of values on the screen, each element on a new line.
14
Task
New Java Syntax, level 14, lesson 3
Locked
HashMap of Objects
There is a HashMap. 10 different object pairs have been added to it. Display the contents of the collection, each element on a new line. Example output: (here we just show one line): Sim - 5
14
Task
New Java Syntax, level 14, lesson 3
Locked
Display a list of keys
There is a HashMap. 10 different strings have been added to it. Display a list of keys on the screen, each element on a new line.
14
Task
New Java Syntax, level 14, lesson 3
Locked
Map of cats
There is a Cat class with a String name. Create a Map. Add 10 cats to the collection. The cat's name should be used as the key. Display the result on the screen, each element on a new line.
Comments (54)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Fadhil Radhian Level 18, Semarang, Indonesia
27 March 2023
these quizzes are fun !
SHAAHEEN Level 10, Gothenburg, Sweden
24 March 2021
practice makes perfect
ImDevin Level 15, Old Town, United States
27 April 2021
actually, practice makes permanent. So, you better make sure what you practice is correct! :)
ashley819 Level 9, Washington DC, United States
25 June 2020
He always says, "a couple of tasks." I don't think that means what he thinks it means...
Chrisantus Makokha Level 32, Nairobi, Kenya
23 January 2021
🤣🤣
Oliver Heintz Level 18, Mustang, United States
17 June 2020
What is the difference between using

Set<String> set = new HashSet<String>();
and

HashSet<String> set = new HashSet<String>();
Gaelle Gilles Level 15, New York City, United States
3 January 2021
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.
Oliver Heintz Level 18, Mustang, United States
5 January 2021
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!
Gaelle Gilles Level 15, New York City, United States
5 January 2021
No worries about the progress you've made. Everyone learns at a different pace. At least you're still at it.
remote87 Level 18, Sofia, Bulgaria
10 January 2021
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
Steven Level 11, White Plains, United States
11 January 2021
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.
Oliver Heintz Level 18, Mustang, United States
12 January 2021
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 Smith Level 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.
Le Phan Level 14, Véry, France
5 February 2022
They have fixed that. Set<String> set = new HashSet<>(); now works like the charm. However, older declarations were failed like mysteries.
Jonaskinny Level 25, Redondo Beach, United States
17 February 2022
I just wish they used the bytecode to validate (which would be identical in either case)
Oliver Heintz Level 18, Mustang, United States
17 June 2020
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?
Regina Level 47, Florida, United States
18 June 2020
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.
Oliver Heintz Level 18, Mustang, United States
20 June 2020
Sweet action. I don't ever plan on skipping tasks, so I'll have plenty of dark matter!
Rod Johnston Level 13, Melbourne, Australia
15 June 2020
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 Smith Level 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.
Jonaskinny Level 25, Redondo Beach, United States
17 February 2022
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.
BlueJavaBanana Level 37
15 April 2020
https://www.youtube.com/watch?v=KyUTuwz_b7Q Absolutley amazing video on hash tables and has functions.
Patrick Denney Level 17, Livermore, United States
6 May 2020
Great video. Really explains Hash Tables in a manner I can understand.
Ted404 Level 11, Dubai, United Arab Emirates
3 April 2020
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.
Nickolas Johnson Level 8, St. Louis, United States of America
8 April 2020
Bhuvana Kumar Level 14, Mumbai, India
27 June 2020
Thanks this link explained me the Key in Hashmap
9 March 2020
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!
Austeja Level 10, Kaunas, Lithuania
13 February 2020
Not that easy as labeled;/ :D