CodeGym
Promotion
CodeGym University
Learning
Course
Tasks
Surveys & Quizzes
Games
Help
Schedule
Community
Users
Forum
Chat
Articles
Success stories
Activity
Reviews
Subscriptions
Light theme
Question
  • Reviews
  • About us
Start
Start learning
Start learning now
  • All questions
hidden #10625598
Level 23
  • 21.07.2020
  • 269views
  • 3comments

can somebody tell me my mistake?

Question about the task We don't need repeats
Java Syntax,  Level 8,  Lesson 8
Under discussion


Create a Map<String, String> and add ten entries that represent (last name, first name) pairs.
Remove people with the same first name.

Requirements:
  • The program should not display text on the screen.
  • The program should not read values from the keyboard.
  • The createMap() method must create and return a HashMap that has (String, String) elements and contains 10 entries.
  • The removeFirstNameDuplicates() method must remove from the map all people who have the same first name.
  • The removeFirstNameDuplicates() method must call the removeItemFromMapByValue() method.
package com.codegym.task.task08.task0817; import java.util.HashMap; import java.util.*; /* We don't need repeats */ public class Solution { public static HashMap<String, String> createMap() { //write your code here HashMap<String, String> map = new HashMap<>(); map.put("can","can"); map.put("casn","can"); map.put("van","dan"); map.put("seen","hen"); map.put("ice","ren"); map.put("gone","form"); map.put("tree","seen"); map.put("use","horse"); map.put("horse","gone"); map.put("cap","dont"); return map; } public static void removeFirstNameDuplicates(Map<String, String> map) { //write your code here HashMap<String, String> copy = new HashMap<String, String>(map); for(Map.Entry<String, String> paar : copy.entrySet()){ removeItemFromMapByValue(map, paar.getValue()); } } public static void removeItemFromMapByValue(Map<String, String> map, String value) { HashMap<String, String> copy = new HashMap<String, String>(map); for (Map.Entry<String, String> pair : copy.entrySet()) { if (pair.getValue().equals(value)) map.remove(pair.getKey()); } } public static void main(String[] args) { HashMap names = createMap(); System.out.println(names); removeFirstNameDuplicates(names); System.out.println(names); } }
0
Comments (3)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Nouser
Level 36 , Germany
21 July 2020, 14:10
you remove all map entries with iterating through the map and passing each map value to the removeItemFromMapByValue method and you display text on the screen
0
hidden #10625598
Level 23
21 July 2020, 14:16
but i have to give the removeItembyValue method a list with a value, if i remove it before what does the method do then? or am i getting it wrong?
0
Nouser
Level 36 , Germany
21 July 2020, 14:41
What? You give that method a reference to a map and a value. And all map entries matching the String value get removed from the map. So if you iterate through the map and send in each iteration the map reference and the actual value to the removeItemFromMapByValue method all will get deleted. Unfortunately it throws an exception as you know (deleting in a collection while looping over it). So you have to fix this two problems.
0
Learn
  • Registration
  • Java Course
  • Help with Tasks
  • Pricing
  • Game Projects
  • Java Syntax
Community
  • Users
  • Articles
  • Forum
  • Chat
  • Success Stories
  • Activity
  • Affiliate Program
Company
  • About us
  • Contacts
  • Reviews
  • Press Room
  • CodeGym for EDU
  • FAQ
  • Support
CodeGym CodeGym is an online course for learning Java programming from scratch. This course is a perfect way to master Java for beginners. It contains 1200+ tasks with instant verification and an essential scope of Java fundamentals theory. To help you succeed in education, we’ve implemented a set of motivational features: quizzes, coding projects, content about efficient learning and Java developer’s career.
Follow us
Interface language
Programmers Are Made, Not Born © 2023 CodeGym
MastercardVisa
Programmers Are Made, Not Born © 2023 CodeGym
This website uses cookies to provide you with personalized service. By using this website, you agree to our use of cookies. If you require more details, please read our Terms and Policy.