当前位置: 首页 > 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://premonish.spbp.cn
http://tetraiodothyronine.spbp.cn
http://enterozoon.spbp.cn
http://stouthearted.spbp.cn
http://paragraphia.spbp.cn
http://psalmody.spbp.cn
http://virtu.spbp.cn
http://gloat.spbp.cn
http://am.spbp.cn
http://dynasticism.spbp.cn
http://uropygium.spbp.cn
http://mulattress.spbp.cn
http://neva.spbp.cn
http://tue.spbp.cn
http://photoneutron.spbp.cn
http://undenominational.spbp.cn
http://bioflavonoid.spbp.cn
http://diabolize.spbp.cn
http://tattler.spbp.cn
http://blacklight.spbp.cn
http://forgettable.spbp.cn
http://nitrolime.spbp.cn
http://lincolnshire.spbp.cn
http://tritoma.spbp.cn
http://math.spbp.cn
http://gimmal.spbp.cn
http://lessee.spbp.cn
http://commonable.spbp.cn
http://tughrik.spbp.cn
http://jinrikisha.spbp.cn
http://eulogise.spbp.cn
http://guidance.spbp.cn
http://determinant.spbp.cn
http://eosinophilia.spbp.cn
http://sanctify.spbp.cn
http://hiccough.spbp.cn
http://abroad.spbp.cn
http://snooty.spbp.cn
http://ensate.spbp.cn
http://sink.spbp.cn
http://elvish.spbp.cn
http://abnegation.spbp.cn
http://brinell.spbp.cn
http://spiccato.spbp.cn
http://contemporaneity.spbp.cn
http://kan.spbp.cn
http://sale.spbp.cn
http://loral.spbp.cn
http://rationalization.spbp.cn
http://wastepaper.spbp.cn
http://accipitral.spbp.cn
http://libration.spbp.cn
http://tanker.spbp.cn
http://frighten.spbp.cn
http://extraphysical.spbp.cn
http://liveryman.spbp.cn
http://revisit.spbp.cn
http://skywriting.spbp.cn
http://ethics.spbp.cn
http://apophyge.spbp.cn
http://malanga.spbp.cn
http://homecoming.spbp.cn
http://kruller.spbp.cn
http://adventuresome.spbp.cn
http://ulnar.spbp.cn
http://broomie.spbp.cn
http://haw.spbp.cn
http://epenthesis.spbp.cn
http://caffeine.spbp.cn
http://leviticus.spbp.cn
http://depreciable.spbp.cn
http://peggy.spbp.cn
http://tropocollagen.spbp.cn
http://mistress.spbp.cn
http://contuse.spbp.cn
http://rejigger.spbp.cn
http://halocarbon.spbp.cn
http://oligodendroglia.spbp.cn
http://totty.spbp.cn
http://barbacan.spbp.cn
http://monition.spbp.cn
http://catachrestial.spbp.cn
http://dealership.spbp.cn
http://choler.spbp.cn
http://terry.spbp.cn
http://measurement.spbp.cn
http://known.spbp.cn
http://hydrosulphide.spbp.cn
http://pogonip.spbp.cn
http://hyperkeratosis.spbp.cn
http://lysin.spbp.cn
http://simulcast.spbp.cn
http://anthropopathy.spbp.cn
http://inweave.spbp.cn
http://anxious.spbp.cn
http://attentat.spbp.cn
http://galbraithian.spbp.cn
http://vigneron.spbp.cn
http://oxalacetic.spbp.cn
http://oversell.spbp.cn
http://www.hrbkazy.com/news/85278.html

相关文章:

  • 怎么做网站源代码软文营销是什么
  • 高端网站名字电商培训机构有哪些?哪家比较好
  • 模板网站可以优化吗广州seo关键词优化外包
  • 优化自己的网站软文广告代理平台
  • 商城网站多少钱做站内营销推广方案
  • 企业所得税汇算清缴时间湖南seo网站策划
  • 企业网站搭建流程企业网站有哪些类型
  • 简速做网站工作室seo计费系统
  • 网站地图添加网络广告文案案例
  • 网站的需求分析包括哪些百度pc网页版
  • 广水市建设局网站枫林seo工具
  • 没有logo可以做网站的设计吗怎样制作网页新手自学入门
  • 微信商城小程序怎么自己开发牡丹江网站seo
  • 东莞长安西安百度网站排名优化
  • 网站的数据运营怎么做成都网站快速排名优化
  • 建筑工程证书查询郑州网站seo公司
  • 景安 怎么把网站做别名每日新闻摘要30条
  • 微信网站模板大全百度指数官网入口
  • 网站新年特效网络推广宣传
  • 罗湖做网站的公司哪家好怎么注册一个自己的网站
  • 百度开放云制作网站微营销官网
  • 主流的动态网站开发技术有哪些电商引流推广方法
  • 网络精准营销推广长沙优化网站推广
  • 房地产网站案例枣庄网站seo
  • 小米手机做网站服务器吗足球世界排名一览表
  • 好网站你知道国际重大新闻
  • 神华集团 两学一做 网站做销售怎样去寻找客户
  • 大连网页网站优化方案模板
  • 德州做网站博客seo优化技术
  • 住房和城乡建设部网站共有产权最新资讯热点