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
7
votes
4 answers

Most convenient way to get the last key or value in a LinkedHashMap?

LinkedHashMap description says "it maintains a doubly-linked list running through all of its entries" so I'm wondering how to get the last entry or key entered? Can I confidently downcast .values() to LinkedList to get that doubly-linked list and…
Navigateur
  • 1,852
  • 3
  • 23
  • 39
7
votes
2 answers

How can I iterate starting at a particular key in a LinkedHashMap?

If I have a data structure Stock { String Symbol; LinkedHashMap DateForPrice; } I know in the LinkedHashMap, I can get the stock price of specific date without traversing the whole list. However, if I want to iterate through the…
Joe Liao
  • 71
  • 1
  • 3
7
votes
1 answer

LinkedHashMap memory consumption

The user uploads a huge file consisting of 1 million words. I parse the file and put the each line of the file into a LinkedHashMap. I need O(1) access and removal by key. Also, I need to preserve the access order, iterate from any…
user12384512
  • 3,362
  • 10
  • 61
  • 97
7
votes
3 answers

Get next item in LinkedHashMap?

I have the first key/value pair in a LinkedHashMap, which I get from a loop: for (Entry entry : map.entrySet()) { String key = entry.getKey(); String value = entry.getValue(); //put key value to use break; } Later…
user375566
7
votes
4 answers

cast LinkedHashMap to HashMap in groovy

How do I convert LinkedHashMap to java.util.HashMap in groovy? When I create something like this in groovy, it automatically creates a LinkedHashMap even when I declare it like HashMap h = .... or def HashMap h = ... I tried doing: HashMap h =…
user140736
  • 1,913
  • 9
  • 32
  • 53
7
votes
1 answer

Understanding acessOrder LinkedHashMap Implementation in java

As I understand LinkedHashMap extends HashMap and LinkedHashMap.Entry extends HashMap.Entry as well. LinkedHashMap has two main attributes 1) header which is a LinkedHashMap.Entry node. and 2) inherited table which is HashMap.Entry[] array. Now…
nanosoft
  • 2,913
  • 4
  • 41
  • 61
7
votes
3 answers

Sorting LinkedHashMap by value

How can you sort a LinkedHashMap using the value ? Is there a way to insert entries into a LinkedHashMap so that they are inserted in order based on their value ?
user3922757
  • 305
  • 2
  • 5
  • 14
7
votes
2 answers

Why iteration through buckets in LinkedHashMap is faster than HashMap?

I am struggling a lot to understand this. Googling, I found "HashMap iterator has to iterate through all the buckets including empty buckets" and "in LinkedHashMap all the entries are doubly linked". If this is the case why the only HashMap…
user3607869
  • 317
  • 1
  • 9
  • 19
7
votes
0 answers

Why doesn't new Map methods generate entry accesses on LinkedHashMap?

Javadocs of LinkedHashMap in JDK8: Invoking the put or get method results in an access to the corresponding entry (assuming it exists after the invocation completes). The putAll method generates one entry access for each mapping in the specified…
leventov
  • 14,760
  • 11
  • 69
  • 98
7
votes
1 answer

Duplicates in LinkedHashMap

In my code I am using a set of interleaved LinkedHashMaps inside each other as below. The code is fine and gives me the result I want except it automatically removes the duplicates. I couldnt find out how I can use TreeMap or Set in order to keep…
Ramin
  • 891
  • 2
  • 10
  • 16
7
votes
3 answers

What order are elements inserted with LinkedHashMap.putAll() in?

I am reading Javadoc for LinkedHashMap, where it is mentioned: The putAll method generates one entry access for each mapping in the specified map, in the order that key-value mappings are provided by the specified map's entry set iterator. My…
Lin Ma
  • 9,739
  • 32
  • 105
  • 175
6
votes
3 answers

Sending LinkedHashMap to intent

I want send LinkedHashMap to another Intent. But I don't known what method for extras is allowable. Bundle extras = getIntent().getExtras(); LinkedHashMap listItems = extras.get(LIST_TXT);
frstua
  • 61
  • 2
6
votes
1 answer

Stable element ordering issue while iterating HashMap in Java 8

Some test cases are failing in my application which depends upon the element insertion order . It used to work fine in Java 7 but this issue started after upgrading to Java 8. While searching internet I found this in an article: Java 8 includes…
Siddhartha
  • 492
  • 1
  • 5
  • 15
6
votes
1 answer

How to efficiently remove entries from a LinkedHashMap in Java?

I want to remove all entries from a LinkedHashMap that were added after an entry with a given key. My first try was: LinkedHashMap var = new LinkedHashMap(); public void removeEntriesAfter(String key) { …
Edward
  • 4,453
  • 8
  • 44
  • 82
6
votes
1 answer

LinkedHashMap Structure for LRU Cache

I am a little confused about how to build a LRU cache by using LinkedHashMap (How would you implement an LRU cache in Java 6?), and I want to make sure I understand how it work internally behind the scene. Lets say I define a class LRUMap that…
peter
  • 8,333
  • 17
  • 71
  • 94