Finding duplicates in a List ignoring a field

前端 未结 4 719
我寻月下人不归
我寻月下人不归 2021-01-22 12:38

I\'ve got a List of Persons and I want to find duplicate entries, consindering all fields except id. So using the equals()-method (and in

4条回答
  •  不要未来只要你来
    2021-01-22 13:19

    You can use Java HashMap using pairs. Map map = new HashMap(). Also, some form of Comparator implementation to go with. If you check with containsKey or containsValue methods and find out you already have something (i.e. you are trying to add a duplicate, keep them in your original list. Otherwise, pop them out. In this way, you will end up with a list with the elements that were duplicates in your original list. TreeSet<,>will be another option, but I haven't used it yet so cannot offer advice.

提交回复
热议问题