Questions tagged [linkedhashmap]

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.

703 questions
10
votes
1 answer

LinkedHashMap ordering

As specified in the javadoc for LinkedHashMap, insertion order is not affected if a key is re-inserted into the map but While running the below program,I notice that inserting the same key again in changing the access order. Map map…
Manish
  • 1,274
  • 3
  • 22
  • 59
10
votes
6 answers

LinkedHashMap signature

Looking at the JDK source code for LinkedHashMap, I noticed that this class is declared as: public class LinkedHashMap extends HashMap implements Map {... why the redundant "implements Map" (since HashMap…
Eleco
  • 3,194
  • 9
  • 31
  • 40
10
votes
1 answer

Iteration order of a LinkedHashMap

After reading the documentation for LinkedHashMap (and having used it several times), I'm still not clear about one of its properties...is the iteration order for a LinkedHashMap: the same as insertion order for entrySet(), keySet(), and values(),…
Andrew Mao
  • 35,740
  • 23
  • 143
  • 224
9
votes
3 answers

How to create a loop through LinkedHashMap>?

Please help me to create a loop through LinkedHashMap> h: if (h.get("key1").size() == 0) System.out.println("There is no errors in key1."); else System.out.println("ERROR: there are unexpected errors in key1."); if…
Prostak
  • 3,565
  • 7
  • 35
  • 46
9
votes
1 answer

LinkedHashMap entrySet's order not being preserved in a stream (Android)

I'm creating a very simple form validation utility for a sign up screen, and I'm running into some unexpected behavior concerning LinkedHashMap and a stream created from its entrySet. I'm storing validation results in a LinkedHashMap, with the…
Chris
  • 909
  • 8
  • 18
9
votes
3 answers

How to convert/cast HashMap to LinkedHashMap?

I'm using Hawk as a replacement for SharedPreferences in my application. I'm trying to store a LinkedHashMap in it, but for some reason when I pull it back from Hawk it returns as a regular HashMap and not a LinkedHashMap. At this point I crash with…
Emil Adz
  • 40,709
  • 36
  • 140
  • 187
9
votes
5 answers

Best structure for list of key-value (integer, string) to be shuffled

I need to implement a structure in Java that is a key-value list (of types Integer-String) and I want to shuffle it. Basically, I would like to do something like that. public LinkedHashMap getQuestionOptionsMap(){ …
MDT
  • 1,599
  • 4
  • 18
  • 26
8
votes
2 answers

Shrink LinkedHashMap in Java

How can you shrink a LinkedHashMap? I overrode the removeEldestEntry method, but this method is only called once when a new value is inserted. So there is no change of making the map smaller this way. The LinkedHashMap only gives my a normal…
Franz Kafka
  • 10,623
  • 20
  • 93
  • 149
8
votes
3 answers

Get LinkedList of entries from LinkedHashMap with values() method

I'm trying to understand if I can get a linked list of entries from my linked hash map. I can get entrySet() and then using iterator I get through each entry in the insertion order. This will give me the linked list of entries in the insertion…
Rostislav V
  • 1,706
  • 1
  • 19
  • 31
8
votes
2 answers

LRUCache entry reordering when using get

I checked out official Android documentation for LRUCache which says : Each time a value is accessed, it is moved to the head of a queue. When a value is added to a full cache, the value at the end of that queue is evicted and may become eligible…
gaurav jain
  • 3,119
  • 3
  • 31
  • 48
8
votes
2 answers

How to check equality of LinkedHashMaps in Java - also taking the insertion-order into account?

I'd like to check the equality of two LinkedHashMaps in Java. the equals()-method is located in AbstractMap and only checks if and entry with the same key and value is present in the compared list. Thus, the insertion order is not checked: package…
Edward
  • 4,453
  • 8
  • 44
  • 82
8
votes
3 answers

LinkedHashMap order issue

It is mentioned in the LinkedHashMap Javadocs: In particular, operations on collection-views do not affect the order of iteration of the backing map. What does "operations on collection-views" mean?
Lin Ma
  • 9,739
  • 32
  • 105
  • 175
8
votes
5 answers

java: maps zoo, what to choose

I'm pretty new to the Java World (since I'm writing primary in C/C++). I'm using maps in my apps. Since java.util.Map is abstract I need to instantiate it's implementation. Usually I use HashMap like: Map x = new HashMap<>(); But…
maverik
  • 5,508
  • 3
  • 35
  • 55
7
votes
5 answers

What are the pros and cons of LinkedHashMaps vs. LinkedHashSets?

Could someone explain the main benefits for choosing one over the other and the detriments that come with that choice?
Bobby
  • 18,217
  • 15
  • 74
  • 89
7
votes
2 answers

Anyone know of a good LinkedDictionary/Hashed LinkedList?

I am in need of a Generic collection that is somewhere in between a Dictionary and LinkedList. I want to be able to: Access elements by key Access previous and next elements I've taken a look at the provided Generic collections as well as the…