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

Practice using Set and Map

Java Syntax
Level 8 , Lesson 8
Available

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

Comments (69)
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.
Agusia Level 22, United Kingdom
18 April 2025
Two errors: 1. map.remove(...) expects a key (String), but you’re passing the whole map.entrySet(). 2. modifying the map during a for‑each iteration: for (Map.Entry<String, Integer> entry : map.entrySet()) { if (entry.getValue() < 500) { // here you remove from map… } } This throws a ConcurrentModificationException. To remove without error, you must use an iterator or the removeIf() method.#
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 1, Queens: git commit -m 'get the money', United States
30 November 2021
Note that 'summer' is defined as June through August... nevermind the summer solstice
Craig McCollum Level 8, 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.