LinkedHashMap is a Hash table and linked list implementation of the Map interface in the Java Standard library. This implementation provides a predictable iteration order, in contrast with the unspecified ordering provided by HashMap, which is normally the order in which keys were inserted into the map.
Questions tagged [linkedhashmap]
703 questions
194
votes
5 answers
Is the order guaranteed for the return of keys and values from a LinkedHashMap object?
I know LinkedHashMap has a predictable iteration order (insertion order). Does the Set returned by LinkedHashMap.keySet() and the Collection returned by LinkedHashMap.values() also maintain this order?

user256239
- 17,717
- 26
- 77
- 89
183
votes
17 answers
Java LinkedHashMap get first or last entry
I have used LinkedHashMap because it is important the order in which keys entered in the map.
But now I want to get the value of key in the first place (the first entered entry) or the last.
Should there be a method like first() and last() or…

maiky
- 3,503
- 7
- 28
- 28
98
votes
6 answers
Casting LinkedHashMap to Complex Object
I've got an application that stores some data in DynamoDB using Jackson to marshall my complex object into a JSON.
For example the object I'm marshalling might look like this:
private String aString;
private List someObjectList;
Where…

tarka
- 5,289
- 10
- 51
- 75
87
votes
3 answers
Does entrySet() in a LinkedHashMap also guarantee order?
I am using a linkedHashMap to guarantee order when someone tries to access it. However, when it comes time to iterate over it, does using entrySet() to return key/value pairs guarantee order as well? No changes will be made while iterating.
EDIT:…

Diego
- 16,830
- 8
- 34
- 46
86
votes
4 answers
What is the difference between LRU and LFU
What is the difference between LRU and LFU cache implementations?
I know that LRU can be implemented using LinkedHashMap.
But how to implement LFU cache?

Javadroider
- 2,280
- 1
- 22
- 45
64
votes
10 answers
How to add element at specific index/position in LinkedHashMap?
I have an ordered LinkedHashMap and i want to add element at specific index , say at first place or last place in the map.
How can i add element in LinkedHashMap at an specific position?
Even if I could add an element to FIRST or LAST position in…

supernova
- 3,111
- 4
- 33
- 30
64
votes
5 answers
How to iterate through LinkedHashMap with lists as values
I have following LinkedHashMap declaration.
LinkedHashMap> test1
my point is how can i iterate through this hash map.
I want to do this following, for each key get the corresponding arraylist and print the values of the…

P basak
- 4,874
- 11
- 40
- 63
58
votes
5 answers
How get value from LinkedHashMap based on index not on key?
I have
LinkedHashMap> hMap;
I want to get List by position not on key.
I don't want to use iterate.
Is there any other way to get Value based on index ?

MAC
- 15,799
- 8
- 54
- 95
53
votes
4 answers
Sorting LinkedHashMap
How can I sort a LinkedHashMap based on its values given that the LinkedHashMap contains of String and Integer. So I need to sort it based on the Values which are Integers.
Thanks a lot

Ramin
- 891
- 2
- 10
- 16
50
votes
8 answers
How is the implementation of LinkedHashMap different from HashMap?
If LinkedHashMap's time complexity is same as HashMap's complexity why do we need HashMap? What are all the extra overhead LinkedHashMap has when compared to HashMap in Java?

Passionate programmer
- 5,748
- 10
- 39
- 43
49
votes
2 answers
GSON issue with String
String s = "m\\"+"/m\\/m/m/m/m/m";
LinkedHashMap hm = new LinkedHashMap<>();
hm.put("test", s);
System.out.println(hm+" Hash map = "+hm.toString());
Fine Output is {test=m\/m\/m/m/m/m/m} Hash map =…

Fahad Ishaque
- 1,916
- 1
- 17
- 21
44
votes
7 answers
Iterating through a LinkedHashMap in reverse order
I have a LinkedHashMap:
LinkedHashMap
that I need to iterate through from a given key's position, backwards. So if I was given the 10th item's key, I'd need iterate backwards through the hashmap 9, 8, 7 etc.

Dominic Bou-Samra
- 14,799
- 26
- 100
- 156
44
votes
2 answers
Does Java's LinkedHashMap maintain the order of keys?
When LinkedHashMap.keySet() is called, will the order of the Set returned be the same as the order the keys were added in?

Armand
- 23,463
- 20
- 90
- 119
41
votes
7 answers
How to get position of key/value in LinkedHashMap using its key
I have a LinkedHashMap (called info) that contains name/age (string/int) pairs. How can I get the position of the key/value if I input the key? For example, if my LinkedHashMap looked like this {bob=12, jeremy=42, carly=21} and I was to search…

M9A
- 3,168
- 14
- 51
- 79
40
votes
5 answers
How is the internal implementation of LinkedHashMap different from HashMap implementation?
I read that HashMap has the following implementation:
main array
↓
[Entry] → Entry → Entry ← linked-list implementation
[Entry]
[Entry] → Entry
[Entry]
[null ]
So, it has an array of Entry objects.
Questions:
I was wondering how can an…

Mercenary
- 2,106
- 6
- 34
- 43