LinkedHashMap is same as HashMap except that it maintains insertion order.
Some points about LinkedHashMap
- LinkedHashMap implements Map interface and extends HashMap class.
- LinkedHashMap maintains insertion order, so when you will be able to access elements in the order they were inserted like ArrayList.
- LinkedHashMap maintains doubly Linked list to maintain insertion order.
Example:
LinkedHashMapMain.java
If you may iterate LinkedHashMap in multiple ways.
When you run above program, you will get below output:
When you run above program, you will get below output:
1
2
3
4
5
6
7
8
9
|
-----------------------------
Iterating Using keySet() and for each loop
Country:India and Capital:Delhi
Country:Japan and Capital:Tokyo
Country:France and Capital:Paris
Country:Russia and Capital:Moscow
-----------------------------
|