I have a list of more than 37K items, and I already implemented hashCode()
, equals()
, so I wonder Collections.binarySearch()
can help
You'll get even better performance by using a HashSet. That will take more space, though.
in order for binarySearch() to work, your list must be sorted. equals() and hashCode() have nothing to do with sorting. your objects need to be Comparable, or you must have a relevant Comparator. either way, you must sort the List first.
and yes, assuming the list is sorted, then you will probably get better performance from binarySearch() as compared to indexOf().
If your collection is sorted, binarySearch()
will be O(log n) as opposed to indexOf()
's O(n) and you will definitely see an improvement.