Collections

Java TreeMap Example Code For All Methods

Table of Contents

Program

import java.util.TreeMap;
public class TreeMapExamples
{
    public static void main(String args[]) 
    {
        TreeMap<Integer, String> tm= new TreeMap<Integer, String>();
        tm.put(2, "Where");
        tm.put(1, "is");
        tm.put(3, "my");
        tm.put(4, "car");
        System.out.println("Tree Map:"+tm);
        
        //size()
        int x=tm.size();						        
        System.out.println("\nThe size is: "+x);   
        System.out.println("\nChecking first entry");

        //firstEntry()				
        System.out.println("\nFirst entry is: "+ tm.firstEntry());
        System.out.println("\nChecking last entry");				
       
        //lastEntry()
        System.out.println("\nLast entry is: "+tm.lastEntry());
        
        //clone()
        TreeMap treemapclone = (TreeMap)tm.clone();				
        System.out.println("\nOriginal map: "+ tm);
        System.out.println("\nCloned map: "+ treemapclone);
       
        //remove()
        tm.remove(3);							        
        System.out.println("\nAfter removing:"+tm);
    }
}

Output

Tree Map:{1=is, 2=Where, 3=my, 4=car}

The size is: 4

Checking first entry

First entry is: 1=is

Checking last entry

Last entry is: 4=car

Original map: {1=is, 2=Where, 3=my, 4=car}

Cloned map: {1=is, 2=Where, 3=my, 4=car}

After removing:{1=is, 2=Where, 4=car}

Description

public int size()

Returns the number of key-value mappings in this map.

Specified by:

size in interface Map<K,V>

Overrides:

size in class AbstractMap<K,V>

Returns:

the number of key-value mappings in this map

public Map.Entry firstEntry()

Returns a key-value mapping associated with the least key in this map, or null if the map is empty.

Specified by:

firstEntry in interface NavigableMap<K,V>

Returns:

an entry with the least key, or null if this map is empty

Since:

1.6

public Map.Entry lastEntry()

Returns a key-value mapping associated with the greatest key in this map, or null if the map is empty.

Specified by:

lastEntry in interface NavigableMap<K,V>

Returns:

an entry with the greatest key, or null if this map is empty

Since:

1.6

public Object clone()

Returns a shallow copy of this TreeMap instance. (The keys and values themselves are not cloned.

Overrides:

clone in class AbstractMap<K,V>

Returns:

a shallow copy of this map

public V remove​(Object key)

Removes the mapping for this key from this TreeMap if present.

Specified by:

remove in interface Map<K,V>

Overrides:

remove in class AbstractMap<K,V>

Parameters:

key – key for which mapping should be removed

Returns:

the previous value associated with key, or null if there was no mapping for key. (A null return can also indicate that the map previously associated null with key.)

Throws:

ClassCastException – if the specified key cannot be compared with the keys currently in the map
NullPointerException – if the specified key is null and this map uses natural ordering, or its comparator does not permit null keys