我是靠谱客的博主 不安花生,这篇文章主要介绍JAVA lambda使用总结(循环、过滤、排序、分组,内存分页),现在分享给大家,希望可以做个参考。

复制代码
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
package com.company; import org.omg.Messaging.SyncScopeHelper; import java.io.UnsupportedEncodingException; import java.util.*; import java.util.stream.Collectors; public class Student { public static void main(String[] args) { // lambda 使用学习总结 ArrayList<Student> studentList = new ArrayList<>(); Random ageRandom = new Random(25); Random sexRandom = new Random(25); for (int i = 0; i < 100; i++) { Student student = new Student(); student.setAge(ageRandom.nextInt(25)); student.setBirthday(new Date(new Random().nextInt())); student.setSex(sexRandom.nextInt(2)); student.setName(getRandomJianHan(3)); System.out.println(student); studentList.add(student); } //foreach循环 studentList.forEach(c-> { c.setAge(c.getAge()+1); System.out.println(c.toString()); }); //filter过滤 List<Student> filterList = studentList.stream().filter(c -> c.age > 20).collect(Collectors.toList()); System.out.println(filterList); //sorted排序 List<Student> sortedList = studentList.stream().sorted(Comparator.comparing(Student::getAge)).collect(Collectors.toList()); System.out.println(sortedList); //list分组 Map<String, List<Student>> collect = studentList.stream().collect(Collectors.groupingBy(Student::getName)); //排序+内存分页 List<Student> collect1 = studentList.stream() .sorted(Comparator.comparing(o -> new Double(o.getAge()))) .skip(0 * 10) .limit(10).collect(Collectors.toList()); System.out.println("ss"+collect1); System.out.println("ss"+collect1.size()); } public static String getRandomJianHan(int len) { String ret = ""; for (int i = 0; i < len; i++) { String str = null; int hightPos, lowPos; // 定义高低位 Random random = new Random(); hightPos = (176 + Math.abs(random.nextInt(39))); // 获取高位值 lowPos = (161 + Math.abs(random.nextInt(93))); // 获取低位值 byte[] b = new byte[2]; b[0] = (new Integer(hightPos).byteValue()); b[1] = (new Integer(lowPos).byteValue()); try { str = new String(b, "GBK"); // 转成中文 } catch (UnsupportedEncodingException ex) { ex.printStackTrace(); } ret += str; } return ret; } private String name; private Integer age; private Integer sex; private Date birthday; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Integer getSex() { return sex; } public void setSex(Integer sex) { this.sex = sex; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } @Override public String toString() { return "Student{" + "name='" + name + ''' + ", age=" + age + ", sex=" + sex + ", birthday=" + birthday + '}'; } }

最后

以上就是不安花生最近收集整理的关于JAVA lambda使用总结(循环、过滤、排序、分组,内存分页)的全部内容,更多相关JAVA内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(59)

评论列表共有 0 条评论

立即
投稿
返回
顶部