什么是删除Java中的一个Array中的重复的最佳方式? [英] What is the best way to remove duplicates in an Array in Java?
问题描述
我要去覆盖equals& hachCode上的对象元素,然后把它们粘在一个Set ...但是我想我应该至少轮询stackoverflow看看是否有另一种方法,或许一些其他API的聪明的方法?
我同意您的方法来覆盖 hashCode()
和 equals )
并使用实现 Set
的东西。
这样做也使它绝对清晰对于任何其他开发人员来说,不需要重复的特征。
另一个原因 - 您最好选择一个满足您需求的实现:
,您不必更改代码以便将来更改实施。
I have an Array of Objects that need the duplicates removed/filtered. I was going to just override equals & hachCode on the Object elements, and then stick them in a Set... but I figured I should at least poll stackoverflow to see if there was another way, perhaps some clever method of some other API?
I would agree with your approach to override hashCode()
and equals()
and use something that implements Set
.
Doing so also makes it absolutely clear to any other developers that the non-duplicate characteristic is required.
Another reason - you get to choose an implementation that meets your needs best now:
and you don't have to change your code to change the implementation in the future.
这篇关于什么是删除Java中的一个Array中的重复的最佳方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!