Java8 新增了很多特性,本文主要介绍使用:
更多的新特性可以参阅官网:What’s New in JDK 8
创建一个测试类
/**
* @author 码农猿
*/
public class Student {
/**
* id
*/
private int id;
/**
* 姓名
*/
private String name;
/**
* 年龄
*/
private int age;
/**
* 手机
*/
private String phone;
//省略 get /set /toString
}
Lambda 表达式的基础语法:Java8中引入了一个新的操作符 “->” 该操作符称为箭头操作符或 Lambda 操作符
箭头操作符将 Lambda 表达式拆分成两部分:
左侧:Lambda 表达式的参数列表
右侧:Lambda 表达式中所需执行的功能, 即 Lambda 体
语法介绍
序号 | 描述 | 示例 |
---|---|---|
1 | 无参数,无返回值 | () -> System.out.println(“Hello Lambda!”) |
2 | 有一个参数,并且无返回值 | (x) -> System.out.println(x) |
3 | 若只有一个参数,小括号可以省略不写 | x -> System.out.println(x) |
4 | 有两个以上的参数,有返回值,并且 Lambda 体中有多条语句 | Comparator com = (x, y) -> {System.out.println(“函数式接口”);return Integer.compare(x, y);}; |
5 | 若 Lambda 体中只有一条语句, return 和 大括号都可以省略不写 | Comparator com = (x, y) -> Integer.compare(x, y); |
6 | Lambda 表达式的参数列表的数据类型可以省略不写,因为JVM编译器通过上下文推断出,数据类型,即“类型推断” | (Integer x, Integer y) -> Integer.compare(x, y); |
1.1.1 List遍历
@Test
public void test1() {
List<Student> studentList = new ArrayList<Student>();
studentList.add(new Student(1, "张三", 11, "13333345671"));
studentList.add(new Student(2, "李四", 10, "13333345672"));
studentList.add(new Student(3, "王五", 12, "13333345673"));
studentList.add(new Student(4, "赵六", 11, "13333345674"));
studentList.add(new Student(5, "大黑", 13, "13333345675"));
studentList.add(new Student(6, "大白", 10, "13333345676"));
// for Each 遍历
for (Student stu : studentList) {
System.out.println(stu);
}
//java 8 Lambda 遍历,stu 代表List里的Student对象
studentList.forEach(stu -> {
System.out.println(stu);
});
}
1.1.2 Map遍历
@Test
public void test2() {
Map<String, Integer> items = new HashMap<String, Integer>();
items.put("A", 10);
items.put("B", 20);
items.put("C", 30);
items.put("D", 40);
items.put("E", 50);
items.put("F", 60);
//遍历 map
for (Map.Entry<String, Integer> entry : items.entrySet()) {
System.out.println("map-key : " + entry.getKey() + " map-value : " + entry.getValue());
}
//java 8 lambda 表达式遍历 ,k,与 v 分别代表map集合的 key与value
items.forEach((k, v) -> System.out.println("map-key : " + k + " map-value : " + v));
}
2.1.1 filter()过滤与collect()
示例:过滤出年龄< 13 的 学生
private static List<Student> studentList = new ArrayList<Student>();
static {
studentList.add(new Student(1, "张三", 11, "13333345671"));
studentList.add(new Student(2, "李四", 10, "13333345672"));
studentList.add(new Student(3, "王五", 12, "13333345673"));
studentList.add(new Student(4, "赵六", 11, "13333345674"));
studentList.add(new Student(5, "大黑", 15, "13333345675"));
studentList.add(new Student(6, "大白", 10, "13333345676"));
}
/**
* 过滤出年龄< 13 的 学生
* filter()与collect()
*/
@Test
public void test1() {
List<Student> studentFilterList = studentList.stream() // 转化为流
.filter(student -> student.getAge() < 13) // 只过滤出 <13 的学生
.collect(Collectors.toList()); //输出流收集回List中,为空的情况下返回空集合
System.out.println("集合大小 : " + studentFilterList.size());
System.out.println("返回结果 : " + studentFilterList.toString());
}
2.1.2多种条件过滤
示例:过滤出年龄为10,并且名称为"大白"的学生
/**
* 多种条件过滤
* filter()与collect()
*/
@Test
public void test2() {
List<Student> studentFilterList = studentList.stream() // 转化为流
.filter(stu -> { //多种条件过滤
if (10 == stu.getAge() && "大白".equals(stu.getName())) {
return true;
}
return false;
}) // 只过滤出 <13 的学生
.collect(Collectors.toList()); //输出流收集回List中,为空的情况下返回空集合
System.out.println("集合大小 : " + studentFilterList.size());
System.out.println("返回结果 : " + studentFilterList.toString());
}
2.1.3对象转Integer或String
示例1:学生年龄Integer 集
/**
* 获得 学生年龄Integer 集
* 并去重处理
* filter()、map()、collect()、distinct()
*/
@Test
public void test3() {
List<Integer> studentAgeList = studentList.stream() // 转化为流
.map(Student::getAge) //流转化为Integer,方法引用写法,即使用 Student中 getAge() 方法
.distinct() // 去重 处理
.collect(Collectors.toList()); //输出流收集回List中,为空的情况下返回空集合
System.out.println("集合大小 : " + studentAgeList.size());
System.out.println("返回结果 : " + studentAgeList.toString());
}
/**
* 获得 学生名称String 集
* 并去重处理
* filter()、map()、collect()、distinct()
*/
@Test
public void test4() {
List<String> studentAgeList = studentList.stream() // 转化为流
.map(Student::getName) //流转化为String
.distinct() // 去重 处理
.collect(Collectors.toList()); //输出流收集回List中,为空的情况下返回空集合
System.out.println("集合大小 : " + studentAgeList.size());
System.out.println("返回结果 : " + studentAgeList.toString());
}
注:Student::getAge 方法引用写法,即Student中 getAge() 方法
2.1.4 返回集合第一条
/**
* 集合返回一条
* findFirst()、orElse()
*/
@Test
public void test5() {
Student student = studentList.stream() // 转化为流
.findFirst()//返回第一条
.orElse(new Student()); //不存在情况下返回空对象,或者返回null,示例 :orElse(null)
System.out.println(student);
}
2.5 返回集合任意一条
/**
* filter()、findAny()、orElse()
*/
@Test
public void test7() {
Student student = studentList.stream() // 转化为流
.filter(stu -> { //多种条件过滤
if (10 == stu.getAge() && "大白".equals(stu.getName())) {
return true;
}
return false;
}) // 过滤年龄等于10
.findAny() //任意找到一条符合的立即返回
.orElse(new Student()); //不存在情况下返回空对象,或者返回null,例如 :orElse(null)
System.out.println(student);
}
2.2.1 普通字符串集合分组
/**
* 普通集合分组
* 对一个List进行分组,并显示每组的个数
*/
@Test
public void test1() {
List<String> items = Arrays.asList("java", "c++", "pyhton", "php", "java");
Map<String, Long> result =
items.stream().collect(
Collectors.groupingBy(
Function.identity() // map 的 key
, Collectors.counting() // map 的 value 分组数量
));
System.out.println("集合大小 : " + result.size());
System.out.println("返回结果 : " + result.toString());
}
2.2.1 对象集合分组
/**
* 对象集合分组
* 对一个List根据年龄进行分组
*/
@Test
public void test2() {
Map<Integer, List<Student>> result =
studentList.stream().collect(Collectors.groupingBy(Student::getAge));
System.out.println("集合大小 : " + result.size());
System.out.println("返回结果 : " + result.toString());
}