1 |
// from bug report 5018354 |
2 |
import java.util.*; |
3 |
|
4 |
public class LastKeyOfSubMap { |
5 |
private static final Comparator NULL_AT_END = new Comparator() { |
6 |
/** |
7 |
* Allows for nulls. Null is greater than anything non-null. |
8 |
*/ |
9 |
public int compare(Object pObj1, Object pObj2) { |
10 |
if (pObj1 == null && pObj2 == null) return 0; |
11 |
if (pObj1 == null && pObj2 != null) return 1; |
12 |
if (pObj1 != null && pObj2 == null) return -1; |
13 |
return ((Comparable) pObj1).compareTo(pObj2); |
14 |
} |
15 |
}; |
16 |
|
17 |
|
18 |
public static void main(String[] pArgs) { |
19 |
SortedMap m1 = new TreeMap(NULL_AT_END); |
20 |
m1.put("a", "a"); |
21 |
m1.put("b", "b"); |
22 |
m1.put("c", "c"); |
23 |
m1.put(null, "d"); |
24 |
|
25 |
SortedMap m2 = new TreeMap(m1); |
26 |
|
27 |
System.out.println(m1.lastKey()); |
28 |
System.out.println(m1.get(m1.lastKey())); |
29 |
Object m1lk = m1.remove(m1.lastKey()); |
30 |
if (m1lk == null) |
31 |
throw new Error("bad remove of last key"); |
32 |
|
33 |
m2 = m2.tailMap("b"); |
34 |
|
35 |
System.out.println(m2.lastKey()); |
36 |
System.out.println(m2.get(m2.lastKey())); |
37 |
Object m2lk = m2.remove(m2.lastKey()); |
38 |
if (m2lk == null) |
39 |
throw new Error("bad remove of last key"); |
40 |
} |
41 |
} |