lambda表达
java 8引入了Lambda表达式, 用以简化单抽象方法接口的实现
参数类型不需要申明, 参数类型自动推断
单抽象方法接口被称为函数式接口,标注为@FunctionalInterface
public static void main(String[] args) {
String[] strs = {"12","2","123","1"};
// 转换为list
List<String> strings = Arrays.asList(strs);
// 匿名内部类
Collections.sort(strings,new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
Integer i1 = Integer.valueOf(o1);
Integer i2 = Integer.valueOf(o2);
// 升序
return i1 - i2;
}
});
// 箭头函数
Collections.sort(strings,(s1,s2)->{
// 多条语句
Integer i1 = Integer.valueOf(s1);
Integer i2 = Integer.valueOf(s2);
// 降序
return i2 - i1;
});
System.out.println(strings);
}
- jdk8中使用了::的用法。
- 对象引用 :: 成员方法名
- 类名 :: 静态方法名
- 类名 :: 成员方法名
- 构造方法
- 数组调用
Integer i1 = 12;
Supplier<String> supplier = i1::toString;
System.out.println(supplier.get());
System.out.println(i1.toString());
String string = "123";
Function<String, Integer> function = Integer::parseInt;
int value = function.apply(string);
System.out.println(value);
// 泛型第一个是长度,第二个是返回值
Function<Integer, Integer[]> fun = Integer[]::new;
Integer[] arr = fun.apply(4);
for (Integer integer : arr) {
System.out.println(integer);
stream
- Java8中引入了全新的Stream API, 在java.uti.stream包中
- Stream API的特点
-
Stream API提供了一套新的流式处理的抽象序列
-
Stream API支持函数式变成/链式操作
-
Stream 可以便是无限序列, 并且大多数情况下是惰性计算的
List<String> strings = Arrays.asList("a", "b", "c", "a");
// 只要a
Stream<String> stream = strings.stream();
// collect 动作算子,把流中的数据转换为集合
// filter 不符合条件的就不要了,返回false就是不要,true就是要
List<String> value = stream.filter(x -> x.equals("a")).collect(
Collectors.toList());
System.out.println(value);
// 不要偶数
List<Integer> integers = Arrays.asList(1, 3, 5, 6, 8, 9, 11, 144, 25,
257);
Stream<Integer> stream1 = integers.stream();
List<Integer> vaIue2 = stream1.filter(x -> x % 2 == 1).collect(
Collectors.toList());
System.out.println(vaIue2);
// skip : 跳过元素
stream1 = integers.stream();
vaIue2 = stream1.skip(2).collect(Collectors.toList());
System.out.println(vaIue2);
// distinct : 去重
stream = strings.stream();
value = stream.distinct().collect(Collectors.toList());
System.out.println(value);
// map : 遍历过程中对元素进行操作,比如涨薪
List<Double> sal = Arrays.asList(1152.2,2000.5,8888.8);
Stream<Double> salStream = sal.stream();
List<Double> newSal = salStream.map(x->x*10).collect(Collectors.toList());
System.out.println(newSal);
// limit 前几条,比如查看前两名
salStream = sal.stream();
// sorted 进行排序 y-x 是降序 , x-y 是升序
newSal = salStream.sorted((x,y)->{
if(y>x){
return 1;
}else if (y == x) {
return 0;
}else{
return -1;
}
}).limit(2).collect(Collectors.toList());
System.out.println(newSal);
public static void main(String[] args) {
List<String> strings = Arrays.asList("a", "b", "c", "a", "b", "c", "a",
"b", "c");
Stream<String> stream = strings.stream();
// forEach
stream.forEach(x -> System.out.println(x));
// 计数 count
stream = strings.stream();
long count = stream.filter(x->x.equals("a")).count();
System.out.println(count);
// max 获取最大值 min 最小值
List<Integer> integers = Arrays.asList(1,2,3,4,5);
Stream<Integer> integerStream = integers.stream();
Integer i1 = integerStream.max((x,y)->x-y).get();
System.out.println(i1);
// anyMatch 匹配数据,比如是否包含
integerStream = integers.stream();
boolean flag = integerStream.anyMatch(x->x==5);
System.out.println(flag);
// 当然这种是否包含可以使用contains解决,但是有的是contains解决不了的
System.out.println(integers.contains(5));
// 比如 所有的用户中 是否有19岁的
List<User> users = new ArrayList<User>();
users.add(new User("a", 18));
users.add(new User("b", 19));
users.add(new User("c", 20));
Stream<User> userStream = users.stream();
flag = userStream.anyMatch(u->u.age==19);
System.out.println(flag);
}
}
class User{
String name;
int age;
public User(String name, int age) {
super();
this.name = name;
this.age = age;
}