ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/LastKeyOfSubMap.java
Revision: 1.5
Committed: Thu Jan 15 18:34:19 2015 UTC (9 years, 3 months ago) by jsr166
Branch: MAIN
CVS Tags: HEAD
Changes since 1.4: +0 -1 lines
Log Message:
delete extraneous blank lines

File Contents

# Content
1 /*
2 * Written by Doug Lea with assistance from members of JCP JSR-166
3 * Expert Group and released to the public domain, as explained at
4 * http://creativecommons.org/publicdomain/zero/1.0/
5 */
6
7 // Adapted from bug report 5018354
8 import java.util.*;
9
10 public class LastKeyOfSubMap {
11 private static final Comparator NULL_AT_END = new Comparator() {
12 /**
13 * Allows for nulls. Null is greater than anything non-null.
14 */
15 public int compare(Object pObj1, Object pObj2) {
16 if (pObj1 == null && pObj2 == null) return 0;
17 if (pObj1 == null && pObj2 != null) return 1;
18 if (pObj1 != null && pObj2 == null) return -1;
19 return ((Comparable) pObj1).compareTo(pObj2);
20 }
21 };
22
23 public static void main(String[] pArgs) {
24 SortedMap m1 = new TreeMap(NULL_AT_END);
25 m1.put("a", "a");
26 m1.put("b", "b");
27 m1.put("c", "c");
28 m1.put(null, "d");
29
30 SortedMap m2 = new TreeMap(m1);
31
32 System.out.println(m1.lastKey());
33 System.out.println(m1.get(m1.lastKey()));
34 Object m1lk = m1.remove(m1.lastKey());
35 if (m1lk == null)
36 throw new Error("bad remove of last key");
37
38 m2 = m2.tailMap("b");
39
40 System.out.println(m2.lastKey());
41 System.out.println(m2.get(m2.lastKey()));
42 Object m2lk = m2.remove(m2.lastKey());
43 if (m2lk == null)
44 throw new Error("bad remove of last key");
45 }
46 }