什么是删除Java中的一个Array中的重复的最佳方式? [英] What is the best way to remove duplicates in an Array in Java?

查看:102
本文介绍了什么是删除Java中的一个Array中的重复的最佳方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个对象数组,需要删除/过滤重复的对象。
我要去覆盖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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