Java descendingMap() method of ConcurrentNavigableMap Interface.

Java descendingMap() method of ConcurrentNavigableMap Interface.


Posted in : Core Java Posted on : March 17, 2011 at 12:16 PM Comments : [ 0 ]

In this section we will discuss how can descendingMap() method be implemented in ConcurrentNavigableMap interface in java.

Java descendingMap() method of ConcurrentNavigableMap Interface.

In this section we will discuss how can descendingMap() method be implemented in ConcurrentNavigableMap interface in java.

Syntax

ConcurrentNavigableMap<K,V> descendingMap()

All data to the ConcurrentNavigableMap is represented in reverse order by the descendingMap() method. Since map gives back the descending map so, any change occurs into the map reflected to the descending map, and vice-versa.

Parameter description

This method has no parameter.

Example of descendingMap method

In this example we will show you how does descendingMap() method work in ConcurrentNavigableMap interface. This example will help you to understand how can you obtain mappings in reverse order from the underlying map.

Example :

package devmanuals.com;
import java.util.concurrent.ConcurrentSkipListMap;
import java.util.concurrent.ConcurrentNavigableMap;
public class CNMDescendingMap {
  public static void main (String args[]){
    ConcurrentNavigableMap<Integer,String> cnm = 
              new ConcurrentSkipListMap<Integer,String>();
    cnm.put(1,"A");
    cnm.put(2,"B");
    cnm.put(3,"C");
    cnm.put(4,"D");
    cnm.put(5,"E");
    cnm.put(6,"F");
    System.out.println("Mappings = "+cnm);
   System.out.println("Set of keys = "+cnm.keySet());
   System.out.println("First entry of the map : "+cnm.firstEntry());
    System.out.println("Last entry of the map : "+cnm.lastEntry());
    cnm=cnm.descendingMap();
    System.out.println("Mappings in descending order = "+cnm);
    System.out.println("Size of map = "+cnm.size());
    for(int i=0;i<=6;i++){
      cnm.remove(6);
    }
    System.out.println("Remainig set of keys = "+cnm.keySet());
    System.out.println("Remaining size of map = "+cnm.size());
  }
}

Output :

Mappings = {1=A, 2=B, 3=C, 4=D, 5=E, 6=F}

Set of keys = [1, 2, 3, 4, 5, 6]

First entry of the map : 1=A

Last entry of the map : 6=F

Mappings in descending order = {6=F, 5=E, 4=D, 3=C, 2=B, 1=A}

Size of map = 6

Remainig set of keys = [5, 4, 3, 2, 1]

Remaining size of map = 5

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics