|
| 1 | +import java.util.Arrays; |
| 2 | +import java.util.Comparator; |
| 3 | +import java.util.function.Consumer; |
| 4 | +import java.util.function.Function; |
| 5 | +import java.util.function.Predicate; |
| 6 | +import java.util.function.Supplier; |
| 7 | + |
| 8 | +public class Demo { |
| 9 | + public static void main(String[] args) { |
| 10 | + Comparator<String> c = new Comparator<String>() { |
| 11 | + @Override |
| 12 | + public int compare(String o1, String o2) { |
| 13 | + return 0; |
| 14 | + } |
| 15 | + }; |
| 16 | + |
| 17 | + DemoIntterface demo = new DemoImpl(); |
| 18 | + |
| 19 | + System.out.println(demo.trasform(5)); |
| 20 | + |
| 21 | + |
| 22 | + String someString = DemoIntterface.SOME_STRING; |
| 23 | + |
| 24 | + DemoIntterface dia = new DemoIntterface() { |
| 25 | + @Override |
| 26 | + public String getString(int i) { |
| 27 | + return String.valueOf(i); |
| 28 | + } |
| 29 | + }; |
| 30 | + |
| 31 | + DemoIntterface di = (a) -> String.valueOf(a); |
| 32 | + |
| 33 | + //"abc" -> 3 |
| 34 | + Function<String, Integer> f1 = (s) -> s.length(); |
| 35 | + |
| 36 | + // 5 -> 25 |
| 37 | + |
| 38 | + Function<Integer, Integer> f2 = (a) -> a * a; |
| 39 | + |
| 40 | + Predicate<String> f3 = (s) -> s.length() % 2 == 0; |
| 41 | + |
| 42 | + Consumer<String> f4 = (s) -> System.out.println(s); |
| 43 | + |
| 44 | + Supplier<Integer> f5 = () -> (int)(Math.random()); |
| 45 | + |
| 46 | + |
| 47 | +// { |
| 48 | +// if(s.length() % 2 == 0) return true; |
| 49 | +// else return false; |
| 50 | +// }; |
| 51 | + |
| 52 | + Function<Integer, Integer> ff1 = (a) -> a*a; |
| 53 | + doSomeMath(ff1, 10); |
| 54 | + |
| 55 | + Function<Integer, Integer> ff2 = (a) -> a*10; |
| 56 | + doSomeMath(ff2, 24); |
| 57 | + |
| 58 | + Function<Integer, Integer> ff3 = Demo::getInt; |
| 59 | + doSomeMath(ff3, 56); |
| 60 | + |
| 61 | + Function<String, String> getString = Demo::getString; |
| 62 | + |
| 63 | + Function<String, String> getStringL = (s) -> s; |
| 64 | + |
| 65 | + transformString((s) -> s, "abc"); |
| 66 | + transformString(Demo::getString, "abc"); |
| 67 | + |
| 68 | + |
| 69 | + Function<String, String> type = Person::type; |
| 70 | + |
| 71 | + Person p = new Person("Ivan"); |
| 72 | + |
| 73 | + Supplier<String> getName = p::getName; |
| 74 | + |
| 75 | + System.out.println(getName.get()); |
| 76 | + |
| 77 | + Function<Person, String> getName1 = Person::getName; |
| 78 | + |
| 79 | + System.out.println(getName1.apply(p)); |
| 80 | + |
| 81 | + Supplier<Person> person = Person::new; |
| 82 | + |
| 83 | + } |
| 84 | + |
| 85 | + public static int getInt(int a) { |
| 86 | + return a%2==0 ? 1: -1; |
| 87 | + } |
| 88 | + |
| 89 | + public static String transformString(Function<String, String> f, String s) { |
| 90 | + return f.apply(s); |
| 91 | + |
| 92 | + } |
| 93 | + |
| 94 | + public static int doSomeMath(Function<Integer, Integer> f, int a) { |
| 95 | + System.out.println(a); |
| 96 | + return f.apply(a); |
| 97 | + } |
| 98 | + |
| 99 | + public static String getString(String input) { |
| 100 | + return input; |
| 101 | + } |
| 102 | +} |
0 commit comments