| 1 |
import java.util.ArrayList; |
| 2 |
import java.util.List; |
| 3 |
|
| 4 |
|
| 5 |
public interface Predicate<T> { |
| 6 |
boolean test(T t); |
| 7 |
} |
| 8 |
|
| 9 |
interface Function<T, R> { |
| 10 |
R apply(T t); |
| 11 |
} |
| 12 |
|
| 13 |
interface Consumer<T> { |
| 14 |
void accept(T t); |
| 15 |
} |
| 16 |
|
| 17 |
interface Supplier<T> { |
| 18 |
T get(); |
| 19 |
} |
| 20 |
|
| 21 |
interface BiFunction<T, U, R> { |
| 22 |
R apply(T t, U u); |
| 23 |
} |
| 24 |
|
| 25 |
interface TriFunction<T, U, V, R> { |
| 26 |
R apply(T t, U u, V v); |
| 27 |
} |
| 28 |
|
| 29 |
public class SuperClass { |
| 30 |
|
| 31 |
public SuperClass() { |
| 32 |
|
| 33 |
} |
| 34 |
|
| 35 |
public SuperClass(String s) { |
| 36 |
|
| 37 |
} |
| 38 |
|
| 39 |
void print(Integer val) { |
| 40 |
System.out.println(val); |
| 41 |
} |
| 42 |
|
| 43 |
static void print(Boolean val) { |
| 44 |
System.out.println(val); |
| 45 |
} |
| 46 |
|
| 47 |
boolean isEqual(Integer i, Integer j) { |
| 48 |
return i.equals(j); |
| 49 |
} |
| 50 |
|
| 51 |
boolean isEqualAsStrings(Integer i, String j) { |
| 52 |
return i.toString().equals(j); |
| 53 |
} |
| 54 |
|
| 55 |
int add(Integer i, Integer j) { |
| 56 |
return i + j; |
| 57 |
} |
| 58 |
|
| 59 |
String getOneNumberAsString(Integer i, Integer j, Integer k) { |
| 60 |
return i.toString(); |
| 61 |
} |
| 62 |
|
| 63 |
int returnSameValue(Integer i) { |
| 64 |
return i; |
| 65 |
} |
| 66 |
|
| 67 |
String returnSameValue(String s) { |
| 68 |
return s; |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
public class MethodReferences extends SuperClass { |
| 73 |
|
| 74 |
List<Integer> list = new ArrayList<>(); |
| 75 |
|
| 76 |
void print(String s) { |
| 77 |
System.out.println(s); |
| 78 |
} |
| 79 |
|
| 80 |
boolean isAnyEqualToString5(List<Integer> list, BiFunction<Integer, String, Boolean> biFunction) { |
| 81 |
for (Integer integer : list) { |
| 82 |
if (biFunction.apply(integer, "5")) { |
| 83 |
return true; |
| 84 |
} |
| 85 |
} |
| 86 |
return false; |
| 87 |
} |
| 88 |
|
| 89 |
Function<Object, Integer> classMethod() { |
| 90 |
return Object::hashCode; |
| 91 |
} |
| 92 |
|
| 93 |
Function<SuperClass, Integer> superclassMethodNotOverridden() { |
| 94 |
return SuperClass::hashCode; |
| 95 |
} |
| 96 |
|
| 97 |
Function<String, Integer> superclassMethodOverridden() { |
| 98 |
return String::hashCode; |
| 99 |
} |
| 100 |
|
| 101 |
Function<Object, Integer> superclassMethodWithSubclassType() { |
| 102 |
return Object::hashCode; |
| 103 |
} |
| 104 |
|
| 105 |
Consumer<String> fieldAccessMethod() { |
| 106 |
return System.out::println; |
| 107 |
} |
| 108 |
|
| 109 |
Consumer<String> thisClassMethod() { |
| 110 |
return this::print; |
| 111 |
} |
| 112 |
|
| 113 |
Consumer<Integer> superclassMethod() { |
| 114 |
return super::print; |
| 115 |
} |
| 116 |
|
| 117 |
Function<Integer, Boolean> instanceMethod() { |
| 118 |
return list::add; |
| 119 |
} |
| 120 |
|
| 121 |
Supplier<SuperClass> zeroArgumentConstructor() { |
| 122 |
return SuperClass::new; |
| 123 |
} |
| 124 |
|
| 125 |
Function<String, SuperClass> singleArgumentConstructor() { |
| 126 |
return SuperClass::new; |
| 127 |
} |
| 128 |
|
| 129 |
Consumer<Boolean> staticMethod() { |
| 130 |
return SuperClass::print; |
| 131 |
} |
| 132 |
|
| 133 |
BiFunction<Integer, String, Boolean> biFunction() { |
| 134 |
return super::isEqualAsStrings; |
| 135 |
} |
| 136 |
|
| 137 |
TriFunction<Integer, Integer, Integer, String> customTriFunction() { |
| 138 |
return super::getOneNumberAsString; |
| 139 |
} |
| 140 |
|
| 141 |
void consumerDeclaredInMethod() { |
| 142 |
Consumer<String> stringConsumer = this::print; |
| 143 |
} |
| 144 |
|
| 145 |
void functionDeclaredInMethod() { |
| 146 |
Function<String, String> stringFunction = super::returnSameValue; |
| 147 |
} |
| 148 |
|
| 149 |
void biFunctionDeclaredInMethod() { |
| 150 |
BiFunction<Integer, Integer, Boolean> biFunction = super::isEqual; |
| 151 |
} |
| 152 |
|
| 153 |
void consumerUsedInStream() { |
| 154 |
list.stream().forEach(super::print); |
| 155 |
} |
| 156 |
|
| 157 |
void functionUsedInStream() { |
| 158 |
list.stream().map(super::returnSameValue); |
| 159 |
} |
| 160 |
|
| 161 |
void biFunctionUsedInStream() { |
| 162 |
list.stream().reduce(0, super::add); |
| 163 |
} |
| 164 |
|
| 165 |
void biFunctionInMethodCall() { |
| 166 |
isAnyEqualToString5(list, super::isEqualAsStrings); |
| 167 |
} |
| 168 |
} |