Day07 Stream流递归Map集合Collections可变参数

编程入门 行业动态 更新时间:2024-10-27 18:20:48

Day07 Stream流<a href=https://www.elefans.com/category/jswz/34/1771140.html style=递归Map集合Collections可变参数"/>

Day07 Stream流递归Map集合Collections可变参数

Stream

        也叫Stream流,是Jdk8开始新增的一套API (java.util.stream.*),可以用于操作集合或者数组的数据。

       Stream流大量的结合了Lambda的语法风格来编程,提供了一种更加强大,更加简单的方式操作

public class Demo1 {public static void main(String[] args) {List<String> list = new ArrayList<>();list.add("张无忌");list.add("周芷若");list.add("赵敏");list.add("张强");list.add("张三丰");//需求:把集合中所有以"张"开头,且是3个字的元素存储到一个新的集合。//实现2: 使用Stream流方式实现List<String> list1 = list.stream().filter(name -> name.startsWith("张") ).filter(name -> name.length() == 3).collect(Collectors.toList());System.out.println(list1);}
}

Stream流的使用步骤 

1.                                       2.                                              3.

                  

如何获取Stream流 

如何获取Stream流:Collection集合:单列集合都支持一个stream()方法,它可以直接获取集合的Stream流数组:Arrays.stream(数组)零散数据:Stream.of(T... values)Map双列集合并没有提供直接获取Stream流的方法,他需要间接获取
public class Demo2 {public static void main(String[] args) {//"玄奘", "悟空", "悟能", "悟净"List<String> list = List.of("玄奘", "悟空", "悟能", "悟净");//Collection集合: 单列集合都支持一个stream()方法,它可以直接获取集合的Stream流list.stream().forEach(System.out::println);//数组:Arrays.stream(数组)System.out.println("-------------------------------------");String[] arr = {"玄奘", "悟空", "悟能", "悟净"};Arrays.stream(arr).forEach(System.out::println);//零散数据:Stream.of(T... values)System.out.println("-------------------------------------");Stream.of("玄奘", "悟空", "悟能", "悟净").forEach(System.out::println);//Map:双列集合并没有提供直接获取Stream流的方法,他需要间接获取System.out.println("-------------------------------------");Map<String,String> map = new HashMap<>();map.put("001","玄奘");map.put("002","悟空");map.put("003","悟能");map.put("004","悟净");//返回一个Set集合map.keySet().stream().forEach(System.out::println);map.values().stream().forEach(System.out::println);System.out.println("-------------------------------------");//这是一个整体EntrySetkeyvalue -> emap.entrySet().stream().forEach(e -> System.out.println(e.getValue() + e.getKey()));}
}

Stream流常见中间方法 

 

public class Demo3 {public static void main(String[] args) {List<Integer> list = List.of(61, 57, 66, 77, 88, 44, 100, 89, 97, 47, 70);//需求1: 找出所有及格的分数,并打印System.out.println("=================");list.stream().filter(e -> e > 60).forEach(System.out::println);//需求2: 找出所有及格的分数, 正序排列, 打印输出System.out.println("=================");list.stream().filter(e -> e > 60).sorted((o1, o2) -> o1 - o2).forEach(System.out::println);//需求3: 找出所有及格的分数, 倒序排列, 打印输出System.out.println("=================");list.stream().filter(e -> e > 60).sorted((o1, o2) -> o2 - o1).forEach(System.out::println);//需求4: 找出所有及格的分数, 倒序排列, 取前3名, 打印输出System.out.println("=================");list.stream().filter(e -> e > 60).sorted((o1, o2) -> o2 - o1).limit(3).forEach(System.out::println);//需求5: 找出所有及格的分数, 倒序排列, 取前4-6名, 打印输出System.out.println("=================");list.stream().filter(e -> e > 60).sorted((o1, o2) -> o2 - o1).skip(3).limit(3).forEach(System.out::println);//需求6:  找出所有及格的分数, 倒序排列, 取前4-6名, 将每个人的分数加10分, 打印输出System.out.println("=================");list.stream().filter(e -> e > 60).sorted((o1, o2) -> o2 - o1).skip(3).limit(3).map(e -> e + 10).forEach(System.out::println);//需求7: 将下面两个集合中的元素进行合并去重System.out.println("=================");List<String> list1 = List.of("1","2","3","4");List<String> list2 = List.of("1","5","7","4");Stream.concat(list1.stream(),list2.stream()).distinct().forEach(System.out::println);}
}

 Stream流常见终结方法

                     调用完成后,不会返回新Stream了,没法继续使用流了。 

 

public class Demo4 {public static void main(String[] args) {List<Student> list = List.of(new Student("玄奘", 60, 165.5),new Student("悟空", 50, 175.5),new Student("悟能", 55, 145.5),new Student("悟净", 40, 185.5));//1. 打印出集合中所有元素list.stream().forEach(System.out::println);//2. 统计出身高不足170的人数Stream<Student> stream = list.stream().filter(e -> e.getHeight() < 170);long count = stream.count();System.out.println(count);//3. 请找出年龄最大的对象, 并输出(了解)Student student = list.stream().max((o1, o2) -> o1.getAge() - o2.getAge()).get();System.out.println(student);//4. 请找出身高最低的对象, 并输出(了解)Student student2 = list.stream().min((o1, o2) -> Doublepare(o1.getHeight(),o2.getHeight())).get();System.out.println(student2);}
}class Student {private String name;private int age;private double height;public Student() {}public Student(String name, int age, double height) {this.name = name;this.age = age;this.height = height;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public double getHeight() {return height;}public void setHeight(double height) {this.height = height;}@Overridepublic String toString() {return "Student{" +"name='" + name + '\'' +", age=" + age +", height=" + height +'}';}
}

 

Stream流转数组/集合 

public class Demo5 {public static void main(String[] args) {List<Teacher> list = List.of(new Teacher("玄奘", 60, 165.5),new Teacher("悟空", 50, 175.5),new Teacher("悟空", 50, 175.5),new Teacher("悟能", 55, 145.5),new Teacher("悟净", 40, 185.5));//1. 请找出身高超过170的教师, 并放到一个新数组中Object[] objects = list.stream().filter(e -> e.getHeight() > 170).toArray();System.out.println(Arrays.toString(objects));Teacher[] teacher =  list.stream().filter(e -> e.getHeight() > 170)//len代表元素中的个数.toArray(len -> new Teacher[len]);System.out.println(Arrays.toString(teacher));//2. 请找出身高超过170的教师, 并放到一个新List集合中List<Teacher> list1 = list.stream().filter(e -> e.getHeight() > 170).collect(Collectors.toList());System.out.println(list1);//3. 请找出身高超过170的教师, 并放到一个新Set集合中Set<Teacher> list2 = list.stream().filter(e -> e.getHeight() > 170).collect(Collectors.toSet());System.out.println(list2);//4. 请找出所有的教师的姓名和身高, 放到一个新Map集合中Map<String,Double> map = list.stream().distinct().collect(Collectors.toMap(e -> e.getName(), e-> e.getHeight()));System.out.println(map);}
}class Teacher {private String name;private int age;private double height;public Teacher() {}public Teacher(String name, int age, double height) {this.name = name;this.age = age;this.height = height;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public double getHeight() {return height;}public void setHeight(double height) {this.height = height;}@Overridepublic boolean equals(Object o) {if (this == o) return true;if (o == null || getClass() != o.getClass()) return false;Teacher teacher = (Teacher) o;return age == teacher.age && Doublepare(teacher.height, height) == 0 && Objects.equals(name, teacher.name);}@Overridepublic int hashCode() {return Objects.hash(name, age, height);}@Overridepublic String toString() {return "Teacher{" +"name='" + name + '\'' +", age=" + age +", height=" + height +'}';}
}

Map集合

1.Map集合称为双列集合,一次需要存一对数据做为一个元素, 格式:{key1=value1 ,        key2=value2 , key3=value3 , ...}

2.Map集合的所有键是不允许重复的,但值可以重复,键和值是一一对应的,每一个键只能找到自己对应的值

Map集合体系

 

Map集合的实现类有哪些?各自的特点是? 

   1.HashMap: 无序,不重复

   2.LinkedHashMap: 有序,不重复

   3.TreeMap:排序,不重复 

 Map接口常用方法

 

Map集合三种遍历方式 

public class Demo2 {public static void main(String[] args) {//1. 创建mapHashMap<String, String> map = new HashMap<>();map.put("001", "玄奘");map.put("002", "悟空");map.put("003", "悟能");map.put("004", "悟净");//2. 各种方式进行遍历test1(map);System.out.println("=================");test2(map);System.out.println("=================");test3(map);}//遍历方式1: 先获取Map集合全部的键,再通过遍历键来找值private static void test1(HashMap<String, String> map) {Set<String> set = map.keySet();for (String s : set) {System.out.println(s + map.get(s));}}//遍历方式2: 将map中的所有键值对放入一个set集合中, 然后遍历set集合拿到每个键值对, 再取里面的键值private static void test2(HashMap<String, String> map) {Set<Map.Entry<String, String>> entries = map.entrySet();for (Map.Entry<String, String> entry : entries) {System.out.println(entry.getKey() + entry.getValue());}}//遍历方式3: Lambda, 使用foreach(BiConsumer bc)private static void test3(HashMap<String, String> map) {map.forEach((k,v) -> System.out.println(k+v));}
}

Map集合案例 

 

现有字符串数组如下:String[] bookArr = {"《红楼梦》-曹雪芹","《西游记》-吴承恩","《三国演义》-罗贯中","《水浒传》-施耐庵"};
需求:请将字符串中的书名提取为Map集合的键,将作者提取为Map集合的值并使用三种不同方式,遍历Map集合打印键值对元素内容
public class Demo3 {public static void main(String[] args) {Map<String,String> map = new HashMap<>();String[] bookArr = {"《红楼梦》-曹雪芹","《西游记》-吴承恩","《三国演义》-罗贯中","《水浒传》-施耐庵"};for (String book : bookArr) {String[] split = book.split("-");map.put(split[0],split[1] );}System.out.println(map);Set<String> set = map.keySet();for (String key : set) {System.out.println(key + map.get(key));}System.out.println("----------------------------");Set<Map.Entry<String, String>> entries = map.entrySet();for (Map.Entry<String, String> entry : entries) {System.out.println(entry.getKey()+entry.getValue());}System.out.println("----------------------------");map.forEach((key , value) -> System.out.println(key+value));}
}

 HashMap底层原理

      和HashSet一样 :Day06List接口&Set接口&树-CSDN博客=1001.2014.3001.5501

LinkedHashMap

底层数据结构依然是基于哈希表实现的,只是每个键值对元素又额外的多了一个双链表的机制记录元素顺序(保证有序)。 

TreeMap 

public class Demo6 {private String put;public static void main(String[] args) {//创建集合Map<Teacher, String> map = new TreeMap<>((o1, o2) -> o1.getAge() - o2.getAge());map.put(new Teacher("张三", 21), "河北");map.put(new Teacher("李四", 20), "山东");map.put(new Teacher("王五", 19), "山西");map.put(new Teacher("赵六", 21), "河南");map.forEach((k, v) -> {System.out.println(k + "-----" + v);});}
}class Teacher {private String name;private int age;public Teacher() {}public Teacher(String name, int age) {this.name = name;this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}@Overridepublic String toString() {return "Teacher{" +"name='" + name + '\'' +", age=" + age +'}';}
}

 集合的嵌套

集合嵌套要求在程序中记住如下省份和其对应的城市信息,记录成功后,要求可以查询出湖北省的城市信息。数据江苏省 = "南京市","扬州市","苏州市","无锡市","常州市"湖北省 = "武汉市","孝感市","十堰市","宜昌市","鄂州市"河北省 = "石家庄市","唐山市","邢台市","保定市","张家口市"分析:定义一个Map集合,键用表示省份名称,值表示城市名称,注意:城市会有多个。 Map<String,List<String>>根据“湖北省”这个键获取对应的值展示即可。
public class Demo7 {public static void main(String[] args) {Map<String, List<String>> map = new HashMap<>();List<String> list = List.of("南京市","扬州市","苏州市","无锡市","常州市");List<String> list2 = List.of("石家庄市","唐山市","邢台市","保定市","张家口市");map.put("江苏省",list);map.put("河北省",list2);System.out.println(map);}
}

 Collections

  可变参数 

可变参数就是一种特殊形参,定义在方法、构造器的形参列表里,格式是:数据类型... 参数名称优点特点:可以不传数据给它;可以传一个或者同时传多个数据给它;也可以传一个数组给它。好处:常常用来灵活的接收数据。注意事项1. 可变参数在方法内部就是一个数组2. 一个形参列表中可变参数只能有一个3. 可变参数必须放在形参列表的最后面
public class Demo {public static void main(String[] args) {sum(1546);}//计算2个整数的和//计算3个整数的和//计算4个整数的和//计算n个整数的和public static void sum(int... a){for (int i : a) {System.out.println(i);}}
}

 Collections工具类

 

public class Demo {public static void main(String[] args) {List<Integer> list = new ArrayList<>();//static <T> boolean addAll(单列集合,可变参数)   批量添加元素Collections.addAll(list,1,2,100,3,45);System.out.println(list);//static void shuffle(List集合)    打乱List集合元素顺序,每次调用都会打乱Collections.shuffle(list);System.out.println(list);//static <T> void  sort(List集合)   List集合进行自然排序Collections.sort(list);System.out.println(list);//排自定义类对象,需要指定排序规则List<Student> stuList = new ArrayList<>();stuList.add(new Student("zhangsan", 18));stuList.add(new Student("wangwu", 22));stuList.add(new Student("zhaoliu", 21));stuList.add(new Student("lisi", 19));stuList.add(new Student("qianqi", 20));//static<T> void sort(List集合,比较器);List集合进行比较器排序Collections.sort(stuList,(o1, o2) -> o1.getAge() - o2.getAge());System.out.println(stuList);}
}class Student {private String name;private int age;public Student() {}public Student(String name, int age) {this.name = name;this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}@Overridepublic String toString() {return "Student{" +"name='" + name + '\'' +", age=" + age +'}';}
}

 

 

 

 

 

更多推荐

Day07 Stream流递归Map集合Collections可变参数

本文发布于:2023-12-03 20:45:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1657462.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:递归   参数   Stream   Map   Collections

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!