常用的Lambda表达式

Java Lambda 表达式
JAVA开发中几个常用的lambda表达式!记得收藏起来哦~

src/test/java/test/lambda_1.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
  
// https://blog.csdn.net/lhmyy521125/article/details/135334709

public class lambda_1 {
public static void main(String[] args) {
System.out.println("\n\n------------## 1、集合遍历---------------");
List<String> list = Arrays.asList("apple", "banana", "orange", "pineapple");
for (String s : list) {
System.out.println(s);
}
System.out.println();
list.forEach(s -> System.out.println(s));
System.out.println();
list.forEach(System.out::println);



System.out.println("\n\n------------## 2、排序---------------");
list = Arrays.asList("apple", "banana", "orange", "pineapple");
Collections.sort(list, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return o2.compareTo(o1);
}
});
list.forEach(System.out::println);

list = Arrays.asList("apple", "banana", "orange", "pineapple");
System.out.println();
Collections.sort(list, (o1, o2) -> o2.compareTo(o1));
list.forEach(System.out::println);


System.out.println("\n\n--------------## 3、映射-------------");

System.out.println("\n对集合中的每个元素进行映射");
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
List<Integer> squares = numbers.stream()
.map(s -> s * s)
.collect(Collectors.toList());
squares.forEach(System.out::println);

System.out.println("\n对对象集合进行映射");
List<String> names = Arrays.asList("Alice", "Bob", "Charlie");
List<Integer> lengths = names.stream().map(String::length).collect(Collectors.toList());
lengths.forEach(System.out::println);

System.out.println("\n对集合中的对象属性进行映射");
List<Person> people = Arrays.asList(
new Person("Alice", 25),
new Person("Bob", 30),
new Person("Charlie", 22)
);
List<Integer> ages = people.stream().map(Person::getAge).collect(Collectors.toList());
ages.forEach(System.out::println);


System.out.println("\n\n------------## 4、过滤---------------");
list = Arrays.asList("apple", "banana", "orange");
List<String> filteredList = list.stream().filter(s -> s.startsWith("b")).collect(Collectors.toList());
filteredList.forEach(System.out::println);



System.out.println("\n\n-------------## 5、分组--------------");
System.out.println("\n按元素类型进行分组");
List<String> words = Arrays.asList("apple", "banana", "orange", "avocado", "blueberry", "blackberry");
Map<Integer, List<String>> groupByLength = words.stream().collect(Collectors.groupingBy(String::length));
groupByLength.forEach((k, v) -> {
System.out.println(k);
v.forEach(System.out::println);
});

System.out.println("\n按对象属性进行分组\n");
people = Arrays.asList(
new Person("Alice", "Engineer"),
new Person("Bob", "Doctor"),
new Person("Charlie", "Engineer"),
new Person("David", "Doctor")
);

Map<String, List<Person>> groupedByOccupation =
people.stream().collect(Collectors.groupingBy(Person::getOccupation));
groupedByOccupation.forEach((k, v) -> {
System.out.println(k);
v.forEach(System.out::println);
});




System.out.println("\n\n-------------## 6、归约--------------");
System.out.println("\n对数字列表进行求和");
numbers = Arrays.asList(1, 2, 3, 4, 5);
int sum = numbers.stream().reduce(0, (a, b) -> a + b);
// int sum = numbers.stream().reduce(0, Integer::sum);
System.out.println(sum);

System.out.println("\n对字符串列表进行连接");
words = Arrays.asList("Java", "Lambda", "Expression");
String result = words.stream().reduce("", (a, b) -> a + " " + b);
System.out.println(result);


System.out.println("\n找到列表中的最大值");
numbers = Arrays.asList(3, 7, 1, 9, 4);
// int max = numbers.stream().reduce(Integer.MIN_VALUE, Math::max);
int max = numbers.stream().reduce(Integer.MIN_VALUE, Integer::max);
System.out.println(max);

System.out.println("\n\n-------------## 7、函数式接口--------------");
System.out.println("\n日常未使用Lambda表达式表现如下");
MyInterface myObject = new MyInterface() {

@Override
public void doSomething(String input) {
System.out.println(input);
}
};
myObject.doSomething("Hello word");

System.out.println("\n使用Lambda表达式表现如下");
myObject = null;
myObject = input -> System.out.println(input);
myObject.doSomething("Hello word!!");



System.out.println("\n\n------------## 8、线程创建---------------");
System.out.println("\n日常未使用Lambda表达式表现如下");
Thread thread = new Thread(new Runnable() {

@Override
public void run() {
System.out.println("start runing");
}
});
thread.start();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}

System.out.println("\n使用Lambda表达式表现如下");
thread = null;
thread = new Thread(() -> System.out.println("start runing!!"));
thread.start();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}


System.out.println("\n\n-------------## 9、Stream流水线操作--------------");
list = Arrays.asList("apple", "banana", "orange");
filteredList = list.stream().filter(fruit -> fruit.startsWith("a")).map(String::toUpperCase).collect(Collectors.toList());
filteredList.forEach(System.out::println);

System.out.println("\n\n------------## 10、Optional的操作---------------");
String str = "Hello World";
Optional.ofNullable(str).map(String::toUpperCase).ifPresent(System.out::println);

}
}

src/test/java/test/MyInterface.java

1
2
3
4
public interface MyInterface {  
public void doSomething(String input);

}

src/test/java/test/Person.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
public class Person {  
private String name;
private int age;
private String Occupation;

public Person() {}

@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", Occupation='" + Occupation + '\'' +
'}';
}

public Person(String name, String occupation) {
this.name = name;
this.Occupation = occupation;
}

public String getOccupation() {
return Occupation;
}

public void setOccupation(String occupation) {
Occupation = occupation;
}

public Person(String name, int age) {
this.name = name;
this.age = age;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}
}

常用的Lambda表达式
http://yuanql.top/2024/07/21/00_项目经历/YuanqlProject/学习测试专用项目/常用的Lambda表达式/
作者
Qingli Yuan
发布于
2024年7月21日
许可协议