13

Is LinkedHashMap LIFO or FIFO in nature? If my map is of the form:

map.put(1,"one");
map.put(2,"two");

what would be the order if I was to iterate on the map using keyset??

EDIT: I think I did actually confuse two different concepts. Let me rephrase the question. What would be the order in which I encounter the quantities using entryset?Thanks for pointing that out btw. I do not intend to remove any entry.

Saucy Goat
  • 1,587
  • 1
  • 11
  • 32
BlahBlah
  • 275
  • 1
  • 5
  • 15

4 Answers4

15

In a linked hash map the elements in the backing doubly-linked list are added at the end (clearly: for preserving iteration order), but can be removed from any part in the list as the elements get removed from the map, it's incorrect to label the backing list (and by extension: the map) as LIFO or FIFO, it's neither - there's no concept of removal order in a map, and consequently no removal order can be assumed for the backing list in a linked hash map.

What a linked hash map does guarantee is that iterating over its contents (be it: the keys or the entries) will occur in the same order in which the elements were inserted in the map; from the documentation:

This implementation differs from HashMap in that it maintains a doubly-linked list running through all of its entries. This linked list defines the iteration ordering, which is normally the order in which keys were inserted into the map (insertion-order).

EDIT :

Regarding the last edit to the question, a LinkedHashMap guarantees that the iteration order of the keySet() will be the same order in which the elements were inserted: 1, 2 for the example in the question. This has nothing to do with FIFO/LIFO, those concepts deal with the order in which elements are removed from a data structure, and they're not related with the iteration order after inserting elements.

Óscar López
  • 232,561
  • 37
  • 312
  • 386
  • (1) The order in a LinkedHashMap is *defined* as insertion order, so (2) the concept of ordering does exist, and (3) there's nothing in the question about 'removal order', whatever that may be. Your second paragraph added in your edit now contradicts your first paragraph. – user207421 Jun 16 '12 at 18:59
  • 1
    @EJP LIFO means that the last element that was added will be the the first _removed_ when a removal operation occurs. FIFO means that the first element that was added will be the first _removed_ when a removal operation occurs. So you see, LIFO/FIFO has everything to do with removal, and nothing with iteration or ordering, OP is confusing different concepts, and so are you. – Óscar López Jun 16 '12 at 19:03
  • 1
    I think i did actually confuse two different concepts.let me rephrase the question.What would be order in which i encounter the quantities using entryset?Thanks for pointing that out btw.i donot intend to remove any entry. – BlahBlah Jun 16 '12 at 19:05
  • @user1453077 I edit my answer, the iteration order will be the same as insertion order – Óscar López Jun 16 '12 at 19:20
  • the answer is almost correct except LHM can have access order. still it FIFO – bestsss Jun 16 '12 at 19:28
  • @user1453077 You're welcome! if this (or any other) answer was helpful for you, please don't forget to accept it, clicking the check mark to its left. – Óscar López Jun 16 '12 at 19:29
  • It has access order as well: `public LinkedHashMap(int initialCapacity, float loadFactor, boolean accessOrder)`. Also `removeEldestEntry` offer "a clear policy: by specification. – bestsss Jun 16 '12 at 19:37
7

LinkedHashMap to quote from the javadocs is "Hash table and linked list implementation of the Map interface, with predictable iteration order" . So the keySet will return keys based on the order of insertion, esssentially a FIFO.

Biju Kunjummen
  • 49,138
  • 14
  • 112
  • 125
2

When access order is not utilized (standard case) you can consider LHM as a linked list w/ very fast access O(1) by key.

In that aspect it is FIFO when access order is unused (look at the c-tors). When access order is used the insertion order doesn't matter if there are any get() operations as they reorder the Entries. Look at protected boolean removeEldestEntry(Map.Entry<K,V> eldest) eldest=FIFO.

Essentially the LHM is a good doubly linked list of Map.Entry<Key, Value> with a hash index over the keys. I myself never use the vanilla HashMap as in its current impl. it has very little benefit over LHM - lower memory footprint but horrid iteration. Java8 (or 9) perhaps may finally fix HashMap, hopefully Doug Lea will push his impl.

bestsss
  • 11,796
  • 3
  • 53
  • 63
1

According to Java docs, if you were to iterate over the map, the keyset would be in insertion-order. So the first key you get is the first key entered, over the existing keys. Note, reinserting a key-value pair does not change the original key position.

user845279
  • 2,794
  • 1
  • 20
  • 38