0

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

2
  • 1
    I don't understand what you mean by "keys are null". The key to a Map is always an object of some sort. What output are you getting that makes you think your keys are null? Commented Aug 1, 2013 at 3:45
  • Are you sure that xfile.getPageTitle(i) is not returning null? Commented Aug 1, 2013 at 3:48

3 Answers 3

2

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.

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

Comments

0

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);

    }

Comments

0

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)

Comments

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.