CodeGym /Courses /New Java Syntax /Practice using Set and Map

Practice using Set and Map

New Java Syntax
Level 14 , Lesson 7
Available

"I trust you've already learned about Set and Map. Here are some tasks to help reinforce your new knowledge."

14
Task
New Java Syntax, level 14, lesson 7
Locked
20 words that start with the letter "L"
You need to create a set of strings (using HashSet) and add 20 words that start with the letter "L".
14
Task
New Java Syntax, level 14, lesson 7
Locked
Greater than 10? You're not a good fit for us
Create a set of numbers (Set) and add 20 different numbers to it. Remove from the set all numbers greater than 10.
14
Task
New Java Syntax, level 14, lesson 7
Locked
Census
Create a Map and add ten entries that represent (last name, first name) pairs. Check how many people have the same first name or last name.
14
Task
New Java Syntax, level 14, lesson 7
Locked
We don't need repeats
Create a Map and add ten entries that represent (last name, first name) pairs. Remove people with the same first name.
14
Task
New Java Syntax, level 14, lesson 7
Locked
Remove all people born in the summer
In the Map, add ten entries that represent (last name, birth date) pairs. Remove from the map all people born in the summer.
14
Task
New Java Syntax, level 14, lesson 7
Locked
Only for the rich
Create a Map and add ten entries that represent (last name, salary) pairs. Remove from the map all people whose salary is below 500.
Comments (68)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
P.B.Kalyan Krishna Level 22, Guntur, India
1 May 2023
In the task that returns a count of the same first and last names, the last name is the key, and the first name is the value in the HashMap. We can have duplicate values in the value field(first name) but the key values must be unique(last name). Therefore a count of the same values in the key field always fetches a 1, because duplicate values are not allowed in the key field. I don't understand why they have written a function to fetch a count of the same values for the last name which evidently is the key field.
Spydrow Level 14, Germany, Germany
12 May 2022
Task only for the rich: why doesn't it work like this in the removeItemFromMap function? Map<String, Integer> copy = new HashMap<>(map); for(Integer salary : copy.values()){ if (salary < 500){ map.remove(salary); } } I'm not quite sure when to use Map.Entry and when to just use String/Integer in the iterator.
AfEi Level 19
26 May 2022
Map.Entry and String/Integer both works. solution one:

Map<String, Integer> copy = new HashMap<>(map);
        for (Map.Entry<String, Integer> pair : copy.entrySet()) {
            if (pair.getValue() < 500) {
                map.remove(pair.getKey());
            }
        }
solution two:

Map<String, Integer> copy = new HashMap<>(map);
        for (String s : copy.keySet()) {
            if (copy.get(s) < 500) {
                map.remove(s);
            }
        }
Dawn( #11020889) Level 37, Toronto, Canada
6 September 2022
map.remove( ) is used to remove the mapping of any particular KEY from the map,not the value.
Romant Level 15, Russia, Russian Federation
3 May 2022
Task "We don't need repeats". "Create a Map<String, Integer> and add ten entries that represent (last name, first name)." Its a Fault. Further in Requirements we see "The createMap() method must create and return a Map that has (String, String) elements and contains 10 entries."
whoseunassailable Level 28, India, India
31 January 2022
I am really confused with every problem while passing the arguments and stuff. Why do they also not want use to print any text to the terminal is something that was actually bothering me. Anyways all exercises were confusing for me coz i felt that the problems can be solved using one or two methods itself but providing so many different methods made me confuse about the entire scenario.
Paul Matros Level 22, Poland, Poland
25 February 2022
Those are relatively simple tasks, true. In real world programming there will be dozens if not hundreds functions in a program, there will be no way but to pass parameters. I believe tasks are constructed this way to practice this early
Anonymous #10853261 Level 21, United States
30 November 2021
Note that 'summer' is defined as June through August... nevermind the summer solstice
Craig McCollum Level 20, Belgrade, United Kingdom
27 October 2024
Was just wondering about that, thank you, it's almost like you've had to work that one out through trial and error.
Angelica Level 15, New York City, United States
11 September 2021
I struggled with the exercise "We don't need repeats" like I havent with any other so far. I guess I do need repeats :)
ImDevin Level 15, Old Town, United States
1 May 2021
the Hard tasks were really hard, but looked up a lot and learned a ton. I guess CG is trying to force us to learn like the programmers on the job? Even so, a little more instructions would've been better. Most difficulty was getting used to the multiple ways to iterate through the data structures and getting familiar with the details of the each methods. It was super helpful to read others' questions and those who commented/answered them. So, thanx everybody and happy coding! :)
11 April 2021
For "We don't need repeats", pay close attention to what is required! You must remove ALL people with the same first names. My downfall was that I was trying to keep the first person and remove the remaining ones with the same name. Wasted a lot of time on this one. Hope this helps someone.
Sinisa Level 11, Banja Luka, Bosnia and Herzegovina
9 March 2021
Boy what a complication for those data structures in comparison to Python.
Sinisa Level 11, Banja Luka, Bosnia and Herzegovina
9 March 2021
The tasks are not so hard, but poorly explained. If the problem is unclear or badly explained you can't develop a solution to it. I find myself increasingly wasting time with confusion stemming from very bad task requirements, instead of thinking about the algorithms to actually solve the particular issues.