CodeGym
Promotion
CodeGym University
Learning
Courses
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
CodeGym/Help with Java Tasks/Removal of firstname failing need help
Alex Balandinos
Level 18
Lusaka
  • 13.03.2019
  • 1429views
  • 2comments

Removal of firstname failing need help

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.Iterator; import java.util.Map; import java.util.Map.Entry; import java.util.HashSet; import java.util.Set; /* We don't need repeats */ public class Solution { public static HashMap<String, String> createMap() { //write your code here HashMap<String, String> map = new HashMap<String, String>(); map.put("Joshi","Shreyas"); map.put("Shah","Sujata"); map.put("Gala","Anjali"); map.put("Ahmed","Sana"); map.put("Maheshwari","Uday"); map.put("Sharma","Uday"); map.put("Verma","Haresh"); map.put("Garival","Omkar"); map.put("Kapur","Sanjiv"); map.put("Pandya","Laxmi"); return map; } public static void removeFirstNameDuplicates(Map<String, String > map) { //write your code here Set<String> keys = map.keySet(); // The set of keys in the map. Iterator<String> keyIter = keys.iterator(); while (keyIter.hasNext()) { String key = keyIter.next(); String value1 = map.get(key); //System.out.println(key + "\t" + value1); String nextValue = map.get(key); if (!value1.equals(nextValue)) { map.remove(key);} else{ removeItemFromMapByValue(map, value1); } } } 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) { } }
0
Comments (2)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
masz3k
Level 16 , Lodz, Poland
21 March 2019, 20:36
Additionally you are searching only if name occurs on just next position - what if there is repeat somewhere else in the code? Let's say Name1 Name2 Name1 You have bad conception
+1
Guadalupe Gagnon
Level 37 , Tampa, United States
19 March 2019, 20:51
You are setting value1 and value2 to the same exact thing every time, which will result in removeItemFromMapByValue being called every single loop cycle, which will remove everything from your map every time.
0
Learn
  • Registration
  • Java Course
  • Help with Tasks
  • Pricing
  • 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 a Java developer’s career.
Follow us
Interface language
English
Deutsch Español हिन्दी Français Português Polski বাংলা 简体中文 मराठी தமிழ் Italiano Bahasa Indonesia 繁體中文 Nederlands 日本語 한국어 Bulgarian Danish Hungarian Basa Jawa Malay Norwegian Romanian Swedish Telugu Thai Українська Filipino Turkish Azərbaycan Русский Vietnamese
Programmers Are Made, Not Born © 2025 CodeGym
MastercardVisa
Programmers Are Made, Not Born © 2025 CodeGym