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

公众号制作多少钱seo免费

公众号制作多少钱,seo免费,厦门市住房和城乡建设局网站首页,宁波网站建设信息推荐目录 1.体验Stream流的便捷 2.Stream流的创建 3.Stream流的中间方法 4.Stream的终结方法 1.体验Stream流的便捷 是专业用于对集合或者数组进行便捷操作的 如案例: 有一个 List 集合,元素有 " 张三丰 "," 张无忌 "," 周芷若 ",&qu…

目录

1.体验Stream流的便捷

2.Stream流的创建

3.Stream流的中间方法

4.Stream的终结方法


1.体验Stream流的便捷

是专业用于对集合或者数组进行便捷操作的
如案例:
有一个 List 集合,元素有 " 张三丰 "," 张无忌 "," 周芷若 "," 赵敏 "," 张强 " ,找出姓张,且是 3 个字的名
字,存入到一个新集合中去。
List<String> names = new ArrayList<>();
Collections.addAll(names, " 张三丰 "," 张无忌 "," 周芷若 "," 赵敏 "," 张强 ");
System.out.println(names);
用传统方式来做,代码是这样的
// 找出姓张,且是3个字的名字,存入到一个新集合中去。
List<String> list = new ArrayList<>();
for (String name : names) {if(name.startsWith("张") && name.length() == 3){list.add(name);}
}
System.out.println(list);
Stream 流来做,代码是这样的( ps: 是不是想流水线一样,一句话就写完了)
List<String> list2 = names.stream().filter(s -> s.startsWith("张")).filter(a ->
a.length()==3).collect(Collectors.toList());
System.out.println(list2);

2.Stream流的创建

主要掌握下面四点:
1 、如何获取 List 集合的 Stream 流?
2 、如何获取 Set 集合的 Stream 流?
3 、如何获取 Map 集合的 Stream 流?
4 、如何获取数组的 Stream 流?
上代码演示:
/*** 目标:掌握Stream流的创建。*/
public class StreamTest2 {public static void main(String[] args) {
// 1、如何获取List集合的Stream流?List<String> names = new ArrayList<>();Collections.addAll(names, "张三丰","张无忌","周芷若","赵敏","张强");      Stream<String> stream = names.stream();// 2、如何获取Set集合的Stream流?Set<String> set = new HashSet<>();Collections.addAll(set, "刘德华","张曼玉","蜘蛛精","马德","德玛西亚");Stream<String> stream1 = set.stream();stream1.filter(s -> s.contains("德")).forEach(s -> System.out.println(s));// 3、如何获取Map集合的Stream流?Map<String, Double> map = new HashMap<>();map.put("古力娜扎", 172.3);map.put("迪丽热巴", 168.3);map.put("马尔扎哈", 166.3);map.put("卡尔扎巴", 168.3);//获取键Set<String> keys = map.keySet();Stream<String> ks = keys.stream();//获取值Collection<Double> values = map.values();       Stream<Double> vs = values.stream();//获取整个MapSet<Map.Entry<String, Double>> entries = map.entrySet();Stream<Map.Entry<String, Double>> kvs = entries.stream();kvs.filter(e -> e.getKey().contains("巴")).forEach(e -> System.out.println(e.getKey()+ "-->" + e.getValue()));// 4、如何获取数组的Stream流?String[] names2 = {"张翠山", "东方不败", "唐大山", "独孤求败"};Stream<String> s1 = Arrays.stream(names2);Stream<String> s2 = Stream.of(names2);}
}

3.Stream流的中间方法

中间方法指的是:调用完方法之后其结果是一个新的 Stream 流,于是可以继续调用方法,这样一来就可以支持链式 编程 (或者叫流式编程)。
直接上代码演示
/*** 目标:掌握Stream流提供的常见中间方法。*/
public class StreamTest3 {public static void main(String[] args) {List<Double> scores = new ArrayList<>();Collections.addAll(scores, 88.5, 100.0, 60.0, 99.0, 9.5, 99.6, 25.0); 
// 需求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()).forEach(s -> System.out.println(s));// 需求3:取出身高最高的前3名学生,并输出。students.stream().sorted((o1, o2) -> Double.compare(o2.getHeight(), o1.getHeight())).limit(3).forEach(System.out::println);//s -> System.out.println(s)System.out.println("-----------------------------------------------");// 需求4:取出身高倒数的2名学生,并输出。 s1 s2 s3 s4 s5 s6students.stream().sorted((o1, o2) -> Double.compare(o2.getHeight(), o1.getHeight())).skip(students.size() - 2).forEach(System.out::println);//s -> System.out.println(s)// 需求5:找出身高超过168的学生叫什么名字,要求去除重复的名字,再输出。students.stream().filter(s -> s.getHeight() > 168). map(Student::getName)     //相当于map(s->s.getName)  从身高映射到人名 .distinct().forEach(System.out::println);//s -> System.out.println(s)// distinct去重复,自定义类型的对象(希望内容一样就认为重复,重写hashCode,equals)students.stream().filter(s -> s.getHeight() > 168).distinct().forEach(System.out::println);//s -> System.out.println(s)//两个流合并为一个流Stream<String> st1 = Stream.of("张三", "李四");Stream<String> st2 = Stream.of("张三2", "李四2", "王五");Stream<String> allSt = Stream.concat(st1, st2);allSt.forEach(System.out::println);//s -> System.out.println(s)}
}

4.Stream的终结方法

因为Stream最终要被其他使用,所以要将Stream流转回为集合/数组(收集Stream流)

代码演示:

/*** 目标: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 size = students.stream().filter(s -> s.getHeight() > 168).count();System.out.println(size);// 需求2:请找出身高最高的学生对象,并输出。Student s = students.stream().max((o1, o2) -> Double.compare(o1.getHeight(),o2.getHeight())).get();//这里的get()是将最后获取的对象取出来 System.out.println(s);// 需求3:请找出身高最矮的学生对象,并输出。Student ss = students.stream().min((o1, o2) -> Double.compare(o1.getHeight(),o2.getHeight())).get();System.out.println(ss);收集Stream流演示        
// 需求4:请找出身高超过170的学生对象,并放到一个新集合中去返回。
// 流只能收集一次。//把学生对象当成一整个数据,所以用listList<Student> students1 = students.stream().filter(a -> a.getHeight() >170).collect(Collectors.toList());System.out.println(students1);//Set集合去重复(这里类要重写equals\hashCode方法)Set<Student> students2 = students.stream().filter(a -> a.getHeight() >170).collect(Collectors.toSet());System.out.println(students2);// 需求5:请找出身高超过170的学生对象,并把学生对象的名字和身高,存入到一个Map集合返回。Map<String, Double> map =students.stream().filter(a -> a.getHeight() > 170).distinct().collect(Collectors.toMap(a -> a.getName(), a ->a.getHeight()));System.out.println(map);//流收到数组里      
// Object[] arr = students.stream().filter(a -> a.getHeight() > 170).toArray();
若不想收集为object类型的数组,要收集为Student类型的数组,如下Student[] arr = students.stream().filter(a -> a.getHeight() > 170).toArray(len -> new Student[len]);System.out.println(Arrays.toString(arr));}
}
http://www.dtcms.com/wzjs/123790.html

相关文章:

  • 龙门石窟网站建设策划报告什么企业需要网络营销和网络推广
  • seo网站三种链接名片seo什么意思
  • 用js做自适应网站站长工具之家seo查询
  • 无锡网站建设专业极速信息成都网络营销搜索推广
  • 华为模板建站免费二级域名平台
  • 视频上到什么地方可以做网站链接百度竞价效果怎么样
  • 南宁建设网站培训搜索排名优化软件
  • 重庆医院网站建设营销软文代写
  • 自己做的网站如何在百度被搜索到seo做什么网站赚钱
  • 郑州网站建设最便宜长沙seo网站管理
  • 个性个人网站百度推广需要什么条件
  • 网站空间怎么登陆上海城市分站seo
  • wordpress4.7 自豪seo网站排名助手
  • 做服饰网站能打开各种网站的浏览器下载
  • 网站开发 图片存放高端大气网站建设
  • 上海网站建设企业名录seo网站优化详解
  • 广州网站建设定制设计10条重大新闻
  • 网站做滚动图片推广普通话手抄报内容大全
  • wordpress 上传大文件洛阳seo网站
  • wordpress菜单高级应用seo内容优化是什么
  • 旅行网站模板百度广告怎么投放
  • 永久免费不收费无限看看seo
  • 宜昌便宜做网站什么是软文写作
  • 南京优质网站建设方案推广优化seo
  • 网站建设公司画册大数据免费查询平台
  • 国内的外贸b2c网站体验式营销案例
  • 微网站怎么注册账号国产最好的a级suv
  • 网络工程规划与设计金昌网站seo
  • 做中学网站太原做推广营销
  • 深圳做网站做app品牌营销包括哪些内容