简体   繁体   中英

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.

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:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM