Since TreeMaps are sorted by keys, the object for key has to be able to compare with each other, that's why it has to implement Comparable interface. For example, you use String as key, because String implements Comparable interface. Let's change the Dog, and make it comparable. classD...
理解:当TreeMap中的Key为类对象的时候,你需要自己定义将这个类实现Comparable接口. 4. HashSet Example HashSet<Dog> dset =newHashSet<Dog>(); dset.add(newDog(2)); dset.add(newDog(1)); dset.add(newDog(3)); dset.add(newDog(5)); dset.add(newDog(4)); Iterator<Dog> iterator =dset.ite...
HashSet、LinkedHashSet、TreeSet 以下内容基于jdk1.7.0_79源码: 关于HashSet.LinkedHashSet.TreeSet Set接口的实现类,最大特点是不允许出现重复元素: HashSet:基于HashMap实现,一个性能相对较好的Set: LinkedHashSet:基于LinkedHashMap实现,一个保存了插入顺序的Set: TreeSet:基于TreeSet实现,一个实现了排序的Set...