java进阶|基于java8的函数式接口做一些数据处理
这次了解一下java提供的一些函数式接口,看看自己对其的理解。判断接口Predicate,这在过滤器filter()方法中用的比较多。
package com.wpw.springbootjuc.java8.map;import lombok.extern.slf4j.Slf4j;import java.util.function.Predicate;/*** Predicate接口** @author wpw*/@Slf4jpublic class PredicateTest {public static void main(String[] args) {Predicate<Integer> predicate = x -> x > 10;log.info("判断指定的数值是否符合规则");System.out.println("判断100是否大于10:" + predicate.test(100));System.out.println("判断10是否大于10:" + predicate.test(10));log.info("获取表达式的取反操作");Predicate<Integer> integerPredicate = predicate.negate();boolean flag = integerPredicate.test(100);System.out.println("flag = " + flag);boolean flag2 = integerPredicate.test(10);System.out.println("flag2 = " + flag2);}}
消费接口Consumer接口,这里也简单定义一个规则,然后进行测试。
Consumer<Integer> consumer = x -> {int a = x << 1;System.out.println("a = " + a);};consumer.accept(10);//a=10
基于BiConsumer接口进行键值对集合数据的输出。
log.info("基于BiConsumer接口进行循环遍历键和值");System.out.println();BiConsumer<String, String> biConsumer = (x, y) -> {System.out.println(String.format("键:%s,值:%s", x, y));};HashMap<String, String> hashMap = new HashMap<>(16, 0.75f);hashMap.put("姓名", "张三");hashMap.put("年龄", "10");hashMap.forEach(biConsumer);
基于Function接口进行数据的转换。
log.info("基于Function进行数据的转换,由T->R的转换");System.out.println();Function<User, UserDto> function = user -> {UserDto userDto = new UserDto();userDto.setUserId(user.getId());userDto.setUserName(user.getName());userDto.setAge(user.getAge());return userDto;};UserDto userDto = function.apply(User.builder().build().setId(1).setName("张三").setAge(10));System.out.println("userDto = " + userDto);}(chain = true)static class User {private Integer id;private String name;private Integer age;}(chain = true)static class UserDto {private Integer userId;private String userName;private Integer age;}
基于Supplier接口进行对象数据的创建和获取。
log.info("基于Suppiler接口进行对象创建的获取");Supplier<User> supplier=User::new;User user = supplier.get();//基于上面自定义user类结构信息user.setId(2).setName("李四").setAge(10);System.out.println("user = " + user);
基于IntFunction接口进行数据规则的定义,然后进行一些操作的处理。
log.info("自定IntFunction接口进行规则的定义");IntFunction<Integer> integerIntFunction = x -> x << 1;System.out.println("integerIntFunction = " + integerIntFunction.apply(2));
基于BiFunction接口进行一些规则,然后进行返回新的对象信息。
log.info("基于BiFunction接口进行一些规则的定义");BiFunction<Integer, Integer, Integer> biFunction = (x, y) -> {return x + y;};Integer integer = biFunction.apply(10, 10);System.out.println("integer = " + integer);System.out.println();BiFunction<User, UserDto, Person> personFunction = (x, y) -> {Person person = Person.builder().build();person.setId(3);person.setAge(x.getAge() + y.getAge());person.setName("王五");return person;};Person person = personFunction.apply(user, userDto);System.out.println("person = " + person);@AllArgsConstructor@NoArgsConstructor@Data@Builder@Accessors(chain = true)static class Person {private Integer id;private String name;private Integer age;}
基于BiPredicate接口进行一些自定义规则,然后根据规则进行一些数据的处理。
log.info("基于BiPredicate接口进行一些数据的处理");BiPredicate<Integer, Integer> biPredicate = (x, y) -> x.compareTo(y) == 0;System.out.println("判断是否符合自定义规则" + biPredicate.test(10, 20));BiPredicate<String, String> stringBiPredicate = (x, y) -> {return x.length() > y.length();};System.out.println("stringBiPredicate) = " + stringBiPredicate.test("abcd","bcdef"));
基于DoubleToIntFunction接口进行数据的处理。
DoubleToIntFunction doubleToIntFunction = x -> ((int) x);int a = doubleToIntFunction.applyAsInt( 6.00);System.out.println("a = " + a);
觉得这种方式实现起来挺简单的,就简单介绍一下这种方式好了。
PredicateTest.Test test=new PredicateTest.Test();int b = test.applyAsInt(9.00);System.out.println("b = " + b);static class Test implements DoubleToIntFunction {@Overridepublic int applyAsInt(double value) {return (int) value;}}
基于这些接口,自己也算总结了一下,便于以后可以很好的查找。
