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

Recommended Answers

All 2 Replies

Have you tried posting this in the Java forum?

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.