- Commons Collections Tutorial
- Commons Collections - Home
- Commons Collections - Overview
- Commons Collections - Environment Setup
- Commons Collections - Bag Interface
- Commons Collections - BidiMap Interface
- Commons Collections - MapIterator Interface
- Commons Collections - OrderedMap Interface
- Commons Collections - Ignore Null
- Commons Collections - Merge & Sort
- Commons Collections - Transforming Objects
- Commons Collections - Filtering Objects
- Commons Collections - Safe Empty Checks
- Commons Collections - Inclusion
- Commons Collections - Intersection
- Commons Collections - Subtraction
- Commons Collections - Union
- Commons Collections Resource
- Commons Collections - Quick Guide
- Commons Collections - Useful Resources
- Commons Collections - Discussion
Commons Collections - OrderedMap Interface
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; public class OrderedMapTester { public 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