Table of Contents
iterate()
Example
输出
ofNullable()
示例
takeWhile()
dropWhile()
Home Java javaTutorial What new features have been added to the Stream API in Java 9?

What new features have been added to the Stream API in Java 9?

Aug 30, 2023 pm 07:49 PM

Java 9中Stream API添加了哪些新功能?

In Java 9, Oracle Corporation has added four useful new methods to Stream API. Those methods are iterate(), ofNullable(), takeWhile() and dropWhile().

iterate()

The iterate() can be used as stream version replacement of traditional for-loops. This method has improved by adding another parameter, the Predicate interface that allows us to stop these endless numbers based on conditions defined with the Predicate interface.

Example

import java.util.stream.Stream;
public class StreamIterateMethodTest {
   public static void main(String[] args) {
      Stream.<strong>iterate</strong>(1, i -> i < 5, i -> i + 1).<strong>forEach</strong>(System.out::println); <strong>// iterate()</strong>
   }
}
Copy after login

输出

<strong>1
2
3
4</strong>
Copy after login
Copy after login

ofNullable()

ofNullable()方法在元素不为null时返回一个stream对象。否则,返回一个空的stream

示例

import java.util.stream.Stream;
public class StreamOfNullableMethodTest {
   public static void main(String[] args) {
      String str = "TutorialsPoint";
      Stream.<strong>ofNullable</strong>(str).<strong>forEach</strong>(System.out::println);   <strong> // ofNullable() method</strong>
   }
}
Copy after login

输出

<strong>TutorialsPoint
</strong>
Copy after login

takeWhile()

The parameter passed to a takeWhile() method is a Predicate interface. This method takes an element of stream object from left to right until the condition of the Predicate object is no longer fulfilled.

Example

import java.util.stream.Stream;
public class StreamTakeWhileMethodTest {
   public static void main(String[] args) {
      Stream.of(1, 2, 3, 4, 5)
         .<strong>takeWhile</strong>(i -> i < 5)         <strong>// takeWhile() method</strong>
         .<strong>forEach</strong>(System.out::println);
   }
}
Copy after login

输出

<strong>1
2
3
4</strong>
Copy after login
Copy after login

dropWhile()

传递给 dropWhile() 方法的参数也是一个 Predicate 接口。它与 takeWhile() 方法相反。该方法从左到右依次传递流对象中的每个元素,并忽略满足条件的所有元素。一旦条件不再满足,它将返回所有剩余的元素。

示例

import java.util.stream.Stream;
public class StreamDropWhileMethodTest {
   public static void main(String[] args) {
      Stream.of(3, 2, 1, 4, 6, 7, 8, 9, 10)
            .<strong>dropWhile</strong>(i -> i < 5)     <strong>// dropWhile() method</strong>
            .<strong>forEach</strong>(System.out::println);
   }
}
Copy after login

输出

<strong>6
7
8
9
10</strong>
Copy after login

The above is the detailed content of What new features have been added to the Stream API in Java 9?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)