core java 2 pdf_Core Java, Volume II--Advanced Features 11th Edition PDF 下载

主要内容:

1.1 FROM ITERATING TO STREAM OPERATIONS When you process a collection, you usually iterate over its elements and do some work with each of them. For example, suppose we want to count all long words in a book. First, let’s put them into a list: var contents = new String(Files.readAllBytes( Paths.get("alice.txt")), StandardCharsets.UTF_8); // Read file into string List words = List.of(contents.split("\\PL+")); // Split into words; nonletters are delimiters Now we are ready to iterate: int count = 0; for (String w : words) { if (w.length() > 12) count++; } With streams, the same operation looks like this: long count = words.stream()

.filter(w ­> w.length() > 12) .count(); Now you don’t have to scan the loop for evidence of filtering and counting. The method names tell you right away what the code intends to do. Moreover, where the loop prescribes the order of operations in complete detail, a stream is able to schedule the operations any way it wants, as long as the result is correct. Simply changing stream into parallelStream allows the stream library to do the filtering and counting in parallel. long count = words.parallelStream() .filter(w ­> w.length() > 12) .count(); Streams follow the “what, not how” principle. In our stream example, we describe what needs to be done: get the long words and count them. We don’t specify in which order, or in which thread, this should happen. In contrast, the loop at the beginning of this section specifies exactly how the computation should work, and thereby forgoes any chances of optimization. A stream seems superficially similar to a collection, allowing you to transform and retrieve data. But there are significant differences: 1. A stream does not store its elements. They may be stored in an underlying collection or generated on demand. 2. Stream operations don’t mutate their source. For example, the filter method does not remove elements from a stream, but it yields a new stream in which they are not present. 3. Stream operations are lazy when possible. This means they are not executed until their result is needed. For example, if you only ask for the first five long words instead of all, the filter method will stop filtering after the fifth match. As a consequence,

you can even have infinite streams! Let us have another look at the example. The stream and parallelStream methods yield a stream for the words list. The filter method returns another stream that contains only the words of length greater than twelve. The count method reduces that stream to a result. This workflow is typical when you work with streams. You set up a pipeline of operations in three stages: 1. Create a stream. 2. Specify intermediate operations for transforming the initial stream into others, possibly in multiple steps. 3. Apply a terminal operation to produce a result. This operation forces the execution of the lazy operations that precede it. Afterwards, the stream can no longer be used. In the example in Listing 1.1, the stream is created with the stream or parallelStream method. The filter method transforms it, and count is the terminal operation. In the next section, you will see how to create a stream. The subsequent three sections deal with stream transformations. They are followed by five sections on terminal op

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值