简体   繁体   中英

Java, hashmap inside a hashmap

follow up from my question here: How To Access hash maps key when the key is an object

I wanted to try something like this: webSearchHash.put(xfile.getPageTitle(i),outlinks.put(keyphrase.get(i), xfile.getOutLinks(i)));

Wonder why my keys are null

here is my code:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;

import readFile.*;

public class WebSearch {

    readFile.ReadFile xfile = new readFile.ReadFile("inputgraph.txt");
    HashMap webSearchHash = new HashMap();
    ArrayList belongsTo = new ArrayList();
    ArrayList keyphrase = new ArrayList();

    public WebSearch() {        
    }

    public void createGraph()
    {
        HashMap <Object, ArrayList<Integer> > outlinks = new HashMap <Object, ArrayList<Integer>>();
        for (int i = 0; i < xfile.getNumberOfWebpages(); i++ )
        {
            keyphrase.add(i,xfile.getKeyPhrases(i));
            webSearchHash.put(xfile.getPageTitle(i),outlinks.put(keyphrase.get(i), xfile.getOutLinks(i)));
        }
    }
}

when I do System.out.print(webSearchHash); the output is {Star-Ledger=null, Apple=null, Microsoft=null, Intel=null, Rutgers=null, Targum=null, Wikipedia=null, New York Times=null}

However System.out.print(outlinks); gives me : {[education, news, internet]=[0, 3], [power, news]=[1, 4], [computer, internet, device, ipod]=[2]} Basically I want a hashmap to be a value of my key

You really shouldn't use a HashMap (or any mutable object) as your key, since it will destabilize your Map . Depending on what you're intending to accomplish, there may be a number of useful approaches and libraries, but using an unstable object as a Map key is asking for trouble.

So figured I just do this which gives exactly what I want:

for (int i = 0; i < xfile.getNumberOfWebpages(); i++ )
    {
        HashMap <Object, ArrayList<Integer> > outlinks = new HashMap <Object, ArrayList<Integer>>();
        keyphrase.add(i,xfile.getKeyPhrases(i));
        outlinks.put(keyphrase.get(i), xfile.getOutLinks(i));
        webSearchHash.put(xfile.getPageTitle(i), outlinks);

    }

Your problem is you are putting in null with this statement

 webSearchHash.put(xfile.getPageTitle(i),outlinks.put(keyphrase.get(i), xfile.getOutLinks(i)));

lets break it down. a put is of the form

map.put(key,value)

so for your key you have getPageTitle(i) . which is fine

for your value, you have the return value of

outlinks.put(keyphrase.get(i), xfile.getOutLinks(i))

according to the javadoc, a hashmap put returns the previous value that was associated with this key (in this case keyphrase.get(i)) or null if no value was previously associated with it.

Since nothing was previously associated with your key, it returns null.

So your statement effectively is saying

webSearchHash.put(xfile.getPageTitle(i),null);

http://docs.oracle.com/javase/6/docs/api/java/util/HashMap.html#put(K , V)

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