java 8 Lambda 表达式用例
import lombok.AllArgsConstructor;
import lombok.Data;
import org.junit.Test;
import java.util.*;
import java.util.function.Consumer;
import java.util.function.Predicate;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toSet;
/**
* @description: Lambda expressions example
* @author zivs.zheng
* @date 2018/3/23 11:27
*/
public class LambdaExample {
// java 开发人员集合
List<Person> javaPersons = new ArrayList<Person>() {{
add(new Person("Tom", "java development", "M", 2000, 28));
add(new Person("Bob", "java development", "M", 2500, 22));
add(new Person("Fei", "java development", "F", 1900, 21));
add(new Person("Jac", "java development", "M", 2300, 25));
add(new Person("Hua", "java development", "F", 1800, 22));
add(new Person("Wei", "java development", "F", 2000, 23));
add(new Person("Tao", "java development", "M", 3000, 27));
}};
// go 开发人员集合
List<Person> goPersons = new ArrayList<Person>() {{
add(new Person("Men", "go development", "F", 2000, 24));
add(new Person("Mao", "go development", "M", 2500, 23));
add(new Person("Qin", "go development", "F", 1900, 22));
add(new Person("Don", "go development", "M", 2300, 25));
add(new Person("Han", "go development", "M", 2400, 28));
}};
@Test
public void iterateJavaPersons() {
// Lambda表达式迭代java开发人员集合
javaPersons.forEach((p) -> System.out.println(p.getName() + "\t" + p.getSalary()));
System.out.println("增加销售额10%:");
Consumer<Person> giveRaise = e -> e.setSalary(e.getSalary() / 100 * 10 + e.getSalary());
javaPersons.forEach(giveRaise);
javaPersons.forEach((p) -> System.out.println(p.getName() + "\t" + p.getSalary()));
}
@Test
public void sortJavaPersons() {
Collections.sort(javaPersons, new Comparator<Person>() {
@Override
public int compare(Person p1, Person p2) {
if (p1.getAge() > p2.getAge()) {
return 1;
}
return -1;
}
});
javaPersons.forEach((p) -> System.out.println(p.getName() + "\t" + p.getAge()));
System.out.println();
// 匿名内部内
javaPersons.sort((Person p1, Person p2) -> {
if (p1.getAge() > p2.getAge()) {
return 1;
}
return -1;
});
// Lambda表达式迭代java开发人员集合
javaPersons.forEach((p) -> System.out.println(p.getName() + "\t" + p.getAge()));
}
@Test
public void filterJavaPersons() {
// 定义 filters
Predicate<Person> ageFilter = (p) -> (p.getAge() > 25);
Predicate<Person> salaryFilter = (p) -> (p.getSalary() > 2000);
Predicate<Person> genderFilter = (p) -> ("M".equals(p.getGender()));
System.out.println("过滤年龄大于25销售额大于2000性别为M的:");
javaPersons.stream()
.filter(ageFilter)
.filter(salaryFilter)
.filter(genderFilter)
.forEach((p) -> System.out.println(p));
System.out.println();
System.out.println("过滤年龄大于25销售额大于2000的:");
goPersons.stream()
.filter(ageFilter)
.filter(salaryFilter)
.forEach((p) -> System.out.println(p));
}
@Test
public void limitPersons() {
// 根据name排序的前五个
List<Person> result = javaPersons
.stream()
.sorted((p1, p2) -> (p1.getName().compareTo(p2.getName())))
.limit(5)
.collect(toList());
result.forEach((p) -> System.out.println(p));
}
@Test
public void minMaxPersons() {
System.out.println("销售额最小:");
Person minPerson = javaPersons
.stream()
.min((p1, p2) -> (p1.getSalary() - p2.getSalary()))
.get();
System.out.println(minPerson);
System.out.println();
System.out.println("销售额最大:");
Person maxPerson = javaPersons
.stream()
.max((p1, p2) -> (p1.getSalary() - p2.getSalary()))
.get();
System.out.println(maxPerson);
}
@Test
public void sumSalaryPersons() {
// Salary 之和
int totalSalary = javaPersons
.parallelStream()
.mapToInt(p -> p.getSalary())
.sum();
System.out.println(totalSalary);
}
@Test
public void setPersons() {
// 获取姓名的集合
Set<String> mapPersons = javaPersons
.stream()
.map(Person::getName)
.collect(toSet());
mapPersons.forEach(m -> System.out.println(m.toString()));
System.out.println();
// 以“,”拼接姓名
String personsStr = javaPersons
.stream()
.map(Person::getName)
.collect(joining(" , "));
System.out.println(personsStr);
}
@Test
public void statsPersons() {
IntSummaryStatistics stats = javaPersons
.stream()
.mapToInt((p) -> p.getAge())
.summaryStatistics();
System.out.println("Max age : " + stats.getMax());
System.out.println("Min age : " + stats.getMin());
System.out.println("Sum age : " + stats.getSum());
System.out.println("Avg age : " + stats.getAverage());
}
@Test
public void variables() {
int n = 0;
final int k = n;
Runnable r = () -> {
int i = k;
System.out.println("i = " + i);
};
n++;
r.run();
}
}
@Data
@AllArgsConstructor
class Person {
private String name, job, gender;
private int salary, age;
}
网友评论