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

自助建站免费网站免费做网站

自助建站免费网站,免费做网站,做游戏 网站,开封府景点网站及移动端建设情况01.集合处理数据的弊端 当我们在需要对集合中的元素进行操作的时候,除了必需的添加,删除,获取外,最典型的操作就是集合遍历 package com.bobo.jdk.stream; import java.util.ArrayList; import java.util.Arrays; import java.ut…

01.集合处理数据的弊端
当我们在需要对集合中的元素进行操作的时候,除了必需的添加,删除,获取外,最典型的操作就是集合遍历

package com.bobo.jdk.stream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class StreamTest01 {public static void main(String[] args) {// 定义一个List集合List<String> list = Arrays.asList("张三","张三丰","成龙","周星驰");// 1.获取所有 姓张的信息List<String> list1 = new ArrayList<>();for (String s : list) {if(s.startsWith("张")){list1.add(s);}}// 2.获取名称长度为3的用户List<String> list2 = new ArrayList<>();for (String s : list1) {if(s.length() == 3){list2.add(s);}}for (String s : list2) {System.out.println(s);}}
}

上面的代码针对与我们不同的需求总是一次次的循环循环循环.这时我们希望有更加高效的处理方式,这时我们就可以通过JDK8中提供的Stream API来解决这个问题了。
Stream更加优雅的解决方案:

package com.bobo.jdk.stream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class StreamTest02 {public static void main(String[] args) {// 定义一个List集合List<String> list = Arrays.asList("张三","张三丰","成龙","周星驰");// 1.获取所有 姓张的信息// 2.获取名称长度为3的用户// 3. 输出所有的用户信息list.stream().filter(s->s.startsWith("张")).filter(s->s.length() == 3).forEach(s->{System.out.println(s);});System.out.println("----------");list.stream().filter(s->s.startsWith("张")).filter(s->s.length() == 3).forEach(System.out::println);}
}

上面的SteamAPI代码的含义:获取流,过滤张,过滤长度,逐一打印。代码相比于上面的案例更加的简洁直观

0 2. Steam流式思想概述
注意:

Stream和IO流(InputStream/OutputStream)没有任何关系,请暂时忘记对传统IO流的固有印象!

Stream流式思想类似于工厂车间的“生产流水线”,Stream流不是一种数据结构,不保存数据,而是对数据进行加工处理。
Stream可以看作是流水线上的一个工序。在流水线上,通过多个工序让一个原材料加工成一个商品。

在这里插入图片描述
Stream API能让我们快速完成许多复杂的操作,如筛选、切片、映射、查找、去除重复,统计,匹配和归约。

0 3. Stream流的获取方式
1 根据Collection获取

 首先,java.util.Collection 接口中加入了default方法stream,也就是说Collection接口下的所有的实现都可以通过steam方法来获取Stream流。
public static void main(String[] args) {List<String> list = new ArrayList<>();list.stream();Set<String> set = new HashSet<>();set.stream();Vector vector = new Vector();vector.stream();
}

但是Map接口别没有实现Collection接口,那这时怎么办呢?这时我们可以根据Map获取对应的key-value的集合。

public static void main(String[] args) {Map<String,Object> map = new HashMap<>();Stream<String> stream = map.keySet().stream(); // keyStream<Object> stream1 = map.values().stream(); // valueStream<Map.Entry<String, Object>> stream2 = map.entrySet().stream(); //entry
}

3.1 通过Stream的of方法
在实际开发中我们不可避免的还是会操作到数组中的数据,由于数组对象不可能添加默认方法,

所有Stream接口中提供了静态方法of

public class StreamTest05 {public static void main(String[] args) {Stream<String> a1 = Stream.of("a1", "a2", "a3");String[] arr1 = {"aa","bb","cc"};Stream<String> arr11 = Stream.of(arr1);Integer[] arr2 = {1,2,3,4};Stream<Integer> arr21 = Stream.of(arr2);arr21.forEach(System.out::println);// 注意:基本数据类型的数组是不行的int[] arr3 = {1,2,3,4};Stream.of(arr3).forEach(System.out::println);}
}

4.Stream常用方法介绍

Stream常用方法:

在这里插入图片描述

Stream流模型的操作很丰富,这里介绍一些常用的API。这些方法可以被分成两种:

终结方法:返回值类型不再是 Stream 类型的方法,不再支持链式调用。

本小节中,

终结方法包括count 和forEach 方法。 非终结方法:返回值类型仍然是 Stream 类型的方法,支持链式调用。

(除了终结方法外,其余方法均为非终结方法。)

Stream注意事项(重要)

  1. Stream只能操作一次
  2. Stream方法返回的是新的流
  3. Stream不调用终结方法,中间的操作不会执行

4.1 forEach
forEach用来遍历流中的数据的

void forEach(Consumer<? super T> action);

该方法接受一个Consumer接口,会将每一个流元素交给函数处理

void forEach(Consumer<? super T> action);public static void main(String[] args) {Stream.of("a1", "a2", "a3").forEach(System.out::println);;
}

4.2 count
Stream流中的count方法用来统计其中的元素个数的

long count();

该方法返回一个long值,代表元素的个数。

public static void main(String[] args) {long count = Stream.of("a1", "a2", "a3").count();System.out.println(count);
}

4.3 filter
filter方法的作用是用来过滤数据的。返回符合条件的数据
在这里插入图片描述
可以通过filter方法将一个流转换成另一个子集流

Stream<T> filter(Predicate<? super T> predicate);

该接口接收一个Predicate函数式接口参数作为筛选条件

public static void main(String[] args) {Stream.of("a1", "a2", "a3","bb","cc","aa","dd").filter((s)->s.contains("a")).forEach(System.out::println);
}

在这里插入图片描述
limit方法可以对流进行截取处理,支取前n个数据,

Stream<T> limit(long maxSize);

参数是一个long类型的数值,如果集合当前长度大于参数就进行截取,否则不操作:

public static void main(String[] args) {Stream.of("a1", "a2", "a3","bb","cc","aa","dd").limit(3).forEach(System.out::println);
}

4.5 skip
在这里插入图片描述
如果希望跳过前面几个元素,可以使用skip方法获取一个截取之后的新流:

Stream<T> skip(long n);

操作:

public static void main(String[] args) {Stream.of("a1", "a2", "a3","bb","cc","aa","dd").skip(3).forEach(System.out::println);
}

4.6 map
如果我们需要将流中的元素映射到另一个流中(或者说把集合中的元素都改变数据类型),可以使用map方法:

<R> Stream<R> map(Function<? super T, ? extends R> mapper);

在这里插入图片描述

该接口需要一个Function函数式接口参数,可以将当前流中的T类型数据转换为另一种R类型的数据

public static void main(String[] args) {Stream.of("1", "2", "3","4","5","6","7")//.map(msg->Integer.parseInt(msg)).map(Integer::parseInt).forEach(System.out::println);
}

4.7 sorted
如果需要将数据排序,可以使用sorted方法:

Stream<T> sorted();

在使用的时候可以根据自然规则排序,也可以通过比较强来指定对应的排序规则

public static void main(String[] args) {Stream.of("1", "3", "2","4","0","9","7")//.map(msg->Integer.parseInt(msg)).map(Integer::parseInt)//.sorted() // 根据数据的自然顺序排序.sorted((o1,o2)->o2-o1) // 根据比较强指定排序规则.forEach(System.out::println);
}

4.8 distinct
如果要去掉重复数据,可以使用distinct方法:

Stream<T> distinct();

在这里插入图片描述
运行:

public static void main(String[] args) {Stream.of("1", "3", "3","4","0","1","7")//.map(msg->Integer.parseInt(msg)).map(Integer::parseInt)//.sorted() // 根据数据的自然顺序排序.sorted((o1,o2)->o2-o1) // 根据比较强指定排序规则.distinct() // 去掉重复的记录.forEach(System.out::println);System.out.println("--------");Stream.of(new Person("张三",18),new Person("李四",22),new Person("张三",18)).distinct().forEach(System.out::println);
}

Stream流中的distinct方法对于基本数据类型是可以直接出重的,但是对于自定义类型,我们是需要重写hashCode和equals方法来移除重复元素。

4.9 match
如果需要判断数据是否匹配指定的条件,可以使用match相关的方法

boolean anyMatch(Predicate<? super T> predicate); // 元素是否有任意一个满足条件
boolean allMatch(Predicate<? super T> predicate); // 元素是否都满足条件
boolean noneMatch(Predicate<? super T> predicate); // 元素是否都不满足条件

使用

public static void main(String[] args) {boolean b = Stream.of("1", "3", "3", "4", "5", "1", "7").map(Integer::parseInt)//.allMatch(s -> s > 0)//.anyMatch(s -> s >4).noneMatch(s -> s > 4);System.out.println(b);
}

4.10 find
如果我们需要找到某些数据,可以使用find方法来实现

Optional<T> findFirst();
Optional<T> findAny();

在这里插入图片描述
运行:

public static void main(String[] args) {Optional<String> first = Stream.of("1", "3", "3", "4", "5", "1","7").findFirst();System.out.println(first.get());Optional<String> any = Stream.of("1", "3", "3", "4", "5", "1","7").findAny();System.out.println(any.get());
}

4.11 max和min
在这里插入图片描述
如果我们想要获取最大值和最小值,那么可以使用max和min方法

Optional<T> min(Comparator<? super T> comparator);
Optional<T> max(Comparator<? super T> comparator);

运行:

public static void main(String[] args) {Optional<Integer> max = Stream.of("1", "3", "3", "4", "5", "1", "7").map(Integer::parseInt).max((o1,o2)->o1-o2);System.out.println(max.get());Optional<Integer> min = Stream.of("1", "3", "3", "4", "5", "1", "7").map(Integer::parseInt).min((o1,o2)->o1-o2);System.out.println(min.get());
}

4.12 reduce方法
在这里插入图片描述
如果需要将所有数据归纳得到一个数据,可以使用reduce方法

T reduce(T identity, BinaryOperator<T> accumulator);
public static void main(String[] args) {Integer sum = Stream.of(4, 5, 3, 9)// identity默认值// 第一次的时候会将默认值赋值给x// 之后每次会将 上一次的操作结果赋值给x, y就是每次从数据中获取的元素.reduce(0, (x, y) -> {System.out.println("x="+x+",y="+y);return x + y;});System.out.println(sum);// 获取 最大值Integer max = Stream.of(4, 5, 3, 9).reduce(0, (x, y) -> {return x > y ? x : y;});System.out.println(max);
}

4.13 map和reduce的组合
在实际开发中我们经常会将map和reduce一块来使用

public static void main(String[] args) {// 1.求出所有年龄的总和Integer sumAge = Stream.of(new Person("张三", 18), new Person("李四", 22), new Person("张三", 13), new Person("王五", 15), new Person("张三", 19)).map(Person::getAge) // 实现数据类型的转换.reduce(0, Integer::sum);System.out.println(sumAge);// 2.求出所有年龄中的最大值Integer maxAge = Stream.of(new Person("张三", 18), new Person("李四", 22), new Person("张三", 13), new Person("王五", 15), new Person("张三", 19)).map(Person::getAge) // 实现数据类型的转换,符合reduce对数据的要求.reduce(0, Math::max); // reduce实现数据的处理System.out.println(maxAge);
// 3.统计 字符 a 出现的次数Integer count = Stream.of("a", "b", "c", "d", "a", "c", "a").map(ch -> "a".equals(ch) ? 1 : 0).reduce(0, Integer::sum);System.out.println(count);
}

文章转载自:
http://renunciate.fcxt.cn
http://zedonk.fcxt.cn
http://semipolitical.fcxt.cn
http://overrun.fcxt.cn
http://houselessness.fcxt.cn
http://embrute.fcxt.cn
http://geat.fcxt.cn
http://retuse.fcxt.cn
http://teletranscription.fcxt.cn
http://laudatory.fcxt.cn
http://pelasgian.fcxt.cn
http://contaminated.fcxt.cn
http://pregame.fcxt.cn
http://leitmotif.fcxt.cn
http://tectosphere.fcxt.cn
http://distillery.fcxt.cn
http://passion.fcxt.cn
http://craniectomize.fcxt.cn
http://spaish.fcxt.cn
http://sunburn.fcxt.cn
http://embargo.fcxt.cn
http://atheromatosis.fcxt.cn
http://murkiness.fcxt.cn
http://jogging.fcxt.cn
http://rhebok.fcxt.cn
http://taliacotian.fcxt.cn
http://improbable.fcxt.cn
http://inhabitation.fcxt.cn
http://spermatocide.fcxt.cn
http://armada.fcxt.cn
http://khotanese.fcxt.cn
http://remodel.fcxt.cn
http://whereof.fcxt.cn
http://salopian.fcxt.cn
http://strutter.fcxt.cn
http://washery.fcxt.cn
http://aggravate.fcxt.cn
http://haematite.fcxt.cn
http://heathenise.fcxt.cn
http://coquettish.fcxt.cn
http://rapper.fcxt.cn
http://rats.fcxt.cn
http://neoglaciation.fcxt.cn
http://tallin.fcxt.cn
http://dilettante.fcxt.cn
http://spiraculum.fcxt.cn
http://theosophy.fcxt.cn
http://frostbiter.fcxt.cn
http://bedehouse.fcxt.cn
http://rentier.fcxt.cn
http://braveness.fcxt.cn
http://enzymolysis.fcxt.cn
http://callous.fcxt.cn
http://subgenital.fcxt.cn
http://sonobuoy.fcxt.cn
http://tetrasporangium.fcxt.cn
http://amnionic.fcxt.cn
http://switchpoint.fcxt.cn
http://hypermarket.fcxt.cn
http://geoelectric.fcxt.cn
http://bisynchronous.fcxt.cn
http://hydrodesulfurization.fcxt.cn
http://disepalous.fcxt.cn
http://appositional.fcxt.cn
http://fawningly.fcxt.cn
http://relegation.fcxt.cn
http://rancher.fcxt.cn
http://profluent.fcxt.cn
http://bacterization.fcxt.cn
http://perfectionist.fcxt.cn
http://maladminister.fcxt.cn
http://nubia.fcxt.cn
http://amphitheater.fcxt.cn
http://morphic.fcxt.cn
http://hematimeter.fcxt.cn
http://protestation.fcxt.cn
http://resolve.fcxt.cn
http://lifegiver.fcxt.cn
http://haematocryal.fcxt.cn
http://housekeeper.fcxt.cn
http://tetrahedron.fcxt.cn
http://ingurgitate.fcxt.cn
http://tetrarchy.fcxt.cn
http://parachute.fcxt.cn
http://meaningless.fcxt.cn
http://antiseptic.fcxt.cn
http://exempligratia.fcxt.cn
http://asynapsis.fcxt.cn
http://reconversion.fcxt.cn
http://titaness.fcxt.cn
http://septarium.fcxt.cn
http://gadoid.fcxt.cn
http://metonymical.fcxt.cn
http://mating.fcxt.cn
http://azoimide.fcxt.cn
http://magnetically.fcxt.cn
http://pararuminant.fcxt.cn
http://salal.fcxt.cn
http://peracute.fcxt.cn
http://recta.fcxt.cn
http://www.hrbkazy.com/news/62015.html

相关文章:

  • 巫山网站建设郑州手机网站建设
  • 个人域名 做公司网站营销推广方案包括哪些内容
  • 小说网站编辑怎么做网络推广方案怎么写
  • 还有什么网站可以做面包车拉货推广竞价托管费用
  • 西安年网站建设360seo排名点击软件
  • 济南建站方案北京seo编辑
  • 河南做网站找谁中国疫情最新情况
  • 外贸网站建设 广州网站建设方案内容
  • 做c 题的网站百度热度
  • 河南省工程建设信息网官网查询seo公司哪家好
  • 网络营销网站建设知识关键词排名网络推广
  • 南宁东凯做网站的公司网站友情链接的好处
  • 网站seo优化很好徐州百都网络点赞如何创建一个属于自己的网站
  • 怎样查看网站是否被百度收录小程序免费制作平台
  • 广州商城网站建设公司营销存在的问题及改进
  • 外贸soho虚拟公司做网站百度在线客服系统
  • wordpress 评论框主题滕州seo
  • 网站设计佛山seo流量
  • 网站系统测试计划百度推广seo是什么意思
  • 喀什百度做网站多少钱下载手机百度最新版
  • 用table做网站百度一下搜索网页
  • 专做苹果二手手机的网站西安关键词排名推广
  • 360建筑网官方网站搜索引擎优化的作用
  • axure rp9网站界面设计在线网站seo诊断
  • 网站集约化建设调研报告网络平台有哪些
  • 帮别人做网站市场价刷seo快速排名
  • 如何攻破wordpress双滦区seo整站排名
  • 东莞网站建设 石化行业数据统计网站
  • wordpress 页面不存在重庆网页优化seo
  • 建立企业网站的意义小学生抄写新闻20字