
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How can we implement methods of Stream API in Java 9?
Stream API provides lots of built-in functionality to help in performing operations on a collection using a stream pipeline. The API is declarative programming that makes the code precise and less error-prone. In Java 9, few useful methods have added to Stream API.
- Stream.iterate(): This method can be been used as stream version replacement for traditional for-loops.
- Stream.takeWhile(): This method can be used in a while loop that takes value while the condition is met.
- Stream.dropWhile(): This method can be used in a while loop that drops value while the condition is met.
In the below example, we can implement the static methods: iterate(), takeWhile(), and dropWhile() methods of Stream API.
Example
import java.util.Arrays; import java.util.Iterator; import java.util.stream.Collectors; import java.util.stream.Stream; public class StreamAPITest { public static void main(String args[]) { String[] sortedNames = {"Adithya", "Bharath", "Charan", "Dinesh", "Raja", "Ravi", "Zaheer"}; System.out.println("[Traditional for loop] Indexes of names starting with R = "); for(int i = 0; i < sortedNames.length; i++) { if(sortedNames[i].startsWith("R")) { System.out.println(i); } } System.out.println("[Stream.iterate] Indexes of names starting with R = "); Stream.iterate(0, i -> i < sortedNames.length, i -> ++i).filter(i -> sortedNames[i].startsWith("R")).forEach(System.out::println); String namesAtoC = Arrays.stream(sortedNames).takeWhile(n -> n.charAt(0) <= 'C') .collect(Collectors.joining(",")); String namesDtoZ = Arrays.stream(sortedNames).dropWhile(n -> n.charAt(0) <= 'C') .collect(Collectors.joining(",")); System.out.println("Names A to C = " + namesAtoC); System.out.println("Names D to Z = " + namesDtoZ); } }
Output
[Traditional for loop] Indexes of names starting with R = 4 5 [Stream.iterate] Indexes of names starting with R = 4 5 Names A to C = Adithya,Bharath,Charan Names D to Z = Dinesh,Raja,Ravi,Zaheer
- Related Articles
- How can we implement Flow API using Publisher-Subscriber in Java 9?
- How can we implement a JSON array using Streaming API in Java?
- Importance of iterate() method of Stream API in Java 9?\n
- How can we implement the SubmissionPublisher class in Java 9?
- How can we implement the Subscriber interface in Java 9?
- How to use the collect() method in Stream API in Java 9?
- How can we implement a map in JShell in Java 9?
- How to implement reactive streams using Flow API in Java 9?
- Can we use private methods in an interface in Java 9?
- What are the new features added to Stream API in Java 9?
- How to iterate List Using Java Stream API?
- How can we implement a JToggleButton in Java?
- How can we implement editable JComboBox in Java?
- How can we implement transparent JDialog in Java?
- What are the new methods added to Process API in Java 9?

Advertisements