简体   繁体   中英

HashMaps and Lists reconciliation in Java

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM