// put cats in HashMap
for (int i = 0; i < cats.length; i++)
{
Cat cat[i] = new Cat();
catMap.put(cats[i], cat[i]);
}
I know you can't do this but this is where my thinking is. Do you need to create a new Cat each time and put it in the hashmap. What do need to put for the second argument of put.
Thanks in advancepackage com.codegym.task.task08.task0803;
import java.util.HashMap;
import java.util.Map;
/*
HashMap of cats
*/
public class Solution {
public static void main(String[] args) throws Exception {
String[] cats = new String[]{"Tiger", "Missy", "Smokey", "Marmalade", "Oscar", "Snowball", "Boss", "Smudge", "Max", "Simba"};
HashMap<String, Cat> map = addCatsToMap(cats);
for (Map.Entry<String, Cat> pair : map.entrySet()) {
System.out.println(pair.getKey() + " - " + pair.getValue());
}
}
public static HashMap<String, Cat> addCatsToMap(String[] cats) {
//write your code here
// initialize hashmap
HashMap<String, Cat> catMap = new HashMap<String, Cat>();
// put cats in HashMap
for (int i = 0; i < cats.length; i++)
{
Cat cat = new Cat();
catMap.put(cats[i], cat);
}
// display contents of HashMap
// for (Map.Entry<String, Cat> cat : catMap.entrySet())
// {
// String key = cat.getKey();
// Cat value = cat.getValue();
// System.out.println(key + " - " + value);
// }
}
public static class Cat {
String name;
public Cat(String name) {
this.name = name;
}
@Override
public String toString() {
return name != null ? name.toUpperCase() : null;
}
}
}