TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to JAVA

Java LinkedHashMap Example

Use the LinkedHashMap collection. The order items are added is maintained.
LinkedHashMap. In a HashMap, the order entries are added is not maintained—it is lost when they are put into the table. But with LinkedHashMap, a linked list maintains the order.HashMap
To ensure we can access elements in the order we add them, LinkedHashMap is a good alternative to HashMap. It still has constant time lookup, but just has some overhead.
An example program. Let us examine this program. We import the LinkedHashMap in the same way we would import HashMap. We use diamond inference syntax in the same way as well.

Put: We invoke put to add 3 entries to our LinkedHashMap. Then we use entrySet() to loop over them.

For

Println: We print the key and value of each Entry to the console with System.out.println.

Console

Important: Notice how the order of our loop is maintained when we access entrySet. The collection never forgot the ordering.

Java program that uses LinkedHashMap import java.util.LinkedHashMap; import java.util.Map.Entry; public class Program { public static void main(String[] args) { // Add 3 entries to a LinkedHashMap. LinkedHashMap<String, Integer> values = new LinkedHashMap<>(); values.put("bird", 10); values.put("frog", 20); values.put("snake", 25); // For LinkedHashMap, we can loop over the items in the order they were added. // ... A linked list keeps track of the order as we add them. for (Entry<String, Integer> pair : values.entrySet()) { System.out.println(pair.getKey() + "/" + pair.getValue()); } } } Output bird/10 frog/20 snake/25
HashMap loop. Consider this version of the program. It uses HashMap instead of LinkedHashMap. The items are added in a certain order, and then loops over the entries.

But: With HashMap (unlike LinkedHashMap) they are looped over in a different order. This program shows the change.

Java program that loops over HashMap import java.util.HashMap; import java.util.Map.Entry; public class Program { public static void main(String[] args) { HashMap<String, Integer> values = new HashMap<>(); values.put("bird", 10); values.put("frog", 20); values.put("snake", 25); for (Entry<String, Integer> pair : values.entrySet()) { System.out.println(pair.getKey() + "/" + pair.getValue()); } } } Output frog/20 bird/10 snake/25
Some considerations. The LinkedHashMap is an ideal replacement for HashMap when we need to maintain the order of items we put into the table. If ordering is needed, prefer LinkedHashMap.

However: If we do not need the order, HashMap is better because it will be slightly faster in most programs.

A simple review. The LinkedHashMap is a special kind of HashMap. It enforces a linked list data structure where the order is maintained. This helps if the time of an addition is important.
For most programs, HashMap is a better choice as it is simpler. It also has less conceptual complexity (it omits the "Linked" part). It is important to choose the correct collection.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf