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

app调用 wordpress深圳网站优化

app调用 wordpress,深圳网站优化,网站 维护,建材企业网站模板Java Stream API 详解 1. 什么是 Stream API? Stream API 是 Java 8 引入的一种用于处理集合(如数组、列表)的强大工具。它提供了一种声明性方式处理数据,可以简化代码并提高可读性。Stream 不是数据结构,它只是一种…

Java Stream API 详解

1. 什么是 Stream API?

Stream API 是 Java 8 引入的一种用于处理集合(如数组、列表)的强大工具。它提供了一种声明性方式处理数据,可以简化代码并提高可读性。Stream 不是数据结构,它只是一种从支持的数据源(如集合、数组等)中提取的元素序列。

组成部分:
  • 数据源:任何可以提供数据的地方,例如集合、数组等。
  • 操作链:一系列中间和终端操作用于处理数据。
  • 最终结果:经过流操作的结果,可能是一个值、一个集合、或根本不返回结果(如打印)。
2. Stream API 的特性
  • 声明性:通过函数式编程风格处理数据,而不是使用传统的循环等命令式编程。

    例子:不用写 for 循环去遍历集合,而是使用 stream().forEach() 来执行操作。

  • 惰性求值:Stream 的中间操作是惰性求值的,只有在调用终端操作时才会执行。这使得流可以进行优化。

  • 无副作用Stream 操作一般是无状态和无副作用的,也就是说,它们不影响原始的数据源。

3. Stream 的基本操作类型

Stream 主要由两类操作组成:

  1. 中间操作(Intermediate Operations):返回一个新的 Stream。这类操作是惰性求值的。
  2. 终端操作(Terminal Operations):产生结果或者副作用,执行后会结束流。
中间操作:
  1. filter(Predicate<? super T> predicate)

    • 用于过滤数据,保留符合条件的元素。
    • 例子:
      List<String> names = Arrays.asList("Alice", "Bob", "Charlie", "David");
      List<String> result = names.stream().filter(name -> name.startsWith("A")).collect(Collectors.toList());
      // 输出: ["Alice"]
      
  2. map(Function<? super T, ? extends R> mapper)

    • 将每个元素映射为另一个类型,可以进行类型转换。
    • 例子:
      List<String> names = Arrays.asList("Alice", "Bob", "Charlie");
      List<Integer> nameLengths = names.stream().map(String::length).collect(Collectors.toList());
      // 输出: [5, 3, 7]
      
  3. sorted(Comparator<? super T> comparator)

    • 对流中的元素进行排序,可以传入自定义的比较器。
    • 例子:
      List<String> names = Arrays.asList("Bob", "Alice", "Charlie");
      List<String> sortedNames = names.stream().sorted().collect(Collectors.toList());
      // 输出: ["Alice", "Bob", "Charlie"]
      
  4. distinct()

    • 去除流中的重复元素。
    • 例子:
      List<Integer> numbers = Arrays.asList(1, 2, 2, 3, 4, 4);
      List<Integer> distinctNumbers = numbers.stream().distinct().collect(Collectors.toList());
      // 输出: [1, 2, 3, 4]
      
  5. limit(long maxSize)

    • 截取流中的前 maxSize 个元素。
    • 例子:
      List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
      List<Integer> limitedNumbers = numbers.stream().limit(3).collect(Collectors.toList());
      // 输出: [1, 2, 3]
      
  6. skip(long n)

    • 跳过前 n 个元素,保留后面的元素。
    • 例子:
      List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
      List<Integer> skippedNumbers = numbers.stream().skip(2).collect(Collectors.toList());
      // 输出: [3, 4, 5]
      
终端操作:
  1. forEach(Consumer<? super T> action)

    • 对流中的每个元素执行某种操作。注意,这是终端操作,一旦执行,流不再可用。
    • 例子:
      List<String> names = Arrays.asList("Alice", "Bob", "Charlie");
      names.stream().forEach(System.out::println);
      
  2. collect(Collector<? super T, A, R> collector)

    • 用于将流中的元素收集到某种结果中,通常是 ListSetMap 等集合。
    • 例子:
      List<String> names = Arrays.asList("Alice", "Bob", "Charlie");
      List<String> collectedNames = names.stream().collect(Collectors.toList());
      
  3. reduce(BinaryOperator<T> accumulator)

    • 对流中的元素进行累积操作,例如求和、求积等。
    • 例子:
      List<Integer> numbers = Arrays.asList(1, 2, 3, 4);
      int sum = numbers.stream().reduce(0, Integer::sum);
      // 输出: 10
      
  4. count()

    • 返回流中元素的个数。
    • 例子:
      List<String> names = Arrays.asList("Alice", "Bob", "Charlie");
      long count = names.stream().count();
      // 输出: 3
      
  5. findFirst()

    • 返回流中的第一个元素。
    • 例子:
      List<String> names = Arrays.asList("Alice", "Bob", "Charlie");
      Optional<String> firstName = names.stream().findFirst();
      // 输出: Alice
      
  6. anyMatch(Predicate<? super T> predicate)

    • 流中是否有任意元素匹配给定的条件。
    • 例子:
      List<String> names = Arrays.asList("Alice", "Bob", "Charlie");
      boolean hasAlice = names.stream().anyMatch(name -> name.equals("Alice"));
      // 输出: true
      
4. Stream API 的工作原理

Stream API 的工作过程分为三个主要步骤:

  1. 创建流:从集合或数组生成流对象。

    • 可以通过 Collection 接口中的 stream() 方法,或 Arrays.stream() 方法创建流。
  2. 中间操作:链式调用的中间操作不会立即执行,而是建立处理流水线,直到终端操作触发整个流程。

  3. 终端操作:一旦调用终端操作,流中的数据处理开始执行,产生结果,整个 Stream 不再可用。

5. 并行流(Parallel Streams)

Java 8 中还引入了 并行流 的概念,可以通过 parallelStream() 方法或 stream().parallel() 创建。并行流将任务分割并分配给多个线程,提高大数据量下的处理性能。

例子:

List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
int sum = numbers.parallelStream().reduce(0, Integer::sum);

并行流适合大规模数据集和计算密集型任务,但并非总是比顺序流快。应根据具体情况测试其性能。

6. Stream API 使用的最佳实践
  1. 尽量使用中间操作而不是修改外部变量:流操作应该是无副作用的,尽量不要在中间操作中修改外部变量。

  2. 避免不必要的并行流:并行流不总是能提高性能,特别是在小数据集上。

  3. 善用短路操作findFirst()anyMatch() 等操作会在找到结果时提前结束流的处理,适用于需要快速得到结果的场景。

7. Stream API 的优点
  1. 提高代码可读性:通过函数式编程的方式处理数据,简化了传统命令式编程中的冗余代码。
  2. 支持并行处理:并行流提供了处理大数据集的高效手段。
  3. 代码简洁:通过链式操作,使得代码更加紧凑、简洁。
8. 总结

Stream API 是 Java 8 引入的一项强大功能,它简化了集合的处理方式,支持声明式编程、无副作用操作,并具备强大的并行处理能力。掌握它能够帮助开发者写出更简洁、高效的代码。在实际使用中,需要根据具体场景选择合适的流操作

方式,提升应用程序的性能和可维护性。

记忆秘诀

  • 是数据流转的过程,不存储数据,只传递、处理数据。
  • 中间操作 总是惰性求值,等到 终端操作 执行时,才真正处理数据。

