Does the contains() method in Java ArrayList use binary search? Or do I need to use Collections to do this instead?
No, you need to use Collections
to use binary search, usually after sorting it. An ArrayList
doesn't know anything about its ordering, and you have to know a list is sorted before you can use binary search.
Alternately, you could use TreeSet
, which is as efficient as using a binary search.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With