jdk8新特性




Labmda 使用

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
/**
* @author Blue
* @date 2020/3/24
**/
public class LabmdaDemo1 {
public void pringSomething(String something, Printer printer) {
printer.printer(something);
}
interface Printer {
void printer(String val);
}

public static void main(String[] args) {
LabmdaDemo1 lambdaDemo1 = new LabmdaDemo1();
String some = "Hello World";

Printer printer = new Printer() {
@Override
public void printer(String val) {
System.out.println(val);
}
};
lambdaDemo1.pringSomething(some, printer);

System.out.println("-----------使用Lambda表达式----------");
Printer printer1 = (String val) -> {
System.out.println(val + 1);
};
System.out.println("----------简化ing:很像es6箭头函数----------------");
Printer printer2 = (val) -> {
System.out.println(val + 2);
};
Printer printer3 = val -> {
System.out.println(val + 3);
};
Printer printer4 = val -> System.out.println(val + 4);

}
}

使用lambda表达式对集合倒序排序

1
2
3
4
5
6
7
8
9
10
11
List<UserInfo> userInfo = userInfoService.findList();
// 根据超时天数升序排序
Comparator<UserInfo> comparator =
(a, b) -> a.getAge().compareTo(b.getAge());
// 倒叙排序
userInfo.sort(comparator.reversed());

// compareTo:
// 如果参数字符串等于此字符串,则返回值 0;
// 如果此字符串小于字符串参数,则返回一个小于 0 的值;
// 如果此字符串大于字符串参数,则返回一个大于 0 的值。

Stream 流

创建 Employee 的实体类,这里也可以使用 Lombok 插件的@Data 快速构造

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
package bean;

import java.util.List;
import java.util.function.Predicate;

/**
* @author Blue
* @date 2020/3/24
**/
public class Employee {
private Integer id;
private Integer age;
private String gender;
List<Employee> children;

/**
* filter的谓词逻辑:
* 年龄大于50
*/
public static Predicate<Employee> ageGreaterThan50 = x -> x.getAge() > 50;
public static Predicate<Employee> genderM = x -> x.getGender().equals("M");


@Override
public String toString() {
return "Employee{" +
"id=" + id +
", age=" + age +
", gender='" + gender + '\'' +
", children=" + children +
'}';
}

public Employee(Integer id, Integer age, String gender, List<Employee> children) {
this.id = id;
this.age = age;
this.gender = gender;
this.children = children;
}

public List<Employee> getChildren() {
return children;
}

public void setChildren(List<Employee> children) {
this.children = children;
}

public void setId(Integer id) {
this.id = id;
}

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

public void setGender(String gender) {
this.gender = gender;
}

public Integer getId() {
return id;
}

public Integer getAge() {
return age;
}

public String getGender() {
return gender;
}
}

初始 filter 用法

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
import bean.Employee;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author Blue
* @date 2020/3/24
* filter用法
**/
public class StreamApiDemo2 {
public static void main(String[] args) {
Employee e1 = new Employee(3, 43, "M", null);
Employee e2 = new Employee(2, 23, "F", null);
Employee e3 = new Employee(4, 53, "M", null);
Employee ch4 = new Employee(8, 33, "M", null);
ArrayList li4 = new ArrayList();
li4.add(ch4);
Employee e4 = new Employee(5, 63, "M", li4);

List<Employee> list = Arrays.asList(e1, e2, e3, e4);
List<Employee> collect = list.stream().
filter(e -> e.getAge() > 50 && e.getId() > 3)
.collect(Collectors.toList());
System.out.println(collect);
list.forEach(System.out::println);
System.out.println("----------negate()取相反的------------");
List<Employee> collect1 = list.stream().
filter(Employee.ageGreaterThan50.and(Employee.genderM).negate())
.collect(Collectors.toList());
System.out.println(collect1);
}
}

Map 和 flatMap

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
import bean.Employee;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* @author Blue
* @date 2020/3/24
* Map方法: 将年龄都增加一岁,性别换成male,female
* flatMap
**/
public class StreamApiDemo3 {
public static void main(String[] args) {
Employee e1 = new Employee(3, 43, "M", null);
Employee e2 = new Employee(2, 23, "F", null);
Employee e3 = new Employee(4, 53, "M", null);
Employee ch4 = new Employee(8, 33, "M", null);
ArrayList li4 = new ArrayList();
li4.add(ch4);
Employee e4 = new Employee(5, 63, "M", li4);
List<Employee> list = Arrays.asList(e1, e2, e3, e4);

List<Employee> maped = list.stream()
.map(e -> {
e.setAge(e.getAge() + 1);
e.setGender(e.getGender().equals("M") ? "male" : "female");
return e;
}).collect(Collectors.toList());
List<Employee> maped1 = list.stream()
//特殊的map,引用类型,进去出去都是同类型
.peek(e -> {
e.setAge(e.getAge() + 1);
e.setGender(e.getGender().equals("M") ? "male" : "female");
}).collect(Collectors.toList());
System.out.println(maped);
System.out.println("--------------------");
System.out.println(maped1);
System.out.println("----------------------------");
List<String> words = Arrays.asList("Hello", "World");
// 将words集合中的元素一个个截取打印出来
words.stream()
.flatMap(w -> Arrays.stream(w.split("")))
.forEach(System.out::println);
}
}

数组操作

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
StringBuilder sb = new StringBuilder();
SecureRandom sr = null;
try {
// 获取高强度安全随机数生成器
sr = SecureRandom.getInstanceStrong();
} catch (NoSuchAlgorithmException e) {
// 获取普通的安全随机数生成器
sr = new SecureRandom();
}
byte[] buffer = new byte[4];
// 用安全随机数填充buffer
sr.nextBytes(buffer);
for (int i = 0; i < buffer.length; i++) {
sb.append(buffer[i]);
}
// Hello, 569695-56
sb.insert(0, "Hello, ");

// 方式一:遍历数组,与方式二不能同时存在,否则下面那个会[B@497470ed[-44, -17, 75, -3]
Arrays.asList(buffer).forEach(System.out::print);

// 方式二:遍历数组(推荐),结果类似 [56, 96, 95, -56]
Collections.singletonList(buffer)
.forEach(item -> System.out.println(Arrays.toString(item)));

System.out.println(Arrays.toString(buffer));
打赏
  • 版权声明: 本博客所有文章除特别声明外,均采用 Apache License 2.0 许可协议。转载请注明出处!
  1. © 2020 Liu Yang    湘ICP备20003709号

请我喝杯咖啡吧~

支付宝
微信