/** * Constructs a new, empty tree map, using the natural ordering of its * keys. All keys inserted into the map must implement the { @link * Comparable} interface. Furthermore, all such keys must be * mutually comparable: k1.compareTo(k2) must not throw * a ClassCastException for any keys k1 and * k2 in the map. If the user attempts to put a key into the * map that violates this constraint (for example, the user attempts to * put a string key into a map whose keys are integers), the * put(Object key, Object value) call will throw a * ClassCastException. */ public TreeMap() { comparator = null; }
/** * Associates the specified value with the specified key in this map. * If the map previously contained a mapping for the key, the old * value is replaced. * * @param key key with which the specified value is to be associated * @param value value to be associated with the specified key * * @return the previous value associated with key, or * null if there was no mapping for key. * (A null return can also indicate that the map * previously associated null with key.) * @throws ClassCastException if the specified key cannot be compared * with the keys currently in the map * @throws NullPointerException if the specified key is null * and this map uses natural ordering, or its comparator * does not permit null keys */ public V put(K key, V value) { Entryt = root; if (t == null) { // TBD: // 5045147: (coll) Adding null to an empty TreeSet should // throw NullPointerException // // compare(key, key); // type check root = new Entry (key, value, null); size = 1; modCount++; return null; } int cmp; Entry parent; // split comparator and comparable paths Comparator cpr = comparator; if (cpr != null) { do { parent = t; cmp = cpr.compare(key, t.key); if (cmp < 0) t = t.left; else if (cmp > 0) t = t.right; else return t.setValue(value); } while (t != null); } else { if (key == null) throw new NullPointerException(); Comparable k = (Comparable ) key; do { parent = t; cmp = k.compareTo(t.key); if (cmp < 0) t = t.left; else if (cmp > 0) t = t.right; else return t.setValue(value); } while (t != null); } Entry e = new Entry (key, value, parent); if (cmp < 0) parent.left = e; else parent.right = e; fixAfterInsertion(e); size++; modCount++; return null; }
一个报错的场景:
package map;import java.util.Map;import java.util.TreeMap;public class TreeMapDemo { public static void main(String[] args) { MaptreeMap = new TreeMap (); for (int i = 0; i < 10; i++) { treeMap.put(i + Math.round(Math.random() * 10), new Staff(i)); } System.out.println(treeMap); System.out.println("result:" + treeMap.containsKey(10)); System.out.println("result:" + treeMap.containsKey(1l)); }}class Staff { private int yearOfWorking; public Staff(int yearOfWorking) { this.yearOfWorking = yearOfWorking; } @Override public String toString() { return "Staff{" + "yearOfWorking=" + yearOfWorking + '}'; }}
{5=Staff{yearOfWorking=1}, 7=Staff{yearOfWorking=5}, 9=Staff{yearOfWorking=7}, 10=Staff{yearOfWorking=9}, 11=Staff{yearOfWorking=2}, 13=Staff{yearOfWorking=8}}Exception in thread "main" java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Integer at java.lang.Integer.compareTo(Integer.java:35) at java.util.TreeMap.getEntry(TreeMap.java:328) at java.util.TreeMap.containsKey(TreeMap.java:209) at map.TreeMapDemo.main(TreeMapDemo.java:13) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)