简体   繁体   中英

Adding an element to an a HashMap in an ArrayList of HashMap in Java

I have a source HashMap in Java:

HashMap<String, Integer> keyWordFrequencies;

Storing keywords of various length. I want to traverse this HashMap and work out the lengths of the ngrams stored in the String part of the map which defines the text of each keyword.

With this data, I want to populate a target ArrayList of HashMaps:

ArrayList<HashMap<String, Integer>> keywordNgrams;

With the results, where the index of the ArrayList corresponds to the ngram size of a given keyword minus one, ie keywordNGrams(0) will receive the unigrams, keywordNGrams(1) will receive the bigrams and so on. But I'm not sure of the necessary syntax. Traversing the source HashMap is easy enough:

Set keyWordFrequenciesSet = keyWordFrequencies.entrySet();
Iterator keyWordFrequenciesIterator = keyWordFrequenciesSet.iterator();
while(keyWordFrequenciesIterator.hasNext()) {
   Map.Entry m = (Map.Entry) keyWordFrequenciesIteratorIterator.next();
   int ngramLength = String_Utils.getLengthOfNgram(m.getKey().toString());

   Add element to keywordNgrams?

But adding the element to the target ArrayList of HashMap is confusing me. I have tried:

keywordNgrams.add(ngramLength, m);

And various alternatives but to no avail. m should be an element of a HashMap, not a HashMap in itself. Can anyone suggest where I am wrong?

Ideally, I would like to traverse the source HashMap keyWordFrequencies once, and the keywordNgrams ArrayList is initialised to the largest possible ngram size to start with.

Since you're dealing with an ArrayList of size 5, I would suggest that when you initialize your ArrayList, do so by adding a new instance of a HashMap at each index. Something like this:

ArrayList<HashMap<String, Integer>> keywordNgrams = new ArrayList<HashMap<String, Integer>> ();

for(int index = 0; index < 5; index++){
  keywordNgrams .put(index, new HashMap<String, Integer>());
}

In order to add elements in your ArrayList, here's what you've to do:

  • Access the specific HashMap for a specified 'n'-gram. This you can do using get(int index) on the ArrayList
  • You would then add the element your returned HashMap and then again do a set(int index, E element) of the same HashMap to your keywordNgrams ArrayList.

A sample code might be something like this:

HashMap<String, Integer> returnedMap = keywordNgrams.get(index); //where index is the position in the list;
returnedMap.put(key, value); //where key & value is the information that you would want to add to your HashMap
keywordNgrams.set(index, returnedMap);

Since you know the greatest ngram-size, I recommend prepopulating keywordNgrams :

List<Map<String, Integer>> keywordNgrams =
    new ArrayList<Map<String, Integer>>();
for(int i = 0; i < 5; ++i)
    keywordNgrams.add(new HashMap<String, Integer>());

Then you can write:

for(final String keyword : keyWordFrequencies.keySet())
    keywordNgrams.get(String_Utils.getLengthOfNgram(keyword) - 1)
            .put(keyword, keyWordFrequencies.get(keyword));

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