English 中文(简体)
Commons Collections - OrderedMap Interface
  • 时间:2024-11-03

Commons Collections - OrderedMap Interface


Previous Page Next Page  

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. Following example illustrates the same.

Example of MapIterator Interface

An example of OrderedMapTester.java is as given below −


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

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

Output

The result will be as follows −


One
Two
Three
Advertisements