ArrayList as key in HashMap

前端 未结 9 1863
Happy的楠姐
Happy的楠姐 2020-11-29 09:17

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

9条回答
  •  情书的邮戳
    2020-11-29 10:02

    ArrayList.equals() is inherited from java.lang.Object - therefore equals() 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() and hashcode() in order to make two arraylists with the same content in the same order return true on a call to equals() and return the same hashcode on a call to hashcode().

    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.

提交回复
热议问题