3 ms·
Plus I don't know about you, but I don't sort (or really use) arrays in Java very much. I see a lot more of the Collections.sort() methods, particularly the com
by dcminter 4y ago
Plus I don't know about you, but I don't sort (or really use) arrays in Java very much. I see a lot more of the Collections.sort() methods, particularly the comparator based one.
- marginalia_nu 4y agoSorting arrays of primitives ought to be nearly as frequent as the need for sorted arrays of primitives due to the downright comical overhead of List<Integer>, but sorting arrays of objects has be rare in Java code written this millennium.
- dcminter 4y agoI mean I don't sort List<Integer> more than once in a blue moon either. I'm much more likely to either be sorting a list of model objects with a comparator, or manipulating a collection of objects that's already been sorted by some external source. In the unlikely event that I'm modeling something that can be represented as a list of integers AND it's a significant performance bottleneck then, sure, I'll consider the array methods. But I don't recall the need arising in the last year or two. Enterprise Java stuff just isn't often bottlenecked on that sort of thing. Network latency fills far more of my dreams ;)
- marginalia_nu 4y agoMaybe we're seeing different sides of the language then. I'm doing a lot of integer-list sorting, building a search engine in java. I'm evenly split between Arrays.sort or GNU Trove's specialized primitive collections. The overhead of List<Integer> really can not be overstated, it can approach order 1000% memory consumption, speed is prima facie about half for dealing with collections, but in practice it can be much lower from the GC churn. Even for lists as small as a million entries this can be quite untenable.
- Scarbutt 4y agoMaybe we're seeing different sides of the language then. You are not seeing different sides of the language but participating in different domains, application/business logic programming vs more system level stuff.
- layer8 4y agoCollections.sort() just copies the list into an array, sorts the array, and copies the sorted elements back into the list. So it also just sorts an array under the hood. To eliminate the temporary array (at least for ArrayList), the List#sort() method was added in Java 8. Arrays are usually still preferred over collections in the case of primitive types (int[] etc.).