Can anyone please explain what exactly is happening here? I feel like I totally don't understand what is happening here. I've passed the previous levels with good understanding. However, HashMaps and in general collections seem really confusing to me. I would also appreciate any additional links for readings or any detailed explanation would be greatly appreciated as well.
package 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) {
HashMap<String, Cat> map2 = new HashMap<>();
for(int i = 0; i < cats.length; i++){
map2.put(cats[i], new Cat(cats[i]));
}
return map2;
}
public static class Cat {
String name;
public Cat(String name) {
this.name = name;
}
@Override
public String toString() {
return name != null ? name.toUpperCase() : null;
}
}
}