Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ArrayList as key in HashMap

Tags:

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; } 
like image 862
thetna Avatar asked Apr 02 '12 09:04

thetna


People also ask

Can HashMap have ArrayList as key?

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.

Can HashMap have ArrayList as value?

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.

Does HashMap use ArrayList?

HashMap implements the Map interface. The List interface is implemented by both ArrayList and LinkedList. LinkedList additionally implements the Queue interface.

What can be used as key in HashMap?

Hashmap is based on Hashtable. It can allow null values and null keys.


2 Answers

Yes you can have ArrayLists 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);     } } 
like image 76
aioobe Avatar answered Oct 05 '22 12:10

aioobe


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).

like image 24
Alex Stybaev Avatar answered Oct 05 '22 11:10

Alex Stybaev