CodeGym
Promotion
CodeGym University
Learning
Course
Tasks
Surveys & Quizzes
Games
Help
Schedule
Community
Users
Forum
Chat
Articles
Success stories
Activity
Reviews
Subscriptions
Light theme
Start learning now
  • All questions
// Java Poser
Level 18
Cincinnati
  • 17.03.2019
  • 1121views
  • 2comments

Desperate Attempt #2

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.Map; import java.util.ArrayList; /* We don't need repeats */ public class Solution { public static HashMap<String, String> createMap() { //write your code here HashMap<String, String> names = new HashMap<>(); names.put("John", "Doe"); names.put("John1", "Doe1"); names.put("John2", "Doe2"); names.put("John3", "Doe3"); names.put("John4", "Doe4"); names.put("John5", "Doe"); names.put("John6", "Doe1"); names.put("John7", "Doe2"); names.put("John8", "Doe3"); names.put("John9", "Doe4"); return names; } public static void removeFirstNameDuplicates(Map<String, String> map) { //write your code here HashMap<String, String> cloneMap = new HashMap<String, String>(map); map.clear(); for (Map.Entry<String, String> kv : cloneMap.entrySet()) { if (!map.containsValue(kv.getValue())) { map.put(kv.getKey(), kv.getValue()); } else { removeItemFromMapByValue(map, kv.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(map.values())) map.remove(pair.getKey()); } } public static void main(String[] args) { //removeFirstNameDuplicates(createMap()); } }
0
Comments (2)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Guadalupe Gagnon
Level 37 , Tampa, United States
17 March 2019, 15:25
It is probably failing because the verification system may be checking 'map' as the code runs. If this is the case then when you clear it at line 33 verification is probably automatically failing. Try replacing line 33 with creating a HashSet<String>. Then in the for each loop, use the same logic with the new HashSet instead of 'map' (checking if the set contains the value and adding it if it does not).
+2
// Java Poser
Level 18 , Cincinnati, United States
18 March 2019, 00:44
Thank you very much Guadalupe, I finally figured it out. took me a few days lmao
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.