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
Marek Pasierbek
Level 9
Poznań
  • 16.07.2020
  • 273views
  • 1comment

I don't have idea

Question about the task Shared last names and first names
Java Syntax,  Level 8,  Lesson 11
Under discussion

1. Create a Map<String, String> and add 10 people represented by (last name, first name) pairs.
2. Among these 10 people, let there be people who share the same first names.
3. Among these 10 people, let there be people who share the same last names.
4. Display the contents of the Map on the screen.

Requirements:
  • The program should display text on the screen.
  • The Solution class must have only three methods.
  • The createPeopleMap() method must create and return a Map with (String, String) elements. Also, add 10 people to the map.
  • In the createPeopleMap() method, you need to add people with the same last name.
  • In the createPeopleMap() method, you need to add people with the same first name.
  • The printPeopleMap() method should display all people in the Map. Display each value on a new line. The last name and first name should be separated by a space.
  • The main() method should call the createPeopleMap() method.
  • The main() method should call the printPeopleMap() method.
package com.codegym.task.task08.task0821; import java.util.HashMap; import java.util.Map; /* Shared last names and first names */ public class Solution { public static Map<String, String> createPeopleList() { Map<String, String> people = new HashMap<String, String>() {{ put("Aaa", "Brian"); put("Bbb", "Casey"); put("Ccc", "Eliot"); put("Ddd", "Justin"); put("Eee", "Mitch"); put("Fff", "Tyler"); put("Ggg", "Tom"); put("Hhh", "Nick"); put("Jjj", "Tyler"); put("Aaa", "Lucas"); }}; return people; } public static void printPeopleList(Map<String, String> map) { for (Map.Entry<String, String> s : map.entrySet()) { System.out.println(s.getKey() + " " + s.getValue()); } } public static void main(String[] args) { Map<String, String> map = createPeopleList(); printPeopleList(map); } }
0
Comments (1)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Gellert Varga
Level 23 , Szekesfehervar, Hungary
16 July 2020, 20:05
1.) The requirements talk about "createPeopleMap() method". But where is this in your program? I can't see it. 2.) Have you tried the program? Have you counted how many item-pairs there are in the output? (Do it!) We learned this: All the keys of the HashMap should be unique. If you try to put two or more equal keys, then the new one will overwrite the previous one.
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.