The main difference between Map and SortedMap contracts is its approach to comparing of keys.
Map contract is to compare keys with equals method.
SortedMap contract is to compare keys with provided or class natural compator.
From javadoc of Comparable<T> interface: This ordering is referred to as the class's natural ordering, and the class's compareTo method is referred to as its natural comparison method.
Objects that implement this interface can be used as keys in a sorted map or as elements in a sorted set, without the need to specify a comparator.
Why NULL can not be set as value in Set?
The natural ordering for a class C is said to be consistent with equals if and only if e1.compareTo(e2) == 0 has the same boolean value as e1.equals(e2) for every e1 and e2 of class C. Note that null is not an instance of any class, and e.compareTo(null) should throw a NullPointerException even though e.equals(null) returns false.
No comments:
Post a Comment