Apache Commons Collections - OrderedMap Interface



Overview

OrderedMap is a new interface for maps to retain the order in which elements are added. LinkedMap and ListOrderedMap are two available implementations. This interfaces supports iterator that of Map and allows iteration in both directions either forwards or backwards in a Map.

Interface Declaration

Following is the declaration for org.apache.commons.collections4.OrderedMap<K,V> interface −

public interface OrderedMap<K,V>
   extends IterableMap<K,V>

Methods

The methods for OrderedMap Interface are as follows −

Sr.No. Method & Description
1

K firstKey()

Gets the first key currently in this map.

2

K lastKey()

Gets the last key currently in this map.

3

OrderedMapIterator<K,V> mapIterator()

Obtains an OrderedMapIterator over the map.

4

K nextKey(K key)

Gets the next key after the one specified.

5

K previousKey(K key)

Gets the previous key before the one specified.

Methods Inherited

This interface inherits methods from the following interface −

  • org.apache.commons.collections4.Get
  • java.util.Map.

Example - Printing First and Last Keys using OrderedMap Interface

CommonCollectionsTester.java

package com.tutorialspoint;

import org.apache.commons.collections4.OrderedMap;
import org.apache.commons.collections4.map.LinkedMap;

public class CommonCollectionsTester {
   public static void main(String[] args) {
      OrderedMap<String, String> map = new LinkedMap<>();
      map.put("One", "1");
      map.put("Two", "2");
      map.put("Three", "3");
      
      System.out.println(map.firstKey());
	  System.out.println(map.lastKey());
   }
}

Output

The output is stated below −

One
Three

Example - Iterating an OrderedMap

CommonCollectionsTester.java

package com.tutorialspoint;

import org.apache.commons.collections4.MapIterator;
import org.apache.commons.collections4.OrderedMap;
import org.apache.commons.collections4.map.LinkedMap;

public class CommonCollectionsTester {
   public static void main(String[] args) {
      OrderedMap<String, String> map = new LinkedMap<>();
      map.put("One", "1");
      map.put("Two", "2");
      map.put("Three", "3");
      
      MapIterator<String, String> iterator = map.mapIterator();
      while (iterator.hasNext()) {
         Object key = iterator.next();
         Object value = iterator.getValue();
         System.out.println("Key: " + key, ", Value: " + value);
      }
   }
}

Output

The output is stated below −

Key: One, Value: 1
Key: Two, Value: 2
Key: Three, Value: 3

Example - Getting Previous/Next keys from an OrderedMap

CommonCollectionsTester.java

package com.tutorialspoint;

import org.apache.commons.collections4.OrderedMap;
import org.apache.commons.collections4.map.LinkedMap;

public class CommonCollectionsTester {
   public static void main(String[] args) {
      OrderedMap<String, String> map = new LinkedMap<>();
      map.put("One", "1");
      map.put("Two", "2");
      map.put("Three", "3");
      
      System.out.println(map.previousKey("Two"));
	  System.out.println(map.nextKey("Two"));
   }
}

Output

The output is stated below −

One
Three
Advertisements