Commons Collections Tutorial
Selected Reading
- Commons Collections - Discussion
- Commons Collections - Useful Resources
- Commons Collections - Quick Guide
- Commons Collections - Union
- Commons Collections - Subtraction
- Commons Collections - Intersection
- Commons Collections - Inclusion
- Commons Collections - Safe Empty Checks
- Commons Collections - Filtering Objects
- Commons Collections - Transforming Objects
- Commons Collections - Merge & Sort
- Commons Collections - Ignore Null
- Commons Collections - OrderedMap Interface
- Commons Collections - MapIterator Interface
- Commons Collections - BidiMap Interface
- Commons Collections - Bag Interface
- Commons Collections - Environment Setup
- Commons Collections - Overview
- Commons Collections - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Commons Collections - MapIterator Interface
Commons Collections - MapIterator Interface
The JDK Map interface is pretty difficult to iterate as Iteration to be done on EntrySet or over the KeySet objects. MapIterator provides simple iteration over Map. Following example illustrates the same.
Example of MapIterator Interface
An example for MapIteratorTester.java is as follows −
import org.apache.commons.collections4.IterableMap; import org.apache.commons.collections4.MapIterator; import org.apache.commons.collections4.map.HashedMap; pubpc class MapIteratorTester { pubpc static void main(String[] args) { IterableMap<String, String> map = new HashedMap<>(); map.put("1", "One"); map.put("2", "Two"); map.put("3", "Three"); map.put("4", "Four"); map.put("5", "Five"); MapIterator<String, String> iterator = map.mapIterator(); while (iterator.hasNext()) { Object key = iterator.next(); Object value = iterator.getValue(); System.out.println("key: " + key); System.out.println("Value: " + value); iterator.setValue(value + "_"); } System.out.println(map); } }
Output
The output is stated below −
key: 3 Value: Three key: 5 Value: Five key: 2 Value: Two key: 4 Value: Four key: 1 Value: One {3=Three_, 5=Five_, 2=Two_, 4=Four_, 1=One_}Advertisements