import java.util.*;
public class MapsOfPersons {
    public static void printMap(Map<String, Person> map) {
        // http://www.mkyong.com/java/how-to-sort-a-map-in-java/
        // entrySet ret Set<Map.Entry<K,V>>
        for (Map.Entry<String, Person> entry : map.entrySet()) {
            System.out.println("\tKey : " + entry.getKey()
                    + " Value : " + entry.getValue());
        }
    }
    public static void printMapKeys(Map<String, Person> map) {
        for (String key : map.keySet()) { // keySet ret set<K>
            System.out.println("\tKey : " + key);
        }
    }
    public static void printMapValues(Map<String, Person> map) {
        for (Person p : map.values()) { // values() ret Collection<V>
            System.out.println("\tValue : " + p);
        }
    }
    public static void main(String args[]) {
        Map<String, Person> map = new HashMap<>();
        System.out.println("Person 12345678Z: " + map.get("12345678Z"));
        map.put("12345678Z", new Person("Pepe", "Garcia Perez"));
        map.put("12341234A", new Person("Pere", "Guerrero"));
        map.put("12345678Z", new Person("Manel", "Guerrero")); // sobreescriu
        // Podiem haver utilitzat: boolean containsKey(Object key)
        // Per veure si ja existia
        System.out.println("Person 12345678Z: " + map.get("12345678Z"));
        System.out.println("Keys:");
        printMapKeys(map);
        System.out.println("Values:");
        printMapValues(map);
        System.out.println("HashMap:");
        printMap(map);
        Map<String, Person> treeMap = new TreeMap<>(map);
        // Per TreeMap<K,V> K ha de ser comparable, V no cal
        System.out.println("TreeMap (ordenat per Key):");
        printMap(treeMap);
    }
}
