Java threads: distinct () in a pre-sorted thread?

As discussed in this question , the implementation of distinct() can use a more efficient algorithm when the thread in which it works is known by the runtime sorted. How can we achieve a similar result if we know that the stream is sorted (for example, because it comes from an external pre-sorted data source, such as an SQL query with the order by clause), but is not marked as such? There is an unordered() operation that removes the ordering flags, but as far as I see no way to tell the system that the data was ordered from the outside.

+7
java performance java-stream
source share
1 answer

You can create your own separator around an existing collection, for example:

  List<Integer> list = Arrays.asList(1, 2, 3, 4); Spliterator<Integer> sp = Spliterators.spliterator(list, Spliterator.SORTED); System.out.println(sp.hasCharacteristics(Spliterator.SORTED)); // true 
+3
source share

All Articles