HashMap of cats

  • 2
  • Locked
Here we've decided to make a collection of cats, so we're going to prepare a proper CATalog. In this program, someone has already gone to the trouble of writing a Cat class with a name field. Our task is to create a HashMap and add 10 cats to it. That's enough to start. As you probably already guessed, the cat's name should be used as the key.
You can't complete this task, because you're not signed in.
Comments (20)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Khongpak Phupkdee
Level 15 , Chiangrai, Thailand
27 April 2021, 11:05
Two days for this mission. However, now I passed. Easy you but not easy me but it helps me know what I missing.
Level 25 , Bexleyheath, United Kingdom
14 September 2020, 19:53
Yeah, ok. Did it. But that is well hard. Not an easy task at this stage at all.
Roman Grygorczuk
Level 19 , Cracow, Poland
11 October 2020, 15:29
The hardest was for me that in the HashMap they are using object Cat. It was very confusing how to act with that object. In the training section there is only String and Integer types.
Level 25 , Bexleyheath, United Kingdom
12 October 2020, 09:19
Yes. Don't worry, that all becomes clearer once you wade through learning about how objects, classes and all that work. I remember that it was a total mystery to me at this stage, but I can assure you it eventually becomes clearer.
Karas Java Developer
9 September 2020, 18:30
Just follow Captain John Squirrels Orders. Create a HashMap inside the addCatsToMap Method, this will be empty at the begining. Second Iterate through the string already passed to the method and at the same time you are iterating through, create cat objects (just give it any name and pass the string as parameter), then put them into the empty HashMap you created and finaly return the HasMap, now should be full of kittens. The rest is already created no need to print anything it is already there.
Petyo Spasov
Level 11 , Asenovgrad, Bulgaria
24 July 2020, 13:12
Hi , I can't understend when and how the toString() method is calling? @Override public String toString() { return name != null ? name.toUpperCase() : null; }
Gellert Varga
Level 23 , Szekesfehervar, Hungary
7 February 2021, 21:34
The toString() method will automatically come into action in this codeline:
System.out.println( . . . . .  + pair.getValue());
The System.out.println() command has a built-in toString() by default in Java. The pair.getValue() really is a cat object, and the toString() will be invoked on this object - in the background, invisibly. But not the original toString() will be executed, but this "@Override" version.:)
John Squirrels Website Admin at CodeGym
14 June 2020, 08:43
Extra theory on Maps can be found in these articles: HashMap: what kind of map is this? Map Interface and its implementations. Java HashMap, LinkedHashMap and TreeMap Examples This particular task can be completed by first creating an empty HashMap inside addCatsToMap(), then in a for each or a standard for loop iterating through the passed array of cats, adding each cat name from the array as a key and a new Cat() as a value to our newly created map. Lastly, return the map, which should now should be filled.
Level 9 , Ottawa, Canada
9 June 2020, 18:15
Based on the lessons provided, in what world is this A: An easy question and B: properly explained? Real close to ditching my subscription. There are way too many of these poorly explained tasks.
Level 41
2 November 2020, 11:45
Haha I know what you mean, but don't give up! Try and look for explanations or ask for help! I've read through some comments here and actually understood better!
Nickolas Johnson
Level 8 , St. Louis, United States of America
4 April 2020, 16:37
OK so the explanation here is garbage so I'm going to drop a few hints. In a HashMap the KEY has to be unique NOT THE VALUE. Since you read in an array of unique values they can all act as the KEY and VALUE. That's what they seem to want even though they don't tell you the value should be the same as the key.
Level 13 , Dubrovnik, Croatia
31 July 2020, 12:32
Yeah thank you, it worked after I put the same thing for both
Angel Stefan
Level 23 , Sibiu, Romania
22 November 2021, 17:01
OMFG!!! This is true but they don t explain it!
Ashish RajAnand
Level 13 , Bhilai , India
12 March 2020, 02:54
How toString method work ? After s.put(String,Cat) here how Cat return Name i can't understand
oli blaustrom
Level 18 , Luzern, Switzerland
21 December 2019, 18:30
Extremely weird. Foreach loop works but normal for loop doesn't.
Level 15 , Old Town, United States
27 April 2021, 18:18
actually, it does work with both types of for loop.
Fadi Alsaidi
Level 34 , Carrollton, TX, USA
21 November 2019, 00:43
What happen when a new object gets created with a reference to a variable name? for example from the task above: I can say New Cat cat1 = new Cat("Simba") or I can instantiated a new Cat in the map value with our assigning it to cat1 . How is the world would i know how to pull any new cat and I dot have its referenced variable? like if I wanted to pull the new Cat "Simba", how would i go about doing that when i don't have the Cat object reference like cat1? I hope that makes sense