|
| 1 | +package com.baeldung.java8; |
| 2 | + |
| 3 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 4 | +import static org.hamcrest.Matchers.contains; |
| 5 | +import static org.junit.Assert.assertEquals; |
| 6 | + |
| 7 | +import java.util.ArrayList; |
| 8 | +import java.util.Arrays; |
| 9 | +import java.util.List; |
| 10 | +import java.util.function.Predicate; |
| 11 | +import java.util.stream.Collectors; |
| 12 | + |
| 13 | +import org.junit.Test; |
| 14 | + |
| 15 | +public class Java8PredicateChainUnitTest { |
| 16 | + |
| 17 | + private List<String> names = Arrays.asList("Adam", "Alexander", "John", "Tom"); |
| 18 | + |
| 19 | + @Test |
| 20 | + public void whenFilterList_thenSuccess() { |
| 21 | + List<String> result = names.stream() |
| 22 | + .filter(name -> name.startsWith("A")) |
| 23 | + .collect(Collectors.toList()); |
| 24 | + |
| 25 | + assertEquals(2, result.size()); |
| 26 | + assertThat(result, contains("Adam", "Alexander")); |
| 27 | + } |
| 28 | + |
| 29 | + @Test |
| 30 | + public void whenFilterListWithMultipleFilters_thenSuccess() { |
| 31 | + List<String> result = names.stream() |
| 32 | + .filter(name -> name.startsWith("A")) |
| 33 | + .filter(name -> name.length() < 5) |
| 34 | + .collect(Collectors.toList()); |
| 35 | + |
| 36 | + assertEquals(1, result.size()); |
| 37 | + assertThat(result, contains("Adam")); |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + public void whenFilterListWithComplexPredicate_thenSuccess() { |
| 42 | + List<String> result = names.stream() |
| 43 | + .filter(name -> name.startsWith("A") && name.length() < 5) |
| 44 | + .collect(Collectors.toList()); |
| 45 | + |
| 46 | + assertEquals(1, result.size()); |
| 47 | + assertThat(result, contains("Adam")); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void whenFilterListWithCombinedPredicatesInline_thenSuccess() { |
| 52 | + List<String> result = names.stream() |
| 53 | + .filter(((Predicate<String>) name -> name.startsWith("A")).and(name -> name.length() < 5)) |
| 54 | + .collect(Collectors.toList()); |
| 55 | + |
| 56 | + assertEquals(1, result.size()); |
| 57 | + assertThat(result, contains("Adam")); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void whenFilterListWithCombinedPredicatesUsingAnd_thenSuccess() { |
| 62 | + Predicate<String> predicate1 = str -> str.startsWith("A"); |
| 63 | + Predicate<String> predicate2 = str -> str.length() < 5; |
| 64 | + |
| 65 | + List<String> result = names.stream() |
| 66 | + .filter(predicate1.and(predicate2)) |
| 67 | + .collect(Collectors.toList()); |
| 68 | + |
| 69 | + assertEquals(1, result.size()); |
| 70 | + assertThat(result, contains("Adam")); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + public void whenFilterListWithCombinedPredicatesUsingOr_thenSuccess() { |
| 75 | + Predicate<String> predicate1 = str -> str.startsWith("J"); |
| 76 | + Predicate<String> predicate2 = str -> str.length() < 4; |
| 77 | + |
| 78 | + List<String> result = names.stream() |
| 79 | + .filter(predicate1.or(predicate2)) |
| 80 | + .collect(Collectors.toList()); |
| 81 | + |
| 82 | + assertEquals(2, result.size()); |
| 83 | + assertThat(result, contains("John", "Tom")); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + public void whenFilterListWithCombinedPredicatesUsingOrAndNegate_thenSuccess() { |
| 88 | + Predicate<String> predicate1 = str -> str.startsWith("J"); |
| 89 | + Predicate<String> predicate2 = str -> str.length() < 4; |
| 90 | + |
| 91 | + List<String> result = names.stream() |
| 92 | + .filter(predicate1.or(predicate2.negate())) |
| 93 | + .collect(Collectors.toList()); |
| 94 | + |
| 95 | + assertEquals(3, result.size()); |
| 96 | + assertThat(result, contains("Adam", "Alexander", "John")); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + public void whenFilterListWithCollectionOfPredicatesUsingAnd_thenSuccess() { |
| 101 | + List<Predicate<String>> allPredicates = new ArrayList<Predicate<String>>(); |
| 102 | + allPredicates.add(str -> str.startsWith("A")); |
| 103 | + allPredicates.add(str -> str.contains("d")); |
| 104 | + allPredicates.add(str -> str.length() > 4); |
| 105 | + |
| 106 | + List<String> result = names.stream() |
| 107 | + .filter(allPredicates.stream() |
| 108 | + .reduce(x -> true, Predicate::and)) |
| 109 | + .collect(Collectors.toList()); |
| 110 | + |
| 111 | + assertEquals(1, result.size()); |
| 112 | + assertThat(result, contains("Alexander")); |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + public void whenFilterListWithCollectionOfPredicatesUsingOr_thenSuccess() { |
| 117 | + List<Predicate<String>> allPredicates = new ArrayList<Predicate<String>>(); |
| 118 | + allPredicates.add(str -> str.startsWith("A")); |
| 119 | + allPredicates.add(str -> str.contains("d")); |
| 120 | + allPredicates.add(str -> str.length() > 4); |
| 121 | + |
| 122 | + List<String> result = names.stream() |
| 123 | + .filter(allPredicates.stream() |
| 124 | + .reduce(x -> false, Predicate::or)) |
| 125 | + .collect(Collectors.toList()); |
| 126 | + |
| 127 | + assertEquals(2, result.size()); |
| 128 | + assertThat(result, contains("Adam", "Alexander")); |
| 129 | + } |
| 130 | + |
| 131 | +} |
0 commit comments