Java – use HashSet to upload and deliver intent in ArrayList?

Imagine that I need to create a collection of elements in which the order can or doesn't matter All I'm going to do is use iterators I noticed that most colleagues use ArrayList and linkedhashset / HashSet My question is, if I know that these elements should be unique, should I use a collection or list? In fact, it doesn't really make a difference, but it doesn't convey that these elements are unique more effectively?

I think this is an interesting problem for large enterprise applications for the following reasons: 1) if you can't guarantee the overall quality of your code, using set may be dangerous Why? Because equals () & hash codes may be overwritten incorrectly, using set can cause some very annoying problems 2) Use lists to better adapt to future changes If any reason for repetition becomes possible, don't worry

Basically it boils down to: if I know I should expect unique elements, should I like to set the list in all cases?

Editor: I think I should also ask: should I use set to ensure that no duplicates are added, or can it only be used to indicate that there are no duplicates for ease of understanding?

Solution

1) Complete hypocrisy Don't fix the errors, fix them

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>