SortedMap (interface) example in java Collection Framework

In this example I will show you how you can use SortedMap interface in your Java application.

Ads

Tutorials   
How to read properties file in Java? Generate random numbers in Java Cloneable Interface in Java What is the flaw with the Stack class? List In Java Java Stack Example HashSet In Java Enumeration in java How to generate random number in java Iterator in java Queue in java Transient Java Keyword clone method in Java Java Queue example Logger in Java Java current date - How to get current date in Java Collection : LinkedList Example Collection : ArrayList Example Random alpha numeric string of fixed size Java List Iterator Example The Hashtable Class The BitSet class Java Vector Java Enumeration Data Structures in Java Java Transient Variable Java Date Format Example Java Properties File Example Map.Entry Java Interface Java Property File java preferences,Change the value of Preferences by user Retrieving a Preference Node Java hasNext Write Keys and Values to the Properties file in Java Get All Keys and Values of the Properties files in Java Properties file in Java Properties file in Java Determining If a Preference Node Contains a Specific Value Change the value of Preferences by user Clone method example in Java Implementing a Simple Event Notifier Determining When a Preference Node Is Added or Removed Listening for Changes to Preference Values in a Preference Node Importing Preferences Finding a Preference in a Preference Tree Retrieving the Parent and Child Nodes of a Preference Node Determining If a Preference Node Exists Removing a Preference Node Creating a Preference Node Getting the Maximum Size of a Preference Key and Value
Ads

SortedMap (interface) example in java Collection Framework

     

In this example I will show you how you can use SortedMap interface in your Java application.

A SortedMap is a map that maintains its entries in ascending order, sorted according to the keys' natural order, or according to a Comparator provided at SortedMap creation time. (Natural order and Comparators are discussed in the section on Object Ordering In addition to the normal Map operations, the Map interface provides operations for:

  • Range-view: Performs arbitrary range operations on the sorted map.
  • Endpoints: Returns the first or last key in the sorted map.
  • Comparator access: Returns the Comparator used to sort the map (if any).

Here is the code of program :

import java.util.*;

public class SortedMapExample{

  public static void main(String[] args) {

  SortedMap map = new TreeMap();

  // Add some elements:
  map.put("2""Two");
  map.put("1""One");
  map.put("5""Five");
  map.put("4""Four");
  map.put("3""Three");

  // Display the lowest key:
  System.out.println("The lowest key value is: " + map.firstKey());

  // Display the highest key:
  System.out.println("The highest key value is: " + map.lastKey());

  // Display All key value
  System.out.println("All key value is:\n" + map);

  // Display the headMap:
  System.out.println("The head map is:\n" + map.headMap("4"));

  // Display the tailMap:
  System.out.println("The tail map is:\n" + map.tailMap("4"));

  // keySet method returns a Set view of the keys contained in this map.
  Iterator iterator = map.keySet().iterator();
  while (iterator.hasNext()) {
  Object key = iterator.next();
  System.out.println("key : " + key + " value :" + map.get(key));
  }
  }
}

Output:

The highest key value is: 5

All key value is:

{1=One, 2=Two, 3=Three, 4=Four, 5=Five}

The head map is:

{1=One, 2=Two, 3=Three}

The tail map is:

{4=Four, 5=Five}

key : 1 value :One

key : 2 value :Two

key : 3 value :Three

key : 4 value :Four

key : 5 value :Five

Download

Advertisements

Share on Google+Share on Google+

SortedMap (interface) example in java Collection Framework

Posted on: July 2, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

Ads
 

Discuss: SortedMap (interface) example in java Collection Framework   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:1
Ankush soni
June 13, 2011
Treemap

What is Treemap used here?

Ads

 

Ads