ContainsKey on treemap throws NPE : So i have a comparator that i defined for my TreeMap to sort it by values,
but when i use containsKey(anyKey) on a key that it does not contain it doesn't return false, but it throws a NullPointerException. Now what i don't understand is why it goes in my comparator for the containsKey() method and how i could find a solution... I've been stuck on this for a couple of hours and any help would be appriciated.

Here is the code :

    public class test {
    
    	public static void main(String[] args) {
    		
    		HashMap<Integer, Integer> map = new HashMap<>();
    		map.put(1,2);
    		map.put(2, 3);
    		Comparator<Integer> comp = new ValueComparator<Integer, Integer>(map);
    		TreeMap<Integer, Integer> tm = new TreeMap<Integer, Integer>(comp);
    		tm.putAll(map);
    		System.out.println(tm.containsKey(1)); // returns true
    		System.out.println(tm.containsKey(3)); //Throws NPE
    	}
    	
    
    }
    
    class ValueComparator<K, V extends Comparable<V>> implements Comparator<K>{
    	 
    	HashMap<K, V> map = new HashMap<K, V>();
     
    	public ValueComparator(HashMap<K, V> map){
    		this.map.putAll(map);
    	}
     
    	@Override
    	public int compare(K s1, K s2) {
    		
    		return map.get(s1).compareTo(map.get(s2));//descending order	
    	}
    }