当前位置: 首页 > wzjs >正文

企业网站设计费用百度站长统计

企业网站设计费用,百度站长统计,如何创建网站赚钱,丹江口网站开发Stream流常见的中间方法 Stream流常见的终结方法 代码 学生类&#xff08;代码一与代码二共涉及到的类&#xff09; package com.itheima.day28_Stream;import java.util.Objects;public class Student implements Comparable<Student> {private String name;private i…

Stream流常见的中间方法

Stream流常见的中间方法

Stream流常见的终结方法

Stream流常见的终结方法
Stream流常见的终结方法2

代码

学生类(代码一与代码二共涉及到的类)

package com.itheima.day28_Stream;import java.util.Objects;public class Student implements Comparable<Student> {private String name;private int age;private double height;@Overridepublic String toString() {return "Student{" +"name='" + name + '\'' +", age=" + age +", height=" + height +'}';}@Overridepublic boolean equals(Object o) {if (this == o) return true;if (o == null || getClass() != o.getClass()) return false;Student student = (Student) o;return age == student.age && Double.compare(height, student.height) == 0 && Objects.equals(name, student.name);}@Overridepublic int hashCode() {return Objects.hash(name, age, 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 int compareTo(Student o) {return this.age - o.age;//年龄升序排序}
}

代码一:掌握Stream流提供的常见中间方法

package com.itheima.day28_Stream;import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Stream;//目标:掌握Stream流提供的常见中间方法
public class StreamTest3 {public static void main(String[] args) {List<Double> scores = new ArrayList<>();Collections.addAll(scores,88.5,56.6,58.8,89.9,100.0,16.6);// 需求1:找出成绩大于等于60分的数据,并升序后,再输出。scores.stream().filter(s->s>=60).sorted().forEach(s->System.out.println(s));List<Student>students =new ArrayList<>();Student s1 = new Student("蜘蛛精",26,172.5);Student s2 = new Student("蜘蛛精",26, 172.5);Student s3 = new Student( "紫霞",23, 167.6);Student s4 = new Student("白晶品", 25,169.0);Student s5 = new Student("牛魔王",35,183.3);Student s6 =new Student( "牛夫人", 34,168.5);Collections.addAll(students,s1,s2,s3,s4,s5,s6);// 需求2:找出年龄大于等于23,且年龄小于等于30岁的学生,并按照年龄降序输出.students.stream().filter(s -> s.getAge()>=23 && s.getAge()<=30).sorted((o1,o2)-> o2.getAge()-o1.getAge())//使用lambda简写,年龄降序排列.forEach(s->System.out.println(s));System.out.println("===========================================");//需求3:取出身高最高的前3名学生,并输出。students.stream().sorted((o1,o2)->Double.compare(o2.getHeight(),o1.getHeight())).limit(3).forEach(System.out::println);System.out.println("===========================================");// 需求4:取出身高倒数的2名学生,并输出。students.stream().sorted((o1,o2)->Double.compare(o2.getHeight(),o1.getHeight())).skip(students.size()-2).forEach(System.out::println);System.out.println("===========================================");// 需求5:找出身高超过168的学生叫什么名字,要求去除重复的名字,再输出。students.stream().filter(s->s.getHeight()>168).map(Student::getName)//把原来的学生对象集合加工(映射)成为名字集合.distinct()//去除重复的名字.forEach(System.out::println);System.out.println("===========================================");// distinct去重复,自定义类型的对象(希望内容一样就认为重复,重写hashCode,equals)students.stream().distinct().forEach(System.out::println);System.out.println("===========================================");//合并两个stream流Stream<String> str1 = Stream.of("张三", "李四");Stream<String> str2 = Stream.of("张3", "李4","王五");Stream<String> Allst = Stream.concat(str1, str2);Allst.forEach(System.out::println);}
}

结果1.1
结果1.2

代码二:掌握Stream流提供的常见终结方法

package com.itheima.day28_Stream;import java.util.*;
import java.util.stream.Collectors;
//目标:掌握Stream流提供的常见终结方法
public class StreamTest4 {public static void main(String[] args) {List<Student> students =new ArrayList<>();Student s1 = new Student("蜘蛛精",26,172.5);Student s2 = new Student("蜘蛛精",26, 172.5);Student s3 = new Student( "紫霞",23, 167.6);Student s4 = new Student("白晶品", 25,169.0);Student s5 = new Student("牛魔王",35,183.3);Student s6 =new Student( "牛夫人", 34,168.5);Collections.addAll(students,s1,s2,s3,s4,s5,s6);// 需求1:请计算出身高超过168的学生有几人。long count = students.stream().filter(s -> s.getHeight() > 168).count();System.out.println(count);System.out.println("===================================");// 需求2:请找出身高最高的学生对象,并输出。Student stu1 = students.stream().max((o1, o2) -> Double.compare(o1.getHeight(), o2.getHeight())).get();System.out.println(stu1);System.out.println("===================================");// 需求3:请找出身高最矮的学生对象,并输出。Student stu2 = students.stream().min((o1, o2) -> Double.compare(o1.getHeight(), o2.getHeight())).get();System.out.println(stu2);System.out.println("===================================");// 需求4:请找出身高超过170的学生对象,并放到一个新集合中去返回。//流只能收集一次,因此"students.stream().filter(s -> s.getHeight() > 170)"不能单独作为新的stream流分别取做collect(Collectors.toList())与collect(Collectors.toSet())操作List<Student> students1 = students.stream().filter(s -> s.getHeight() > 170).collect(Collectors.toList());System.out.println(students1);Set<Student> students2 = students.stream().filter(s -> s.getHeight() > 170).collect(Collectors.toSet());System.out.println(students2);// 需求5:请找出身高超过170的学生对象,并把学生对象的名字和身高,存入到一个Map集合返回。System.out.println("===================================");Map<String, Double> map = students.stream().filter(s -> s.getHeight() > 170).distinct()//map集合的元素不可重复,因此先去重.collect(Collectors.toMap(a -> a.getName(), a -> a.getHeight()));System.out.println(map);System.out.println("===================================");//存入到数组//写法1Object[] array1 = students.stream().filter(s -> s.getHeight() > 170).toArray();System.out.println(Arrays.toString(array1));System.out.println("======================================");//写法2Student[] array2 = students.stream().filter(s -> s.getHeight() > 170).toArray(len -> new Student[len]);System.out.println(Arrays.toString(array2));}
}

结果2

http://www.dtcms.com/wzjs/132349.html

相关文章:

  • 广州市网站建设制作费用微信营销软件排行榜
  • 怎样设计网站版面百度主页网址
  • 做爰的网站百度一下首页设为主页
  • 免费网站下载app软件搜索广告和信息流广告区别
  • envato wordpress toolkitseo是搜索引擎吗
  • 西语网站域名湖南知名网络推广公司
  • wordpress 上传模板武汉本地seo
  • 找做网站的seo诊断的网络问题
  • 网站建设原理与实践2345网址导航 中国最
  • oss静态网站托管推广普通话的意义30字
  • web网站源码广州专业网络推广公司
  • 出入合肥最新通知今天sem优化策略
  • 小男孩和女人做的网站百度网站排名
  • 做ppt的模板的网站平台优化是指什么
  • 做基因功能注释的网站微信软文
  • 武汉高端商城网站建设seo先上排名后收费
  • 网站页面 原则昆明seo网站管理
  • 合肥小程序设计seo关键字优化价格
  • 北京seo优化网站建设保定关键词排名推广
  • 免费资源部落wordpress福建键seo排名
  • 自动建站网站源码网站设计公司报价
  • php做的网站有哪些ks数据分析神器
  • 网站建设联盟重庆百度搜索优化
  • 小程序wordpress打包网站页面优化内容包括哪些
  • 云主机 网站指南seo自动刷外链工具
  • 网站的虚拟人怎么做的上海网站营销推广
  • 钟楼做网站seo网站是什么意思
  • 军事新闻2023网站做优化好还是推广好
  • 临平网站建设新开网站
  • 联系我们_网站制作公司seo关键词优化指南