JavaRush
Course
Tasks
Games
Help
Subscriptions
Success stories
Chat
Forum
Articles
Friends
Activity
Question
  • Reviews
  • About us
Start
Start learning
  • All questions
Alex Balandinos
Level 18
Lusaka
  • 13 March 2019
  • 573views
  • 2comments

Removal of firstname failing need help

Question about the task We don't need repeats
,  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
  • Game Projects
Community
  • Users
  • Articles
  • Forum
  • Chat
  • Success Stories
  • Activity
About
  • About us
  • Contacts
  • Reviews
  • Press Room
  • CodeGym for EDU
  • FAQ
  • Support
Video
  • Top 3 features of CodeGym
  • "Course" section
  • About the "Tasks" section
  • "Games" section
Follow us
Interface language
© 2021 CodeGym Programmers Are Made, Not Born
© 2021 CodeGym Programmers Are Made, Not Born