反转存储单词及其行号的HashMap

时间:2015-03-29 21:25:30

标签: java hashmap

我有一个HashMap

HashMap<String, LinkedList<Integer>> indexMap;

将文件中的所有单词及其出现的相应行号存储起来。

示例 -

This is just an example
to demonstrate what I am saying an is

会显示

This [1]
demonstrate [2]
an [1 2]
is [1 2]
...
....

等等。我想反转这个HashMap,以便它显示存储在每个行号的单词。

对于上面的特定示例,它应该显示

1 [This, an, just, example, is]
2 [demonstrate, what, to, I, am, saying, is, an]

对于这项特殊任务,这就是我所做的 -

import java.io.FileReader;
import java.io.LineNumberReader;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

public class ReverseIndex {
    private static Map<String, LinkedList<Integer>> indexMap = new HashMap<String, LinkedList<Integer>>();

    public static LinkedList<Integer> getIndex(String word) {
        return indexMap.get(word);
    }

    public static void main(String[] args) {
        try {
            LineNumberReader rdr = new LineNumberReader(
                    new FileReader(
                            args[0]));
            String line = "";
            int lineNumber = 0;
            //CREATING THE INITIAL HASHMAP WHICH WE WANT TO REVERSE
            while ((line = rdr.readLine()) != null) {
                lineNumber++;
                String[] words = line.split("\\s+");
                for (int i = 0; i < words.length; i++) {
                    LinkedList<Integer> temp = new LinkedList<Integer>();
                    if (getIndex(words[i]) != null)
                        temp = getIndex(words[i]);
                    temp.add(lineNumber);
                    indexMap.put(words[i], temp);
                }
            }
            //FINISHED CREATION

            Map<Integer, LinkedList<String>> myNewHashMap = new HashMap<Integer, LinkedList<String>>();


            for(Map.Entry<String, LinkedList<Integer>> entry : indexMap.entrySet()){
                LinkedList<Integer> values = entry.getValue();
                String key = entry.getKey();
                LinkedList<String> temp = new LinkedList<String>();
                for(int i = 0; i <= lineNumber; i++) {
                    if(values.contains(i)) {
                        if(!temp.contains(key))
                            temp.add(key);
                        myNewHashMap.put(i, temp);
                    }
                }
            }
            for(Map.Entry<Integer, LinkedList<String>> entry : myNewHashMap.entrySet()){
                Integer tester = entry.getKey();
                LinkedList<String> temp2 = new LinkedList<String>();
                temp2 = entry.getValue();
                System.out.print(tester + " ");
                for(int i = 0; i < temp2.size(); i++) {
                    System.out.print(temp2.get(i) + " ");
                }
                System.out.println();
            }
            rdr.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

然而,问题是,对于我们上面的例子,它会打印 -

1 example 
2 an 

如何将其反转以使其与预期输出完美配合?

1 个答案:

答案 0 :(得分:0)

只需使用以下代码替换main中的第一个for循环。我已根据惯例对原始代码进行了一些更改,例如移出循环中的变量声明,并以检查LinkedList&lt;&#39; String&gt;&gt;&gt;的方式更改逻辑。已存在的行号如果是,则将其添加到列表中,或者创建新的LinkedList&lt;&#39; String&#39;&gt;然后添加单词。

        LinkedList<Integer> values = null;
        String key = null;
        LinkedList<String> temp = null;
        for(Map.Entry<String, LinkedList<Integer>> entry : indexMap.entrySet())
        {
            values = entry.getValue();
            key = entry.getKey();
            temp = new LinkedList<String>();

            for(int value : values)
            {
                temp = myNewHashMap.get(value);
                if(temp == null )
                {
                    temp = new LinkedList<String>();
                    myNewHashMap.put(value,temp);
                }
                temp.add(key);
            }
        }