3

I cannot seem to figure out how to access the values of my hashmap

What I am basically trying to do is create a hashmap with an array as one of the values like json style.. If that makes sense?

So I want something like hash{key: value1, value2, value3, [number1,number2]}

and be able to access it like (pseudocode:) hash.get(3).get(1)

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));
        outlinks.put(keyphrase.get(i), xfile.getOutLinks(i));

    }

}

keyphrases is an ArrayList

this is my output of System.out.print(outlinks);

{[education, news, internet]=[0, 3], [power, news]=[1, 4], [computer, internet, device, ipod]=[2], [university, education]=[5]}

How would I go about getting say just this: [education, news, internet]=[0, 3]

I have tried:

outlinks.get(xfile.getKeyPhrases(i))

xfile.getKeyPhrases(0) would for example return [education, news, internet]

4

4 回答 4

1

您可以先获取地图的键集(Map.keySet());outlinks.keySet()

然后您可以在地图上使用这些键来获取您的条目(键的值)

于 2013-08-01T01:24:34.003 回答
0

You haven't posted enough of the surrounding code for your question to be entirely clear, but look at the Javadocs for Map. You will probably get what you want by iterating over outlinks.values().

于 2013-08-01T01:16:55.183 回答
0

我建议使用自定义对象并在您的集合中使用它。

您可以创建一个 POJO/Bean 类并使用您想要的详细信息覆盖 toString 方法,例如对数组中的项目进行迭代。当您使用它打印或显示时,将调用 toString 方法。

以下链接向您展示了一些想法:

http://www.javapractices.com/topic/TopicAction.do?Id=55

http://en.wikipedia.org/wiki/Plain_Old_Java_Object

于 2013-08-01T01:24:14.210 回答
0

您可以使用Map.keySet()方法访问任何 HashMap 的键。

另请注意,它java.util.HashMap是无序的。HashMap 不保证地图的顺序;特别是,它不保证订单会随着时间的推移保持不变。

你想重新审视你的 HashMap 的结构,你有ArrayList你的关键。

于 2013-08-01T01:26:53.513 回答