0

I am going to create a list in java, what would be the best thing to use to check whether or not there are duplicates in the list:

1.x.compareTo(y) (Can I use this one?)
2.x.equals(y)
3. x == y

Thanks

Edited by london-G

3
Contributors
2
Replies
4
Views
4 Years
Discussion Span
Last Post by zeroliken
1

the first one checks for order if it's greater than, less than or equal to the object it is being compared with and returns a specified value given the result

the equals method checks for the equivalence of the objects with their values

the == works similarly as the equals method but it also checks wether the references to the object are equal

my opinion is since you only need to check if the list have duplicates the first option wouldn't be necessary
if the list contains elements that have primitive data types use '==' else use equals method
you can test each option and see for yourself which is appropriate, if you have doubts you can always check the API

Edited by zeroliken

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.