Would it be possible to add an ArrayList
as the key of HashMap
. I would like to keep the frequency count of bigrams. The bigram is the key and the value is its frequency.
For each of the bigrams like "he is", I create an ArrayList
for it and insert it into the HashMap
. But I am not getting the correct output.
public HashMap<ArrayList<String>, Integer> getBigramMap(String word1, String word2) { HashMap<ArrayList<String>, Integer> hm = new HashMap<ArrayList<String>, Integer>(); ArrayList<String> arrList1 = new ArrayList<String>(); arrList1 = getBigram(word1, word2); if (hm.get(arrList1) != null) { hm.put(arrList1, hm.get(arrList1) + 1); } else { hm.put(arrList1, 1); } System.out.println(hm.get(arrList1)); return hm; } public ArrayList<String> getBigram(String word1, String word2) { ArrayList<String> arrList2 = new ArrayList<String>(); arrList2.add(word1); arrList2.add(word2); return arrList2; }
Yes you can have ArrayList s as a keys in a hash map, but it is a very bad idea since they are mutable. If you change the ArrayList in any way (or any of its elements), the mapping will basically be lost, since the key won't have the same hashCode as it had when it was inserted.
As HashMap contains key-value pairs, there are three ways you can convert given HashMap to ArrayList. You can convert HashMap keys into ArrayList or you can convert HashMap values into ArrayList or you can convert key-value pairs into ArrayList. Let's see these three methods in detail.
HashMap implements the Map interface. The List interface is implemented by both ArrayList and LinkedList. LinkedList additionally implements the Queue interface.
Hashmap is based on Hashtable. It can allow null values and null keys.
Yes you can have ArrayList
s as a keys in a hash map, but it is a very bad idea since they are mutable.
If you change the ArrayList
in any way (or any of its elements), the mapping will basically be lost, since the key won't have the same hashCode
as it had when it was inserted.
The rule of thumb is to use only immutable data types as keys in a hash map. As suggested by Alex Stybaev, you probably want to create a Bigram
class like this:
final class Bigram { private final String word1, word2; public Bigram(String word1, String word2) { this.word1 = word1; this.word2 = word2; } public String getWord1() { return word1; } public String getWord2() { return word2; } @Override public int hashCode() { return word1.hashCode() ^ word2.hashCode(); } @Override public boolean equals(Object obj) { return (obj instanceof Bigram) && ((Bigram) obj).word1.equals(word1) && ((Bigram) obj).word2.equals(word2); } }
Why can't you use something like this:
class Bigram{ private String firstItem; private String secondItem; <getters/setters> @Override public int hashCode(){ ... } @Override public boolean equals(){ ... } }
instead of using the dynamic collection for limited number of items (two).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With