Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
  • Piet Souris
  • Frits Walraven
  • Carey Brown

TreeMap delete

Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can I delete a key and its mapping from a TreeMap,
the remove function just updates the value of the key to null.
More specific: after using remove(key) I should have one element less in the treemap.
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please read our JavaRanch naming policy and select an approriate username; "Muru" is insufficient because we require a first and last name. Thanks.
As for your question, I don't see what the problem is. The following code shows a TreeMap before and after a key is removed:
<code><pre>import java.util.*;

class Test {

public static void main(String[] args) throws Exception {
Map map = new TreeMap();
map.put("a", "A");
map.put("b", "B");
System.out.println("Removed b");

static void showMap(Map map) {
Set entries = map.entrySet();
Iterator itr = entries.iterator();
while (itr.hasNext()) {
Map.Entry entry = (Map.Entry);
System.out.println("key: " + entry.getKey()
+ " value: " + entry.getValue());
The output is:
key: a value: A
key: b value: B
Removed b
key: a value: A
It looks to me like the entry for b is gone entirely - I tested this using SDK 1.2.2 and SDK 1.3. If you're getting other results, I'd like to see your code.
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic