时间:2023-04-24 11:09:01 | 来源:网站运营
时间:2023-04-24 11:09:01 来源:网站运营
山寨Stream API:摘自《Java中级路线》的Java8新特性一章。List<Person> list = new ArrayList<>();请大家先自行实现这个需求。
list.add(new Person("李健", 46));
list.add(new Person("周深", 28));
list.add(new Person("张学友", 59));
请筛选出年龄大于40的歌手的名字
public class ChainCall { public static void main(String[] args) { Person bravo1988 = new Person().setName("bravo1988").setAge(18).setMoney(1000.0); }}class Person { private String name; private Integer age; private Double money; public String getName() { return name; } public Integer getAge() { return age; } public Double getMoney() { return money; } public Person setName(String name) { this.name = name; return this; } public Person setAge(Integer age) { this.age = age; return this; } public Person setMoney(Double money) { this.money = money; return this; }}
/** * @author bravo * @date 2020-03-19 17:55 */public class MockStream { public static void main(String[] args) throws JsonProcessingException { MyList<Person> personMyList = new MyList<>(); personMyList.add(new Person("李健", 46)); personMyList.add(new Person("周深", 28)); personMyList.add(new Person("张学友", 59)); // 需求:过滤出年龄大于40的歌手的名字 MyList<String> result = personMyList.filter(person -> person.getAge() > 40).map(person -> person.getName()); prettyPrint(result.getList()); System.out.println("/n---------------------------------------------/n"); // 对比真正的Stream API List<Person> list = new ArrayList<>(); list.add(new Person("李健", 46)); list.add(new Person("周深", 28)); list.add(new Person("张学友", 59)); List<String> collect = list.stream().filter(person -> person.getAge() > 40).map(person -> person.getName()).collect(Collectors.toList()); prettyPrint(collect); } /** * 按JSON格式输出 * * @param obj * @throws JsonProcessingException */ private static void prettyPrint(Object obj) throws JsonProcessingException { ObjectMapper objectMapper = new ObjectMapper(); String s = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(obj); System.out.println(s); }}@Data@AllArgsConstructorclass Person { private String name; private Integer age;}@Getterclass MyList<T> { private List<T> list = new ArrayList<>(); public boolean add(T t) { return list.add(t); } /** * 给MyList传递具体的判断规则,然后MyList把内部现有符合判断(true)的元素集返回 * @param predicate * @return */ public MyList<T> filter(Predicate<T> predicate){ MyList<T> filteredList = new MyList<>(); for (T t : list) { if (predicate.test(t)) { // 收集判断为true的元素 filteredList.add(t); } } return filteredList; } /** * 把MyList中的List<T>转为List<R> * * @param mapper * @param <R> * @return */ public <R> MyList<R> map(Function<T, R> mapper) { MyList<R> mappedList = new MyList<>(); for (T t : list) { mappedList.add(mapper.apply(t)); } return mappedList; }}/** * 定义一个Predicate接口,名字无所谓 * * @param <T> */@FunctionalInterfaceinterface Predicate<T> { /** * 定义了一个test()方法,传入任意对象,返回true or false,具体判断逻辑由子类实现 * * @param t * @return */ boolean test(T t);}/** * 定义一个Function接口,名字无所谓 * * @param <E> * @param <R> */@FunctionalInterfaceinterface Function<E, R> { /** * 定义一个apply()方法,接收一个E返回一个R。也就是把E映射成R * * @param e * @return */ R apply(E e);}
看不懂没关系,感受一下即可。关键词:山寨