Sekar
Sekar

Reputation: 387

Search Algorithm in Java Collection

This question might have been answered. I couldn't find it though.

In Java Collections i have always used Collections.sort(coll) followed by search. My question is that will collections always perform binary search or is it decided internally. Does collection store any identifier to decide between linear and binary search.

Upvotes: 0

Views: 327

Answers (1)

Serkan Yıldırım
Serkan Yıldırım

Reputation: 191

When you look at the Java 8 source code of Collections.sort(coll), it uses the Arrays.sort(Object[] a) method. If you do not pass java.util.Arrays.useLegacyMergeSort as a system property. Otherwise, it uses legacy merge sort.

Arrays.sort(Object[] a) uses TimSort which is strategic algorithm that chooses insertion sort or merge sort according to the given array's ordering and size. You can find the details in below links:

Upvotes: 3

Related Questions