From afe09e32debe87faf4eaa17d5ca9d02285c42cd7 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Wed, 30 Oct 2013 13:23:42 -0500 Subject: [PATCH] Add a 'comparator' field to TreeMap This will be needed for Java-compatible serialization of tree maps. Note that the field should be null when the TreeMap uses the default comparator. Signed-off-by: Johannes Schindelin --- classpath/java/util/TreeMap.java | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/classpath/java/util/TreeMap.java b/classpath/java/util/TreeMap.java index 6028637c38..29ea5972c0 100644 --- a/classpath/java/util/TreeMap.java +++ b/classpath/java/util/TreeMap.java @@ -11,24 +11,32 @@ package java.util; public class TreeMap implements Map { - private TreeSet> set; + private final Comparator comparator; + private transient TreeSet> set; - public TreeMap(final Comparator comparator) { - set = new TreeSet(new Comparator>() { - public int compare(MyEntry a, MyEntry b) { - return comparator.compare(a.key, b.key); - } - }); + public TreeMap(Comparator comparator) { + this.comparator = comparator; + initializeSet(); } - public TreeMap() { - this(new Comparator() { + private void initializeSet() { + final Comparator comparator = this.comparator != null ? + this.comparator : new Comparator() { public int compare(K a, K b) { return ((Comparable) a).compareTo(b); } + }; + set = new TreeSet(new Comparator>() { + public int compare(MyEntry a, MyEntry b) { + return comparator.compare(a.key, b.key); + } }); } + public TreeMap() { + this(null); + } + public String toString() { return Collections.toString(this); }