|
| 1 | +package com.baeldung.bifunction; |
| 2 | + |
| 3 | +import org.junit.Test; |
| 4 | + |
| 5 | +import java.util.ArrayList; |
| 6 | +import java.util.Arrays; |
| 7 | +import java.util.Comparator; |
| 8 | +import java.util.List; |
| 9 | +import java.util.function.BiFunction; |
| 10 | +import java.util.stream.Collectors; |
| 11 | +import java.util.stream.Stream; |
| 12 | + |
| 13 | +import static org.assertj.core.api.Assertions.assertThat; |
| 14 | + |
| 15 | +public class BiFunctionalInterfacesUnitTest { |
| 16 | + @Test |
| 17 | + public void givenStreamValues_whenMappedToNewValues() { |
| 18 | + List<String> mapped = Stream.of("hello", "world") |
| 19 | + .map(word -> word + "!") |
| 20 | + .collect(Collectors.toList()); |
| 21 | + |
| 22 | + assertThat(mapped).containsExactly("hello!", "world!"); |
| 23 | + } |
| 24 | + |
| 25 | + @Test |
| 26 | + public void givenStreamValues_whenReducedWithPrefixingOperation() { |
| 27 | + String result = Stream.of("hello", "world") |
| 28 | + .reduce("", (a, b) -> b + "-" + a); |
| 29 | + |
| 30 | + assertThat(result).isEqualTo("world-hello-"); |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + public void givenStreamValues_whenReducedWithPrefixingLambda_thenHasNoTrailingDash() { |
| 35 | + String result = Stream.of("hello", "world") |
| 36 | + .reduce("", (a, b) -> combineWithoutTrailingDash(a, b)); |
| 37 | + |
| 38 | + assertThat(result).isEqualTo("world-hello"); |
| 39 | + } |
| 40 | + |
| 41 | + private String combineWithoutTrailingDash(String a, String b) { |
| 42 | + if (a.isEmpty()) { |
| 43 | + return b; |
| 44 | + } |
| 45 | + return b + "-" + a; |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + public void givenStreamValues_whenReducedWithPrefixingMethodReference_thenHasNoTrailingDash() { |
| 50 | + String result = Stream.of("hello", "world") |
| 51 | + .reduce("", this::combineWithoutTrailingDash); |
| 52 | + |
| 53 | + assertThat(result).isEqualTo("world-hello"); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void givenTwoLists_whenCombined() { |
| 58 | + List<String> list1 = Arrays.asList("a", "b", "c"); |
| 59 | + List<Integer> list2 = Arrays.asList(1, 2, 3); |
| 60 | + |
| 61 | + List<String> result = new ArrayList<>(); |
| 62 | + for (int i=0; i < list1.size(); i++) { |
| 63 | + result.add(list1.get(i) + list2.get(i)); |
| 64 | + } |
| 65 | + |
| 66 | + assertThat(result).containsExactly("a1", "b2", "c3"); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void givenTwoLists_whenCombinedWithGeneralPurposeCombiner() { |
| 71 | + List<String> list1 = Arrays.asList("a", "b", "c"); |
| 72 | + List<Integer> list2 = Arrays.asList(1, 2, 3); |
| 73 | + |
| 74 | + List<String> result = listCombiner(list1, list2, (a, b) -> a + b); |
| 75 | + |
| 76 | + assertThat(result).containsExactly("a1", "b2", "c3"); |
| 77 | + } |
| 78 | + |
| 79 | + private static <T, U, R> List<R> listCombiner(List<T> list1, |
| 80 | + List<U> list2, |
| 81 | + BiFunction<T, U, R> combiner) { |
| 82 | + List<R> result = new ArrayList<>(); |
| 83 | + for (int i = 0; i < list1.size(); i++) { |
| 84 | + result.add(combiner.apply(list1.get(i), list2.get(i))); |
| 85 | + } |
| 86 | + return result; |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + public void givenTwoLists_whenComparedWithCombiningFunction() { |
| 91 | + List<Double> list1 = Arrays.asList(1.0d, 2.1d, 3.3d); |
| 92 | + List<Float> list2 = Arrays.asList(0.1f, 0.2f, 4f); |
| 93 | + |
| 94 | + // algorithm to determine if the value in list1 > value in list 2 |
| 95 | + List<Boolean> result = listCombiner(list1, list2, (a, b) -> a > b); |
| 96 | + |
| 97 | + assertThat(result).containsExactly(true, true, false); |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + public void givenTwoLists_whenComparedWithCombiningFunctionByMethodReference() { |
| 102 | + List<Double> list1 = Arrays.asList(1.0d, 2.1d, 3.3d); |
| 103 | + List<Float> list2 = Arrays.asList(0.1f, 0.2f, 4f); |
| 104 | + |
| 105 | + // algorithm to determine if the value in list1 > value in list 2 |
| 106 | + List<Boolean> result = listCombiner(list1, list2, this::firstIsGreaterThanSecond); |
| 107 | + |
| 108 | + assertThat(result).containsExactly(true, true, false); |
| 109 | + } |
| 110 | + |
| 111 | + private boolean firstIsGreaterThanSecond(Double a, Float b) { |
| 112 | + return a > b; |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + public void givenTwoLists_whenComparedForEqualityByCombiningFunction() { |
| 117 | + List<Float> list1 = Arrays.asList(0.1f, 0.2f, 4f); |
| 118 | + List<Float> list2 = Arrays.asList(0.1f, 0.2f, 4f); |
| 119 | + |
| 120 | + List<Boolean> result = listCombiner(list1, list2, (a, b) -> a.equals(b)); |
| 121 | + |
| 122 | + assertThat(result).containsExactly(true, true, true); |
| 123 | + } |
| 124 | + |
| 125 | + @Test |
| 126 | + public void givenTwoLists_whenComparedForEqualityByCombiningFunctionWithMethodReference() { |
| 127 | + List<Float> list1 = Arrays.asList(0.1f, 0.2f, 4f); |
| 128 | + List<Float> list2 = Arrays.asList(0.1f, 0.2f, 4f); |
| 129 | + |
| 130 | + List<Boolean> result = listCombiner(list1, list2, Float::equals); |
| 131 | + |
| 132 | + assertThat(result).containsExactly(true, true, true); |
| 133 | + } |
| 134 | + |
| 135 | + @Test |
| 136 | + public void givenTwoLists_whenComparedWithCombiningFunctionWithCompareTo() { |
| 137 | + List<Double> list1 = Arrays.asList(1.0d, 2.1d, 3.3d); |
| 138 | + List<Double> list2 = Arrays.asList(0.1d, 0.2d, 4d); |
| 139 | + |
| 140 | + List<Integer> result = listCombiner(list1, list2, Double::compareTo); |
| 141 | + |
| 142 | + assertThat(result).containsExactly(1, 1, -1); |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * Allows you to to pass in a lambda or method reference and then |
| 147 | + * get access to the BiFunction it is meant to become |
| 148 | + */ |
| 149 | + private static <T, U, R> BiFunction<T, U, R> asBiFunction(BiFunction<T, U, R> function) { |
| 150 | + return function; |
| 151 | + } |
| 152 | + |
| 153 | + @Test |
| 154 | + public void givenTwoLists_whenComparedWithCombiningFunctionWithComposedBiFunction() { |
| 155 | + List<Double> list1 = Arrays.asList(1.0d, 2.1d, 3.3d); |
| 156 | + List<Double> list2 = Arrays.asList(0.1d, 0.2d, 4d); |
| 157 | + |
| 158 | + List<Boolean> result = listCombiner(list1, list2, |
| 159 | + asBiFunction(Double::compareTo) |
| 160 | + .andThen(i -> i > 0)); |
| 161 | + |
| 162 | + assertThat(result).containsExactly(true, true, false); |
| 163 | + } |
| 164 | +} |
0 commit comments