TreeMap in Java

Advertisements

Prev Tutorial Next Tutorial

TreeMap in Java

A TreeMap contains values based on the key. It implements the NavigableMap interface and extends AbstractMap class.

Points to Remember

  • It contains only unique elements.
  • It cannot have null key but can have multiple null values.
  • It is same as HashMap instead maintains ascending order.
TreeMap

Example of TreeSet

import java.util.*;
class TreeMapDemo
{
public static void main(String args[])
{  
   
  TreeMap<Integer,String> tm=new TreeMap<Integer,String>();  
  tm.put(1,"Deo");  
  tm.put(2,"zen");  
  tm.put(3,"porter");  
  tm.put(4,"piter");  
  
 for(Map.Entry m:tm.entrySet())
 {  
   System.out.println(m.getKey()+" "+m.getValue());  
 }
 }  
}  

Output

1,Deo
2,zen  
3,porter  
4, piter

Prev Tutorial Next Tutorial

Advertisements

Buy This Ad Space @$20 per Month, Ad Size 600X200 Contact on: hitesh.xc@gmail.com or 9999595223

Pure VPN Privide Lowest Price VPN Just @ $1.65. Per Month with Non Detected IP Lowest Price Non Detected IP VPN

Magenet is best Adsense Alternative here we earn $2 for single link, Here we get links ads. Magenet

Download Projects

Google Advertisements


Buy Websites 9999595223

Buy College Projects with Documentation Contact on whatsapp 9999595223. Contact on: hitesh.xc@gmail.com or 9999595223 Try this Keyword C++ Programs

Advertisements