Encounter Order Collections - Best Practice and Example

Sorting

Streams may or may not have a specific order of meetings. One way or another, the stream has an order of meetings, depending on the source and intermediate operations. Certain stream sources (such as List or arrays) are internally ordered, while others (such as HashSet) are not. Some intermediate operations, such as sorted (), may order the meeting in an unordered stream, while others may make an ordered stream unordered, such as BaseStream.unordered (). In addition, some terminal operations may ignore the order of meetings, such as Foreach ().

  • Are there other types that do not have a meeting order property, HashSeteh?
  • If I am not interested in maintaining the existing order or some sorting, is it considered that it is best practice to explicitly call unorderedan intermediate operation for each thread that will be computed in parallel?
+4
source share
3 answers

In addition to the collection HashSetand HashMaps views , an Stream.generate()unordered stream will generate.

Needless to say, the threads generated by using Randomare also disordered. In addition, it Stream.empty()does not report that it has the order of meetings, but it does not really matter ...

, Stream , unordered() - , , . , , distinct(), .

, , findAny() findFirst() , , , .

+5

Encounter order - , . . ArrayList , .

  • HashSet, HashMap .
  • collect , . stream() . . , , - :

    List<Integer> list = Arrays.asList(1,2,3);
    int sum = list.stream().collect(Collectors.summingInt(e -> e));
    

    , .

+4

. , unordered . , .

. , , . , . 4- . , , , , ; .

, List ; .

, , , . , .

findFirst findAny . , 8 , . , 7 , , - , . , findAny ...

+2

Source: https://habr.com/ru/post/1678433/


All Articles