Skip to content

Commit 54ffcb2

Browse files
author
Sergey Lukichev
committed
lambda expressions demo
1 parent d4b7cb8 commit 54ffcb2

5 files changed

Lines changed: 178 additions & 0 deletions

File tree

Drafts/LambdaDemo/.gitignore

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/target/
2+
!.mvn/wrapper/maven-wrapper.jar
3+
4+
### STS ###
5+
.apt_generated
6+
.classpath
7+
.factorypath
8+
.project
9+
.settings
10+
.springBeans
11+
.sts4-cache
12+
13+
### IntelliJ IDEA ###
14+
.idea
15+
*.iws
16+
*.iml
17+
*.ipr
18+
19+
### NetBeans ###
20+
/nbproject/private/
21+
/build/
22+
/nbbuild/
23+
/dist/
24+
/nbdist/
25+
/.nb-gradle/

Drafts/LambdaDemo/src/Demo.java

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
public class DemoImpl implements DemoIntterface {
2+
3+
int anInt;
4+
5+
@Override
6+
public String getString(int i) {
7+
return String.valueOf(i);
8+
}
9+
10+
@Override
11+
public String trasform(int i) {
12+
anInt = 3;
13+
return "Hello World " + i + anInt;
14+
}
15+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
@FunctionalInterface
2+
public interface DemoIntterface {
3+
String SOME_STRING = "Hello";
4+
5+
String getString(int i);
6+
//String transform(int i);
7+
8+
default String trasform(int i) {
9+
return "Hello World";
10+
}
11+
}

Drafts/LambdaDemo/src/Person.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
public class Person {
2+
private String name;
3+
4+
public Person() {
5+
}
6+
7+
public Person(String name) {
8+
this.name = name;
9+
}
10+
11+
public String getName() {
12+
return name;
13+
}
14+
15+
public static String type(String s) {
16+
return "HUMAN";
17+
}
18+
19+
@Override
20+
public String toString() {
21+
return "Person{" +
22+
"name='" + name + '\'' +
23+
'}';
24+
}
25+
}

0 commit comments

Comments
 (0)