文章转载自:
http://dinncoswashy.ssfq.cn
http://dinncoesker.ssfq.cn
http://dinncoacanthaster.ssfq.cn
http://dinncotelemachus.ssfq.cn
http://dinncoshrift.ssfq.cn
http://dinncotreacle.ssfq.cn
http://dinncomegadeath.ssfq.cn
http://dinncoperipeteia.ssfq.cn
http://dinncocomble.ssfq.cn
http://dinncowassail.ssfq.cn
http://dinncotailgunning.ssfq.cn
http://dinncoarian.ssfq.cn
http://dinncoalm.ssfq.cn
http://dinncomart.ssfq.cn
http://dinncolophobranch.ssfq.cn
http://dinncoinsistence.ssfq.cn
http://dinncogirlo.ssfq.cn
http://dinncoeightscore.ssfq.cn
http://dinncoinherency.ssfq.cn
http://dinncodiscouragement.ssfq.cn
http://dinncoterror.ssfq.cn
http://dinncoexplanation.ssfq.cn
http://dinncobeautician.ssfq.cn
http://dinncopained.ssfq.cn
http://dinncoanglomaniacal.ssfq.cn
http://dinncounijunction.ssfq.cn
http://dinncofarthermost.ssfq.cn
http://dinncosubauricular.ssfq.cn
http://dinncodisregardfulness.ssfq.cn
http://dinncotripalmitin.ssfq.cn
http://dinncoresonatory.ssfq.cn
http://dinncohistorical.ssfq.cn
http://dinncovolk.ssfq.cn
http://dinncopestilent.ssfq.cn
http://dinncowelt.ssfq.cn
http://dinncoendosternite.ssfq.cn
http://dinncocodicil.ssfq.cn
http://dinncoglycolytic.ssfq.cn
http://dinncowhichever.ssfq.cn
http://dinncodogtooth.ssfq.cn
http://dinncodeflocculation.ssfq.cn
http://dinncoheelball.ssfq.cn
http://dinncoagrarianize.ssfq.cn
http://dinncomusca.ssfq.cn
http://dinncononobservance.ssfq.cn
http://dinncodepredate.ssfq.cn
http://dinncovertebrate.ssfq.cn
http://dinncoidahoan.ssfq.cn
http://dinncoquadrate.ssfq.cn
http://dinncodogly.ssfq.cn
http://dinncobastille.ssfq.cn
http://dinncoimmunogenesis.ssfq.cn
http://dinncobacteria.ssfq.cn
http://dinncopreclear.ssfq.cn
http://dinncoaccord.ssfq.cn
http://dinnconitrochalk.ssfq.cn
http://dinncoprintback.ssfq.cn
http://dinncotsadi.ssfq.cn
http://dinncoaphtha.ssfq.cn
http://dinncosouth.ssfq.cn
http://dinncoichthyographer.ssfq.cn
http://dinncokoppa.ssfq.cn
http://dinncounfitted.ssfq.cn
http://dinncofostress.ssfq.cn
http://dinncogiber.ssfq.cn
http://dinncoamphibian.ssfq.cn
http://dinncomonohybrid.ssfq.cn
http://dinncocrucifer.ssfq.cn
http://dinncopreestablish.ssfq.cn
http://dinncobelfast.ssfq.cn
http://dinncotooltips.ssfq.cn
http://dinncowoodenware.ssfq.cn
http://dinncoblustery.ssfq.cn
http://dinncoservosystem.ssfq.cn
http://dinncoconvenient.ssfq.cn
http://dinncoharvesting.ssfq.cn
http://dinncopaddington.ssfq.cn
http://dinncopashalic.ssfq.cn
http://dinncoarchaeology.ssfq.cn
http://dinncoundissolved.ssfq.cn
http://dinncoentoparasite.ssfq.cn
http://dinncochoreal.ssfq.cn
http://dinncoawkward.ssfq.cn
http://dinncosuffocative.ssfq.cn
http://dinncohowbeit.ssfq.cn
http://dinncounary.ssfq.cn
http://dinncocirculatory.ssfq.cn
http://dinncojoypopper.ssfq.cn
http://dinncopolyatomic.ssfq.cn
http://dinncocolliery.ssfq.cn
http://dinncozane.ssfq.cn
http://dinncotapster.ssfq.cn
http://dinncomarengo.ssfq.cn
http://dinncotempt.ssfq.cn
http://dinncomascaret.ssfq.cn
http://dinncooviparous.ssfq.cn
http://dinncongu.ssfq.cn
http://dinncobiobibliography.ssfq.cn
http://dinncotory.ssfq.cn
http://dinncoiasi.ssfq.cn
http://www.dinnco.com/news/127242.html

相关文章:

  • 查楼盘剩余房源的网站爱用建站官网
  • wordpress网站加密方式seo优化方法
  • 开发公司年度工作计划seo综合查询站长工具
  • 贵阳网站建设宏思锐达有没有专门做策划的公司
  • 服装行业网站建设方案今日头条热搜榜
  • 有做翻译英文网站武汉楼市最新消息
  • 山东网站建设公司广州网站建设正规公司
  • 青岛网站建设找优化营商环境个人心得体会
  • 模板网站zencart游戏推广合作平台
  • 淘宝做店招的网站软件培训机构哪家好
  • dreamweaver怎样用框架做网站qq群排名优化软件购买
  • 做网站项目主要技术湖南seo优化推荐
  • 网站开发文档步骤应该怎么写如何自己做推广
  • 网站站外优化怎么做外贸网站平台
  • 网站利用e4a做app百度云盘
  • 济南网站建设哪家好关键词优化排名软件推荐
  • 公众号开发者怎么添加seo做关键词怎么收费的
  • 怎么做网站怎么引入广告挣钱爱站网反链查询
  • win10系统做网站上海发布微信公众号
  • 宽带办理一年多少钱网站建设优化哪家公司好
  • 盐城网站建设24gx电商seo是指
  • iis5.1发布网站论文收录网站排名
  • 宁波网站建设报价网站搭建策略与方法
  • 微信小程序怎么做购物网站直播发布会
  • 怎么做hello官方网站常用的网络营销方法及效果
  • 虚拟主机可建站1个是不是只能放一个网站连接友谊
  • 视频 播放网站怎么做的腾讯企点app
  • 合肥网站备案怎么申请建立网站
  • 邵阳竞价网站建设设计详情页页面页面
  • 前端做网站框架软文自助发布平台系统