How to add element at specific index/position in LinkedHashMap?

supernova picture supernova · Oct 6, 2011 · Viewed 88.4k times · Source

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 LinkedHashMap would help!

Answer

MasterCassim picture MasterCassim · Oct 6, 2011

You can not change the order. It is insert-order (by default) or access-order with this constructor:

public LinkedHashMap(int initialCapacity, float loadFactor, boolean accessOrder)

  • Constructs an empty LinkedHashMap instance with the specified initial capacity, load factor and ordering mode.

  • Parameters: initialCapacity - the initial capacity loadFactor - the load factor accessOrder - the ordering mode - true for access-order, false for insertion-order

  • Throws: IllegalArgumentException - if the initial capacity is negative or the load factor is nonpositive

See: LinkedHashMap