From the course: Advanced Java Programming

Unlock the full course today

Join today to access over 22,400 courses taught by industry experts or purchase this course individually.

Implementing a LinkedHashMap

Implementing a LinkedHashMap - Java Tutorial

From the course: Advanced Java Programming

Start my 1-month free trial

Implementing a LinkedHashMap

- [Instructor] When using a normal HashMap there is no guarantee that you can get the entries back in the same order that you put them in. However, when you use a linked HashMap, the order of the entries is retained. In this example, I have a class called LinkedHashMap example with a main method in it. In the main method there is a linked HasMap called phonebook which has the contact's name as the key, and their phone number as the value. I have added four contacts called Kevin, Joe, Brenda, and Gary to the phonebook. After adding the contacts, I print out the value associated with the name Kevin. Finally, I have a for each loop that iterates over all of the entries in the map and prints out the key and the value for each one. With Linked HashMaps, you can specify if you want the elements to be retrieved in the order that they were added in, or in the order that they have been accessed in. To choose between these options, I need to alter the constructor I used when I created the…

Contents