简体   繁体   English

用Java填充Hashmap

[英]Populating a Hashmap in Java

Ok I am having quite a bit of an issue. 好的,我有很多问题。 I want to populate my hash map with a set of String keywords. 我想用一组String关键字填充哈希图。 So basically, The user enters a keyword and if it matches the keyword in the hash map a parseFile method is called. 因此,基本上,用户输入一个关键字,如果它与哈希图中的关键字匹配,则将调用parseFile方法。 Else if the user input is a synonym of the keyword, I search the hash map for the original keyword. 否则,如果用户输入是关键字的同义词,我将在哈希图中搜索原始关键字。 But my issue is populating the hash map. 但是我的问题是填充哈希图。 Can anyone guide me in the right direction? 谁能指导我正确的方向?

 public static void main(String[] args) throws FileNotFoundException {

    /* your synonym map of keywords and you have to populate 
    it before the user can give values.*/ 
    HashMap<String, String[]> synonymMap = new HashMap<String, String[]>();
    populateSynonymMap(); //populate the map


    /*Get user response*/ 
    System.out.println("What would you like to know?");
    System.out.print("> ");
    Scanner scanner = new Scanner(System.in);
    String input = scanner.nextLine().toLowerCase();

    /*first check if the input already is the original one*/
    if (synonymMap.containsKey(input)) {
        parseFile(input);
    } else {
        /*if not then search the synonymMap to get the original keyword*/
        for (Map.Entry<String,String[]> entry : synonymMap.entrySet()) {

            String[] value = entry.getValue();                
            if (Arrays.asList(value).contains(input)) {
                parseFile(entry.getKey());
                break;
            }
        }
    }

}

May be you can try the following code. 可能您可以尝试以下代码。 This does what you like to accomplish. 这就是您想要完成的事情。 But I think you need to think of redesigning the way you are looking up keyword synonym. 但是我认为您需要考虑重新设计查找关键字同义词的方式。

And if you are facing issues only populating the hashmap it may be because you are defining map inside main method and then not populating it really anywhere else. 如果仅在填充哈希表时遇到问题,那可能是因为您在main方法中定义了map,然后没有真正在其他任何地方填充它。

So for the second option, you can change assignment in main method like .. 因此,对于第二个选项,您可以在..之类的主方法中更改分配。

    synonymMap = populateSynonymMap(); // populate the map

And the sample method to populate the map. 和样本方法来填充地图。

private static HashMap<String, String[]> populateSynonymMap() {
    HashMap<String, String[]> synonymMap = new HashMap<String, String[]>();

    String s[] = new String[] { "x1", "x2", "x3" };
    String t[] = new String[] { "y1", "y2", "y3" };
    String u[] = new String[] { "z1", "z2", "z3" };

    synonymMap.put("x", s);
    synonymMap.put("y", t);
    synonymMap.put("z", u);

    return synonymMap;

}

You must define a method accepting a Map to populate 您必须定义一个接受Map的方法来填充

public void populateSynonymMap(Map<String, String[]> synonymsMap)

or declare your Map as attribute in the class to be visible in the method. 或在类中将Map声明为属性以在方法中可见。

Then inside method you must have a word and synonyms array: 然后在方法内部,您必须有一个单词和同义词数组:

String word = "synonym";
String[] synonyms = "equivalent,methonim".split(",");

Then put both in the map: 然后将两者都放入地图中:

synonimsMap.put(word, synonyms);

Do it as many times as you need (if a lot, define 2 arrays, one String[] for words and another String[][] for synonims and iterate over them to populate the map in an easier way... 根据需要进行多次(如果需要的话,请定义2个数组,一个String[]用于单词,另一个String[][]用于同义词,并对其进行迭代以更容易的方式填充地图...

String[] words = // populate word array
String[][] synonims = // populate synonims 2d array

for (int c = 0; c < words.length; c++) {
    synonimsMap.put(words[c], synonyms[c]);
}

Hope this guides you enough... 希望这对您有足够的指导...

Rather than declaring you HashMap as 与其将您的HashMap声明为

HashMap<String, String[]> synonymMap = new HashMap<String, String[]>();

Why don't you declare is as 你为什么不宣布是

HashMap<String, List<String>> synonymMap = new HashMap<String, ArrayList<String>>();

& then arrayListObj.add(entry.getValue()); &然后arrayListObj.add(entry.getValue());

then it will obviate the need for this Arrays.asList(value) 那么它将消除对此Arrays.asList(value)

Initialize the method like 像这样初始化方法

public void populateSynonymMap(String key, List<String> value){
    synonymMap.put(key, value);
    }

Input values in the List 列表中的输入值

List<String> listOne=new ArrayList<String>();
listOne.add("Input String"); //Keep on adding the Strings in this way
//then call the populateSynonymMap method
//Say your key is A
populateSynonymMap("A", listOne);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM