United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: JDK-5018254 (coll) 'remove' operation fails in tailMap of a null-key-allowing TreeMap
JDK-5018254 : (coll) 'remove' operation fails in tailMap of a null-key-allowing TreeMap

Details
Type:
Bug
Submit Date:
2004-03-23
Status:
Resolved
Updated Date:
2012-10-08
Project Name:
JDK
Resolved Date:
2005-09-04
Component:
core-libs
OS:
windows_2000
Sub-Component:
java.util:collections
CPU:
x86
Priority:
P4
Resolution:
Fixed
Affected Versions:
1.4.2
Fixed Versions:

Related Reports

Sub Tasks

Description
Name: gm110360			Date: 03/22/2004


FULL PRODUCT VERSION :
java version "1.4.2-beta"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2-beta-b19)
Java HotSpot(TM) Client VM (build 1.4.2-beta-b19, mixed mode)

FULL OS VERSION :
Microsoft Windows 2000 [Version 5.00.2195]

A DESCRIPTION OF THE PROBLEM :
I created a TreeMap that allows null keys.  The comparator puts the null key at the end of the map and uses natural ordering for all other keys.  If I create a new map with the 'tailMap' method, the 'remove' operation doesn't work properly with the new map.  When passing null to 'remove', the return value is always null instead of the associated value.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1. Create a Comparator that allows null keys
2. Create a TreeMap with the above comparator.
3. Add some key-value pairs to the map.  One key should be null with a non-null value.
4. Get a tailMap of the original Map.
5. Call 'remove(null)' on the tail map.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
The 'remove' call should return the value originally associated with 'null.
ACTUAL -
The 'remove' call always returns null.

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.util.*;

public class Test {
	private static final Comparator NULL_AT_END = new Comparator() {
		/**
		 * Allows for nulls.  Null is greater than anything non-null.
		 */
		public int compare(Object pObj1, Object pObj2) {
			if (pObj1 == null && pObj2 == null) return 0;
			if (pObj1 == null && pObj2 != null) return 1;
			if (pObj1 != null && pObj2 == null) return -1;
			return ((Comparable) pObj1).compareTo(pObj2);
		}
	};


	public static void main(String[] pArgs) {
		SortedMap m1 = new TreeMap(NULL_AT_END);
		m1.put("a", "a");
		m1.put("b", "b");
		m1.put("c", "c");
		m1.put(null, "d");

		SortedMap m2 = new TreeMap(m1);

		System.out.println(m1.lastKey());
		System.out.println(m1.get(m1.lastKey()));
		System.out.println(m1.remove(m1.lastKey())); // should (and does) output 'd'

		m2 = m2.tailMap("b");

		System.out.println(m2.lastKey());
		System.out.println(m2.get(m2.lastKey()));
		System.out.println(m2.remove(m2.lastKey())); // should output 'd' but outputs 'null'
	}
}
---------- END SOURCE ----------
(Incident Review ID: 192542) 
======================================================================

                                    

Comments
EVALUATION

The reporter is correct; this is a bug.

###@###.### 2004-03-23

Will be fixed as part of JSR166 maintenance.
###@###.### 2005-03-09 03:07:15 GMT
                                     
2004-03-23



Hardware and Software, Engineered to Work Together