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
  • 26.07.2020
  • 397views
  • 1comment

can somebody tell me why this is wrong?

Question about the task Deep cloning of a map
Java Multithreading,  Level 1,  Lesson 8
Under discussion


Provide the ability to create a deep clone of a Solution object.
The data in the users map should also be cloned.
Don't forget about the equals and hashCode methods needed to correctly add User elements to the HashMap.

Requirements:
  • The Solution class should support the Cloneable interface.
  • The User class should support the Cloneable interface.
  • The User class must correctly implement the clone method.
  • The Solution class must correctly implement the clone method.
package com.codegym.task.task21.task2107; import java.util.LinkedHashMap; import java.util.*; /* Deep cloning of a map */ public class Solution { public static void main(String[] args) { Solution solution = new Solution(); solution.users.put("Hubert", new User(172, "Hubert")); solution.users.put("Zapp", new User(41, "Zapp")); Solution clone = null; try { clone = solution.clone(); System.out.println(solution); System.out.println(clone); System.out.println(solution.users); System.out.println(clone.users); } catch (CloneNotSupportedException e) { e.printStackTrace(System.err); } } @Override public Solution clone() throws CloneNotSupportedException { Solution solution = (Solution) super.clone(); Map<String, User> copyMap = new LinkedHashMap<>(); solution.users = copyMap; for(Map.Entry<String, User> pair : users.entrySet()){ copyMap.put(pair.getKey(), pair.getValue()); } return solution; } protected Map<String, User> users = new LinkedHashMap(); public static class User { int age; String name; public User(int age, String name) { this.age = age; this.name = name; } @Override public boolean equals(Object o){ if(this == o) return true; if(!(o instanceof User)) return false; User user = (User)o; return Objects.equals(user.age, age) && Objects.equals(user.name, name); } @Override public int hashCode(){ return Objects.hash(name, age); } public Object clone()throws CloneNotSupportedException{ User user = (User)super.clone(); return user; } } }
0
Comments (1)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
hidden #10625598
Level 23
26 July 2020, 10:50
ah fuck i forgot to implement cloneable
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.