Lambda
JDK8开始支持Lambda表达式,用来让程序编写更优雅
利用Lambda可以更简洁的实现匿名内部类与函数声明与调用
基于Lambda提供stream流式处理极大简化对集合的操作
创建接口
@FunctionalInterface
public interface MathOperation {
public Float operate(Integer a, Integer b);
}
//传统方法
MathOperation addition2 = new MathOperation() {
@Override
public Float operate(Integer a, Integer b) {
return a + b + 0f;
}
};
System.out.println(addition2.operate(5, 4));
// 1.标准使用方法
MathOperation addition = (Integer a, Integer b) -> {
System.out.println("加法运算");
return a + b + 0f;
};
System.out.println(addition.operate(5, 3));
// 2.Lambda允许忽略参数类型
MathOperation subtraction = (a, b) -> {
System.out.println("减法运算");
return a - b + 0f;
};
System.out.println(subtraction.operate(5, 3));
// 3.单行实现代码可以省略大括号和return
MathOperation multiplication = (a, b) -> a * b + 0f;
System.out.println("执行乘法");
System.out.println(multiplication.operate(5, 4));
函数式编程
函数式编程是基于函数式接口并使用lambda表达的编程方式
函数式编程理念是将代码作为可重用数据代入到程序运行中
函数式编程强调"你想做什么",而不是"你想怎么做"
函数式接口是有且只有一个抽象方法的接口
Java中拥有大量函数式接口,如java.lang.Runnable
JDK8后提供了一系列新的函数式接口,位于java.util.function
函数式接口Predicate
Predicate是新增的函数式接口,位于java.util.function
Predicate用于测试传入的数据是否满足判断要求
Predicate接口需要实现test()方法进行逻辑判断
Predicate<Integer> predicate = n -> n > 4;
boolean result = predicate.test(10);
System.out.println(result);
public static void main(String[] args) {
// 筛选奇数
filter(list, n -> n % 2 == 1);
// 筛选偶数
filter(list, n -> n % 2 == 0);
// 大于5的数
filter(list, n -> n > 5 && n % 2 == 0);
}
public static void filter(List<Integer> list, Predicate<Integer> predicate) {
for (Integer num :
list) {
if (predicate.test(num)) {
System.out.print(num + " ");
}
}
System.out.println();
}
JDK8常用函数式接口
接口 | 用途 |
---|---|
Consumer | 对应有一个输入参数无输出的功能代码 |
Function<T,R> | 对应有一个输入参数并且需要返回数据的功能代码 |
Predicate | 用于条件判断,返回布尔值 |
Consumer函数式接口
public static void main(String[] args) {
output(s -> System.out.println("向控制台打印:" + s));
output(s -> {
System.out.println("向xxx_网站发送数据包:" + s);
});
}
public static void output(Consumer<String> consumer) {
String text = "好好学习";
consumer.accept(text);
}
Function函数式接口
public static void main(String[] args) {
Function<Integer, String> randomStringFunction = l -> {
String chars = "abc123";
StringBuilder stringBuffer = new StringBuilder();
Random random = new Random();
for (int i = 0; i < l; i++) {
int position = random.nextInt(chars.length());
stringBuffer.append(chars.charAt(position));
}
return stringBuffer.toString();
};
System.out.println(randomStringFunction.apply(32));
}
在接口上添加注解
@FunctionalInterface // 通知编译器这是函数式接口
public interface MathOperation {
public Float operate(Integer a, Integer b);
}
Stream流式处理
Stream流式处理是建立在Lambda基础上的多数据处理技术
Stream对集合数据处理进行高度抽象,极大简化代码量
Stream可对集合进行迭代,去重,筛选,排序,聚合等一系列处理
Stream常用方法
接口 | 用途 |
---|---|
forEach | 循环遍历 |
map | map 方法用于映射每个元素到对应的结果 |
filter | filter 方法用于对通过的设置条件过滤除元素 |
limit | limit 方法用于获取指定数量的流 |
sorted | sorted 方法用于对流进行排序 |
Collectors | Collectors 类实现将流转换成集合和聚合元素 |
Stream流的五种创建方式
public class StreamGenerator {
// 基于数组进行创建
@Test
public void generator1() {
String[] arr = {"李莉", "安迪", "史蒂斯"};
Stream<String> stream = Stream.of(arr);
stream.forEach(System.out::println);
}
// 基于集合进行创建
@Test
public void generator2() {
List<String> list = new ArrayList<>();
list.add("张三");
list.add("李四");
list.add("王五");
Stream<String> stream = list.stream();
stream.forEach(System.out::println);
}
// 创建无限长度流
@Test
public void generator3() {
Stream<Integer> stream = Stream.generate(() -> new Random().nextInt(10000) + 1);
stream.limit(100).forEach(System.out::println);
}
// 基于迭代器创建流
@Test
public void generator4() {
Stream<Integer> stream = Stream.iterate(1, n -> n + 1);
stream.limit(20).forEach(System.out::println);
}
// 基于字符序列创建流
@Test
public void generator5() {
String str = "abcdefg";
IntStream stream = str.chars();
stream.forEach(c -> System.out.println((char) c));
}
Stream常用方法
public class StreamMethod {
// 提取集合中所有的偶数并求和
@Test
public void case1() {
List<String> list = Arrays.asList("1", "2", "3", "4", "5", "6", "7");
int sum = list.stream()
.mapToInt(s -> Integer.parseInt(s))
.filter(n -> n % 2 == 0)
.sum();//获取stream对象
System.out.println(sum);
}
// 所有名字首字母大写
@Test
public void case2() {
List<String> list = Arrays.asList("lily", "jack", "tom");
List<String> newList = list.stream()
// 按规则对每一个流数据进行转换
.map(s -> s.substring(0, 1).toUpperCase() + s.substring(1))
// .forEach(System.out::println)
// collect对数据进行手机,生成新的List/Set
.collect(Collectors.toList());
System.out.println(newList);
}
// 将所有奇数从大到小进行排序,切不许出现重复
@Test
public void case3() {
List<Integer> list = Arrays.asList(1, 34, 76, 345433, 234, 2, 4, 6, 1, 43, 1, 2, 345, 3);
List<Integer> newList = list.stream()
// 去除重复的流数据
.distinct()
.filter(n -> n % 2 == 1)
.sorted((a, b) -> b - a)
.collect(Collectors.toList());
System.out.println(newList);
}
}