黑马Stream教程-练习题

avatar
作者
猴君
阅读量:0

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

import java.util.*; import java.util.stream.Collectors; import java.util.stream.Stream;  import static java.util.List.*;  class Actor {     private String name;     private Integer age;      public Actor(String name, int age) {         this.name = name;         this.age = age;     }      public void setAge(Integer age) {         this.age = age;     }      public Integer getAge() {         return age;     }      public String getName() {         return name;     }      public void setName(String name) {         this.name = name;     }  } public class Main1 {     public static void main(String[] args) { //        System.out.println("Hello World!"); // //        List<String> list = of("张三1", "李四", "王五", "张六2"); //        list.stream().filter(name -> name.startsWith("张")).filter(name -> name.length() == 3).forEach(System.out::println); //        ArrayList<String> list = new ArrayList<>(); //        Collections.addAll(list,"a","b","c","d","e");         Stream<String> stream = list.stream(); //        list.stream().forEach(System.out::println);          ArrayList<String> list = new ArrayList<>();         Collections.addAll(list,"张无忌-男-15","周芷若-女-14","赵敏-女-13","张强-男-20","张三丰-男-100","张翠山-男-40","张良-男-35","王二麻子-男-37","谢广坤-男-41");  //        String[] array = list.stream().toArray(value -> new String[value]); //        System.out.println(Arrays.toString(array));        // list.stream().filter(e -> "男".equals(e.split("-")[1])).collect(Collectors.toList()).forEach(System.out::println);         //System.out.println("---------------------");       //  list.stream().filter(e -> "男".equals(e.split("-")[1])).collect(Collectors.toSet()).forEach(System.out::println);        //  System.out.println("-----------to Map-----------");      //   list.stream().filter(e -> "男".equals(e.split("-")[1])).collect(Collectors.toMap(e -> e.split("-")[0], e -> e.split("-")[2])).forEach((k,v) -> System.out.println(k + ":" + v));         System.out.println("-----------Practice 1-----------");         ArrayList<Integer> integers = new ArrayList<>();         Collections.addAll(integers,1,2,3,4,5,6,7,8,9,10);         integers.stream().filter(ele->ele%2==0).forEach(System.out::println);         System.out.println("-----------Practice 2-----------");         ArrayList<String> list1 = new ArrayList<>();         Collections.addAll(list1,"zhangsan,23","lisi,24","wangwu,25");         list1.stream().filter(strEle->Integer.parseInt(strEle.split(",")[1])>=24).collect(Collectors.toMap(strEle->strEle.split(",")[0],strEle->strEle.split(",")[1])).forEach((k,v)->System.out.println(k+":"+v));         System.out.println("-----------Practice 3-----------");         ArrayList<String> L1 = new ArrayList<>();         Collections.addAll(L1,"蔡坤坤,24","叶齁咸,23","刘不甜,22","吴签,24","谷嘉,30”,“肖梁梁,27");         ArrayList<String> L2 = new ArrayList<>();         Collections.addAll(L2,"赵小颖,35","杨颖,36","高元元,43","张天天,31","刘诗,35","杨小幂,33");         Stream<String> stream1 = L1.stream().filter(e -> e.split(",")[0].length() == 3).limit(2);         Stream<String> stream2 = L2.stream().filter(e -> e.split(",")[0].startsWith("杨")).skip(1);         Stream.concat(stream1,stream2).map(e->new Actor(e.split(",")[0],Integer.parseInt(e.split(",")[1]))         ).collect(Collectors.toList()).forEach(actor-> System.out.println(actor.getName()+"---"+actor.getAge()));      } } 

广告一刻

为您即时展示最新活动产品广告消息,让您随时掌握产品活动新动态!