|
| 1 | +import org.junit.Before; |
| 2 | +import org.junit.Test; |
| 3 | + |
| 4 | +import java.util.*; |
| 5 | +import java.util.function.Function; |
| 6 | +import java.util.logging.Level; |
| 7 | +import java.util.logging.Logger; |
| 8 | +import java.util.stream.Collectors; |
| 9 | +import java.util.stream.IntStream; |
| 10 | + |
| 11 | +import static org.junit.Assert.assertEquals; |
| 12 | + |
| 13 | + |
| 14 | +public class StreamsOrderingTest { |
| 15 | + |
| 16 | + Logger logger = Logger.getLogger( StreamsOrderingTest.class.getName()); |
| 17 | + |
| 18 | + @Before |
| 19 | + public void setUp() throws Exception { |
| 20 | + logger.setLevel(Level.ALL); |
| 21 | + } |
| 22 | + |
| 23 | + @Test |
| 24 | + public void givenTwoCollections_whenStreamed_thenCheckOutputDifferent(){ |
| 25 | + |
| 26 | + List<String> list = Arrays.asList("B", "A", "C", "D", "F"); |
| 27 | + Set<String> set = new TreeSet<>(Arrays.asList("B", "A", "C", "D", "F")); |
| 28 | + |
| 29 | + Object[] listOutput = list.stream().toArray(); |
| 30 | + Object[] setOutput = set.stream().toArray(); |
| 31 | + |
| 32 | + assertEquals("[B, A, C, D, F]", Arrays.toString(listOutput)); |
| 33 | + assertEquals("[A, B, C, D, F]", Arrays.toString(setOutput)); |
| 34 | + |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + public void givenTwoCollections_whenStreamedInParallel_thenCheckOutputDifferent(){ |
| 39 | + |
| 40 | + List<String> list = Arrays.asList("B", "A", "C", "D", "F"); |
| 41 | + Set<String> set = new TreeSet<>(Arrays.asList("B", "A", "C", "D", "F")); |
| 42 | + |
| 43 | + Object[] listOutput = list.stream().parallel().toArray(); |
| 44 | + Object[] setOutput = set.stream().parallel().toArray(); |
| 45 | + |
| 46 | + assertEquals("[B, A, C, D, F]", Arrays.toString(listOutput)); |
| 47 | + assertEquals("[A, B, C, D, F]", Arrays.toString(setOutput)); |
| 48 | + |
| 49 | + } |
| 50 | + |
| 51 | + |
| 52 | + |
| 53 | + @Test |
| 54 | + public void givenOrderedInput_whenUnorderedAndOrderedCompared_thenCheckUnorderedOutputChanges(){ |
| 55 | + Set<Integer> set = new TreeSet<>( |
| 56 | + Arrays.asList(-9, -5, -4, -2, 1, 2, 4, 5, 7, 9, 12, 13, 16, 29, 23, 34, 57, 68, 90, 102, 230)); |
| 57 | + |
| 58 | + Object[] orderedArray = set.stream() |
| 59 | + .parallel() |
| 60 | + .limit(5) |
| 61 | + .toArray(); |
| 62 | + Object[] unorderedArray = set.stream() |
| 63 | + .unordered() |
| 64 | + .parallel() |
| 65 | + .limit(5) |
| 66 | + .toArray(); |
| 67 | + |
| 68 | + logger.info(Arrays.toString(orderedArray)); |
| 69 | + logger.info(Arrays.toString(unorderedArray)); |
| 70 | + } |
| 71 | + |
| 72 | + |
| 73 | + @Test |
| 74 | + public void givenUnsortedStreamInput_whenStreamSorted_thenCheckOrderChanged(){ |
| 75 | + |
| 76 | + List<Integer> list = Arrays.asList(-3,10,-4,1,3); |
| 77 | + |
| 78 | + Object[] listOutput = list.stream().toArray(); |
| 79 | + Object[] listOutputSorted = list.stream().sorted().toArray(); |
| 80 | + |
| 81 | + assertEquals("[-3, 10, -4, 1, 3]", Arrays.toString(listOutput)); |
| 82 | + assertEquals("[-4, -3, 1, 3, 10]", Arrays.toString(listOutputSorted)); |
| 83 | + |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + public void givenUnsortedStreamInput_whenStreamDistinct_thenShowTimeTaken(){ |
| 88 | + long start, end; |
| 89 | + start = System.currentTimeMillis(); |
| 90 | + IntStream.range(1,1_000_000).unordered().parallel().distinct().toArray(); |
| 91 | + end = System.currentTimeMillis(); |
| 92 | + System.out.println(String.format("Time taken when unordered: %d ms", (end - start))); |
| 93 | + } |
| 94 | + |
| 95 | + |
| 96 | + @Test |
| 97 | + public void givenSameCollection_whenStreamTerminated_thenCheckEachVsEachOrdered(){ |
| 98 | + |
| 99 | + List<String> list = Arrays.asList("B", "A", "C", "D", "F"); |
| 100 | + |
| 101 | + list.stream().parallel().forEach(e -> logger.log(Level.INFO, e)); |
| 102 | + list.stream().parallel().forEachOrdered(e -> logger.log(Level.INFO, e)); |
| 103 | + |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + public void givenSameCollection_whenStreamCollected_thenCheckOutput(){ |
| 108 | + |
| 109 | + List<String> list = Arrays.asList("B", "A", "C", "D", "F"); |
| 110 | + |
| 111 | + List<String> collectionList = list.stream().parallel().collect(Collectors.toList()); |
| 112 | + Set<String> collectionSet = list.stream().parallel().collect(Collectors.toCollection(TreeSet::new)); |
| 113 | + |
| 114 | + assertEquals("[B, A, C, D, F]", collectionList.toString()); |
| 115 | + assertEquals("[A, B, C, D, F]", collectionSet.toString()); |
| 116 | + |
| 117 | + } |
| 118 | + |
| 119 | + |
| 120 | + @Test |
| 121 | + public void givenListIterationOrder_whenStreamCollectedToMap_thenCeckOrderChanged() { |
| 122 | + List<String> list = Arrays.asList("A", "BB", "CCC"); |
| 123 | + |
| 124 | + Map<String, Integer> hashMap = list.stream().collect(Collectors.toMap(Function.identity(), String::length)); |
| 125 | + |
| 126 | + Object[] keySet = hashMap.keySet().toArray(); |
| 127 | + |
| 128 | + assertEquals("[BB, A, CCC]", Arrays.toString(keySet)); |
| 129 | + |
| 130 | + } |
| 131 | + |
| 132 | + @Test |
| 133 | + public void givenListIteration_whenStreamCollectedtoHashMap_thenCheckOrderMaintained() { |
| 134 | + List<String> list = Arrays.asList("A", "BB", "CCC", "CCC"); |
| 135 | + |
| 136 | + Map<String, Integer> linkedHashMap = list.stream().collect(Collectors.toMap( |
| 137 | + Function.identity(), |
| 138 | + String::length, |
| 139 | + (u, v) -> u, |
| 140 | + LinkedHashMap::new |
| 141 | + )); |
| 142 | + |
| 143 | + Object[] keySet = linkedHashMap.keySet().toArray(); |
| 144 | + |
| 145 | + assertEquals("[A, BB, CCC]", Arrays.toString(keySet)); |
| 146 | + } |
| 147 | + |
| 148 | +} |
0 commit comments