1

I have a nested LinkedHashMap that looks like this:

LinkedHashMap<String,LinkedHashMap<String,LinkedList<Long>>> map = new...

The issue is that only 1 inner map is added per outer map, whereas I am expecting 2. I think the problem is how I'm constructing my map, and over-writing the first inner map with the second. (To briefly summarize my program, I am mapping each hand onto each finger. The mapping structure needs to be Finger={Right_Hand=[],Left_Hand=[], not vice versa.)

The constructor:

Set<String> handNames = new HashSet<String>(Arrays.asList("Left","Right");
Set<String> fingerNames = new HashSet<String>(Arrays.asList("Pinky","Ring","Middle","Index","Thumb");
LinkedHashMap<String, LinkedHashMap<String,LinkedList<Long>>> fingerHandMap = new LinkedHashMap<String, LinkedHashMap<String,LinkedList<Long>>>();

createNestedMap() {
    for (String finger : fingerNames)   
        for (String hand : handNames) {
            LinkedHashMap<String, LinkedList<Long>> handMap = new LinkedHashMap<String, LinkedList<Long>>();
            handMap.put(hand, new LinkedList<Long>());
            fingerHandMap.put(finger, handMap);
        }
}

When I print out the map, though, it looks like this:
{Ring={Left=[]}, Pinky={Left=[]}, Thumb={Left=[]}, Middle={Left=[]}, Index={Left=[]}}

How would I go about creating unique LinkedLists, to allow the map to look like:
{Ring={Right=[], Left=[]}, Pinky={Right=[], Left=[]}, Thumb={Right=[], Left=[]}, Middle={Right=[], Left=[]}, Index={Right=[], Left=[]}}

Thanks!

1 Answer 1

1

I'm going to write what you're currently doing in psuedocode, so hopefully you can see what you're doing wrong:

create a new finger hand map
for each finger:
    for each hand:
        create a new hand map
        put an entry mapping the hand to an empty list in the hand map
        put an entry mapping the finger to the hand map in the finger hand map

Keep in mind that when you put a key-value entry in a map it replaces any existing entry with the same key.

Let me know if you need further clarification.

Sign up to request clarification or add additional context in comments.

5 Comments

I still don't see it. I feel dumb, but I've probably been staring at this program for too long today. Any clues?
@Adam_G See my edit just now. Basically you're overwriting each "right" map with a "left" map. You really want to look up the same map again and add to it.
Why am I over-writing it, though, if one of the keys is "right" and one is "left"? (I know you're right; I'm just not seeing it.)
@Adam_G Yeah. If you debug, you'll see it. It's the finger keys that are being overwritten.
This is why I should've just stopped coding and gone home for the day. Thank you so much for your help. I can keep some of my sanity now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.