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;
}
I've come up with this solution. It is obviously not usable in all cases, for example over stepping the hashcodes int capacity, or list.clone() complications(if the input list gets changed, key stays the same as intended, but when the items of List are mutable, cloned list has the same reference to its items, which would result in changing the key itself).
Sure it possible. I suppose the issue in your
put
. Try obtain key for bigram, increment it, remove entry with this bigram and insert updated valueArrayList.equals()
is inherited fromjava.lang.Object
- thereforeequals()
on ArrayList is independent of the content of the list.If you want to use an ArrayList as a map key, you will need to override
equals()
andhashcode()
in order to make two arraylists with the same content in the same order return true on a call toequals()
and return the same hashcode on a call tohashcode()
.Is there any particular reason you have to use an ArrayList as opposed to say a simple String as the key?
edit: Ignore me, as Joachim Sauer pointed out below, I am so wrong it's not even funny.
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 samehashCode
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:Why can't you use something like this:
instead of using the dynamic collection for limited number of items (two).
Please check below my code in order to understand if key is ArrayList in Map and how JVM will do it for inputs: here i write hashCode and equals method for TesthashCodeEquals class.
.
so this will check the number of objects in List and the values of valriabe in object. if number of objects are same and the values of instance variables is also same then it will consider duplicate key and override the key.
now if i change only the value of object on list3
list3.add(new TesthashCodeEquals(2, 2));
then it will print:
so that It always check the number of objects in List and the value of instance variable of object.
thanks