Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
SortedMap (interface) example in java Collection Framework 
 

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

 

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 code

                         

» View all related tutorials
Related Tags: c string ide class reference io references size sed get vi key value field fields this id length max preferences

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.