0

My issue is that I need a HashMap which returns a reference to an internal LinkedList when hashMap.get(key) is called— not simply return the value that corresponds to the key.

From what I've gathered, a LinkedHashMap enables a doubly-linked list to occupy each map entry for collision handling. However, I want to be able to get a reference to the overarching LinkedList that encapsulates all values mapped into it (each object that shares a LinkedList also share a particular feature I'm very interested in due to my overridden hash code function).

Put differently, I aim to avoid the linked list auto-traversal built into the LinkedHashMap class and just want the reference of the list itself to operationalize.

I want this reference to be returned in addition to having the capacity to add new values to the end of the LinkedLists with linkedHashMap.put(key, value) invocation.

Any pointers would be appreciated.

Sam S.
  • 348
  • 1
  • 3
  • 15
  • "From what I've gathered, a LinkedHashMap enables a doubly-linked list to occupy each map entry for collision handling" - no, the linked list that gives LinkedHashMap its name does something completely different. The collision-resolution lists are present in a normal HashMap. – user2357112 Aug 30 '16 at 22:03
  • how about `Map>` ?? Does that suit your needs? `linkedHashMap.put(key, value)` can be done with simple get and update steps (but wont be atomic if not synchronized) – Antoniossss Aug 30 '16 at 22:04
  • 3
    Why not just use a `HashMap>`? – user2357112 Aug 30 '16 at 22:04
  • 1
    " I am working on a personal project and I'll spare you the unnecessary details. " --- too late. – bradimus Aug 30 '16 at 22:07
  • 1
    You are asking the wrong questions and caring about the wrong stuff. Ignore the Map implementation name. Just do as @user2357112 suggests. – Bohemian Aug 30 '16 at 22:07
  • 1
    I was thinking of doing what @user2357112 said. But I was worried about how the put() function would work as Edward Peters had alluded to below. – Sam S. Aug 30 '16 at 22:08
  • If you desperately need a `LinkedList` for the values, then use a `HashMap>`. It's usually best practice to use the abstract type (in this case `List`) where possible. BTW, "I am working on a personal project and I'll spare you the unnecessary details." is itself an unnecessary detail. – Bohemian Aug 30 '16 at 22:10
  • *Edited it out. Sorry for the wasted sentence and I appreciate the response. – Sam S. Aug 30 '16 at 22:12

1 Answers1

1

LinkedHashMap just stores its keys in a defined order (A LinkedList backs the KeySet). It isn't anything about how it handles collisions.

For what you've described, I think you'll have to implement things yourself. You're basically making a Map<KeyType, List<EntryType>>, with a "put" function that appends to the associated list. It isn't too much code.

I probably wouldn't make it actually extend Map, though, because what you've described doesn't really match the interface for that.

Edward Peters
  • 3,623
  • 2
  • 16
  • 39
  • In Java 8, it's much easier with [`Map#computeIfAbsent`](https://docs.oracle.com/javase/8/docs/api/java/util/Map.html#computeIfAbsent-K-java.util.function.Function-) by using something like `theMap.computeIfAbsent(key, k -> new LinkedList<>()).add(value);` – 4castle Aug 30 '16 at 22:10
  • @4castle Ooh, I did not know that was a thing. Thanks! – Edward Peters Aug 30 '16 at 22:17