|
| 1 | +package com.baeldung.functionalinterface; |
| 2 | + |
| 3 | +import java.util.Arrays; |
| 4 | +import java.util.HashMap; |
| 5 | +import java.util.List; |
| 6 | +import java.util.Map; |
| 7 | +import java.util.concurrent.TimeUnit; |
| 8 | +import java.util.function.Function; |
| 9 | +import java.util.function.Supplier; |
| 10 | +import java.util.stream.Collectors; |
| 11 | +import java.util.stream.Stream; |
| 12 | + |
| 13 | +import com.google.common.util.concurrent.Uninterruptibles; |
| 14 | +import org.junit.Test; |
| 15 | + |
| 16 | +import static org.junit.Assert.*; |
| 17 | + |
| 18 | +public class FunctionalInterfaceTest { |
| 19 | + |
| 20 | + @Test |
| 21 | + public void whenPassingLambdaToComputeIfAbsent_thenTheValueGetsComputedAndPutIntoMap() { |
| 22 | + |
| 23 | + Map<String, Integer> nameMap = new HashMap<>(); |
| 24 | + Integer value = nameMap.computeIfAbsent("John", s -> s.length()); |
| 25 | + |
| 26 | + assertEquals(new Integer(4), nameMap.get("John")); |
| 27 | + assertEquals(new Integer(4), value); |
| 28 | + |
| 29 | + } |
| 30 | + |
| 31 | + @Test |
| 32 | + public void whenPassingMethodReferenceToComputeIfAbsent_thenTheValueGetsComputedAndPutIntoMap() { |
| 33 | + |
| 34 | + Map<String, Integer> nameMap = new HashMap<>(); |
| 35 | + Integer value = nameMap.computeIfAbsent("John", String::length); |
| 36 | + |
| 37 | + assertEquals(new Integer(4), nameMap.get("John")); |
| 38 | + assertEquals(new Integer(4), value); |
| 39 | + |
| 40 | + } |
| 41 | + |
| 42 | + public byte[] transformArray(short[] array, ShortToByteFunction function) { |
| 43 | + byte[] transformedArray = new byte[array.length]; |
| 44 | + for (int i = 0; i < array.length; i++) { |
| 45 | + transformedArray[i] = function.applyAsByte(array[i]); |
| 46 | + } |
| 47 | + return transformedArray; |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void whenUsingCustomFunctionalInterfaceForPrimitives_thenCanUseItAsLambda() { |
| 52 | + |
| 53 | + short[] array = {(short) 1, (short) 2, (short) 3}; |
| 54 | + byte[] transformedArray = transformArray(array, s -> (byte) (s * 2)); |
| 55 | + |
| 56 | + byte[] expectedArray = {(byte) 2, (byte) 4, (byte) 6}; |
| 57 | + assertArrayEquals(expectedArray, transformedArray); |
| 58 | + |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void whenUsingBiFunction_thenCanUseItToReplaceMapValues() { |
| 63 | + |
| 64 | + Map<String, Integer> salaries = new HashMap<>(); |
| 65 | + salaries.put("John", 40000); |
| 66 | + salaries.put("Freddy", 30000); |
| 67 | + salaries.put("Samuel", 50000); |
| 68 | + |
| 69 | + salaries.replaceAll((name, oldValue) -> name.equals("Freddy") ? oldValue : oldValue + 10000); |
| 70 | + |
| 71 | + assertEquals(new Integer(50000), salaries.get("John")); |
| 72 | + assertEquals(new Integer(30000), salaries.get("Freddy")); |
| 73 | + assertEquals(new Integer(60000), salaries.get("Samuel")); |
| 74 | + |
| 75 | + } |
| 76 | + |
| 77 | + |
| 78 | + @Test |
| 79 | + public void whenPassingLambdaToThreadConstructor_thenLambdaInferredToRunnable() { |
| 80 | + |
| 81 | + Thread thread = new Thread(() -> System.out.println("Hello From Another Thread")); |
| 82 | + thread.start(); |
| 83 | + |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + public void whenUsingSupplierToGenerateNumbers_thenCanUseItInStreamGenerate() { |
| 88 | + |
| 89 | + int[] fibs = {0, 1}; |
| 90 | + Stream<Integer> fibonacci = Stream.generate(() -> { |
| 91 | + int result = fibs[1]; |
| 92 | + int fib3 = fibs[0] + fibs[1]; |
| 93 | + fibs[0] = fibs[1]; |
| 94 | + fibs[1] = fib3; |
| 95 | + return result; |
| 96 | + }); |
| 97 | + |
| 98 | + List<Integer> fibonacci5 = fibonacci.limit(5) |
| 99 | + .collect(Collectors.toList()); |
| 100 | + |
| 101 | + assertEquals(new Integer(1), fibonacci5.get(0)); |
| 102 | + assertEquals(new Integer(1), fibonacci5.get(1)); |
| 103 | + assertEquals(new Integer(2), fibonacci5.get(2)); |
| 104 | + assertEquals(new Integer(3), fibonacci5.get(3)); |
| 105 | + assertEquals(new Integer(5), fibonacci5.get(4)); |
| 106 | + |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + public void whenUsingConsumerInForEach_thenConsumerExecutesForEachListElement() { |
| 111 | + |
| 112 | + List<String> names = Arrays.asList("John", "Freddy", "Samuel"); |
| 113 | + names.forEach(name -> System.out.println("Hello, " + name)); |
| 114 | + |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + public void whenUsingBiConsumerInForEach_thenConsumerExecutesForEachMapElement() { |
| 119 | + |
| 120 | + Map<String, Integer> ages = new HashMap<>(); |
| 121 | + ages.put("John", 25); |
| 122 | + ages.put("Freddy", 24); |
| 123 | + ages.put("Samuel", 30); |
| 124 | + |
| 125 | + ages.forEach((name, age) -> System.out.println(name + " is " + age + " years old")); |
| 126 | + |
| 127 | + } |
| 128 | + |
| 129 | + @Test |
| 130 | + public void whenUsingPredicateInFilter_thenListValuesAreFilteredOut() { |
| 131 | + |
| 132 | + List<String> names = Arrays.asList("Angela", "Aaron", "Bob", "Claire", "David"); |
| 133 | + |
| 134 | + List<String> namesWithA = names.stream() |
| 135 | + .filter(name -> name.startsWith("A")) |
| 136 | + .collect(Collectors.toList()); |
| 137 | + |
| 138 | + assertEquals(2, namesWithA.size()); |
| 139 | + assertTrue(namesWithA.contains("Angela")); |
| 140 | + assertTrue(namesWithA.contains("Aaron")); |
| 141 | + |
| 142 | + } |
| 143 | + |
| 144 | + @Test |
| 145 | + public void whenUsingUnaryOperatorWithReplaceAll_thenAllValuesInTheListAreReplaced() { |
| 146 | + |
| 147 | + List<String> names = Arrays.asList("bob", "josh", "megan"); |
| 148 | + |
| 149 | + names.replaceAll(String::toUpperCase); |
| 150 | + |
| 151 | + assertEquals("BOB", names.get(0)); |
| 152 | + assertEquals("JOSH", names.get(1)); |
| 153 | + assertEquals("MEGAN", names.get(2)); |
| 154 | + |
| 155 | + } |
| 156 | + |
| 157 | + @Test |
| 158 | + public void whenUsingBinaryOperatorWithStreamReduce_thenResultIsSumOfValues() { |
| 159 | + |
| 160 | + List<Integer> values = Arrays.asList(3, 5, 8, 9, 12); |
| 161 | + |
| 162 | + int sum = values.stream() |
| 163 | + .reduce(0, (i1, i2) -> i1 + i2); |
| 164 | + |
| 165 | + assertEquals(37, sum); |
| 166 | + |
| 167 | + } |
| 168 | + |
| 169 | + @Test |
| 170 | + public void whenComposingTwoFunctions_thenFunctionsExecuteSequentially() { |
| 171 | + |
| 172 | + Function<Integer, String> intToString = Object::toString; |
| 173 | + Function<String, String> quote = s -> "'" + s + "'"; |
| 174 | + |
| 175 | + Function<Integer, String> quoteIntToString = quote.compose(intToString); |
| 176 | + |
| 177 | + assertEquals("'5'", quoteIntToString.apply(5)); |
| 178 | + |
| 179 | + } |
| 180 | + |
| 181 | + public double squareLazy(Supplier<Double> lazyValue) { |
| 182 | + return Math.pow(lazyValue.get(), 2); |
| 183 | + } |
| 184 | + |
| 185 | + @Test |
| 186 | + public void whenUsingSupplierToGenerateValue_thenValueIsGeneratedLazily() { |
| 187 | + |
| 188 | + Supplier<Double> lazyValue = () -> { |
| 189 | + Uninterruptibles.sleepUninterruptibly(1000, TimeUnit.MILLISECONDS); |
| 190 | + return 9d; |
| 191 | + }; |
| 192 | + |
| 193 | + double valueSquared = squareLazy(lazyValue); |
| 194 | + |
| 195 | + assertEquals(81d, valueSquared, 0); |
| 196 | + |
| 197 | + } |
| 198 | + |
| 199 | +} |
0 commit comments