Java: Avoid inserting duplicate in arraylist

后端 未结 6 1455
太阳男子
太阳男子 2020-11-29 08:56

I am novice to java. I have an ArrayList and I want to avoid duplicates on insertion. My ArrayList is

ArrayList karList         


        
6条回答
  •  天命终不由人
    2020-11-29 09:11

    You need to use any Set implementation, e.g you can use HashSet. If you want to add custom object kar into your HashSet, you need to override equals and hashcode method. You can read more about equals and hashcode, see

提交回复
热议问题