The BaseStream interface. Examples of using interface methods
Before studying this topic, it is recommended that you familiarize yourself with the following topic:
Contents
- 1. List of methods of the BaseStream interface
- 2. Method close(). Closing the data stream. Example
- 3. Method isParallel(). Determining if the data stream is parallel. Example
- 4. Method iterator(). Get the iterator for data stream. Example
- 5. Method onClose(). Get the stream with the specified close event handler. Example
- 6. Method parallel(). Return the parallel data stream. Example
- 7. Method sequential(). Return a sequential data stream. Example
- 8. Method unordered(). Return the unordered data stream. Example
- Related topics
Search other websites:
1. List of methods of the BaseStream interface
Below is a list of the main methods of the BaseStream interface and their purpose:
- close() – close data stream;
- isParallel() – determine if a stream is parallel;
- iterator() – get an iterator for a stream;
- onClose() – set the stream close event handler;
- parallel() – return parallel data stream;
- sequential() – return sequential data stream;
- spliterator() – get an iterator-delimiter;
- unordered() – return an unordered data stream.
⇑
2. Method close(). Closing the data stream. Example
To close the data stream, the close() method is used, which has the following general form
void close();
Be sure to close only those data streams that are associated with files. Closing other data streams is optional.
The example demonstrates how to close the System.in data stream that follows from keyboard input. The Scanner class is used to enter data. First, an array of integers is formed, then this array is displayed. At the end of the program, the data stream is closed by the close() method.
// Examples of opening/closing a data stream import java.util.stream.*; import java.util.*; public class TrainStreamAPI { public static void main(String[] args) { // Method close() // Get data stream from keyboard Scanner scanner = new Scanner(System.in); // Read the array of numbers. End of input - number 0 Integer num; ArrayList<Integer> AL = new ArrayList(); while (true) { System.out.print("num = "); // Get number from keyboard num = scanner.nextInt(); // Check if 0 is entered if (num==0) break; // Add number to array AL.add(num); } // Display array to screen System.out.println("End of input."); System.out.println("AL = " + AL); // Close the stream scanner.close(); } }
Program execution result
num = 15 num = 18 num = -9 num = 3 num = 0 End of input. AL = [15, 18, -9, 3]
⇑
3. Method isParallel(). Determining if the data stream is parallel. Example
The isParallel() method is designed to determine if the calling data stream is parallel. The method has the following general form:
boolean isParallel()
The method returns true if the data stream is parallel. If the data stream is sequential, then the method returns false.
The text of the program demonstrating the use of the isParallel() method is as follows.
import java.util.ArrayList; import java.util.stream.*; public class StreamAPI { public static void main(String[] args) { // The IsParallel() method - determine if data stream is parallel // 1. Create an array of numbers that will be a data stream ArrayList<Integer> AL = new ArrayList<Integer>(); for (int i=0; i<10; i++) { AL.add((int)(Math.random()*100)); // numbers from 0 to 99 } // 2. Display the array System.out.println(AL); // 3. Get serial data stream from AL array Stream<Integer> stream = AL.stream(); // 4. Determine if stream is parallel if (stream.isParallel()) System.out.println("stream is parallel"); else System.out.println("stream is not parallel"); // 5. Get parallel data stream from AL array Stream<Integer> parallelStream = AL.parallelStream(); // 6. Determine if parallelStream is parallel if (parallelStream.isParallel()) System.out.println("parallelStream is parallel"); else System.out.println("parallelStream is not parallel"); } }
Program execution result
[48, 64, 30, 40, 19, 17, 27, 75, 48, 52] stream is not parallel parallelStream is parallel
⇑
4. Method iterator(). Get the iterator for data stream. Example
The iterator() method is for getting an iterator. Using iterator, you can view and process the elements of a stream. The general form of the method is as follows
Iterator<T> iterator()
here
- Iterator<T> – a type that characterizes an iterator that processes elements of some generic type T.
The method is the final operation.
The text of the program, demonstrating the iterator() method, are shown below.
import java.util.*; import java.util.stream.*; import java.util.function.*; public class StreamAPI { public static void main(String[] args) { // 1. Create a set of numbers ArrayList<Double> AL = new ArrayList<Double>(); AL.add(1.5); AL.add(2.8); AL.add(-2.3); AL.add(3.4); AL.add(1.1); // 2. Create a stream of data from a set of numbers AL Stream<Double> stream = AL.stream(); // 3. Get an iterator for stream Iterator<Double> it = stream.iterator(); // 4. Display elements of a stream using an iterator System.out.print("stream = "); while (it.hasNext()) { System.out.print(it.next()+" "); } System.out.println(); // 5. Remove negative stream elements, create resulting array ArrayList<Double> AL2 = new ArrayList<Double>(); double t; // Form a new stream and get an iterator on it stream = AL.stream(); it = stream.iterator(); // Loop through stream while (it.hasNext()) { // Get a number from stream t = it.next(); // If the number is positive, then add it to the array AL2 if (t>=0.0) AL2.add(t); } // 6. Display array AL2 System.out.println("AL2 = " + AL2); } }
Program execution result
stream = 1.5 2.8 -2.3 3.4 1.1 AL2 = [1.5, 2.8, 3.4, 1.1]
⇑
5. Method onClose(). Get the stream with the specified close event handler. Example
The onClose() method allows you to set up a thread close event handler – a special method that will be called when the thread is closed and perform some finishing actions.
The onClose() method has the following general form
S onClose(Runnable closeHandler)
here
- closeHandler – the class that contains the run() method of the Runnable interface. The run() method contains the code that needs to be executed when the calling thread is closed;
- S – a stream with a closeHandler installed;
After the closeHandler has been set, it is called when the calling thread executes the close() method.
Example. The example declares the MyCloseStream class, which acts as a handler called when the stream is closed.
The MyCloseStream class implements the Runnable interface. This means that the class must implement a single run() method of the Runnable interface. The run() method contains the code that should be executed when the stream is closed. In the program, for demonstration purposes, a simple informational message is displayed in the run() method.
The demo program is as follows.
import java.util.*; import java.util.stream.*; // A class of stream closing event handler. class MyCloseStream implements Runnable { // Method that closes the stream public void run() { // Here code, which is called when the stream is closed // Display some informational message System.out.println("Close the stream."); // ... } } public class StreamAPI { public static void main(String[] args) { // Demonstration of calling the run() method of the MyCloseStream class // 1. Create a set of numbers ArrayList<Integer> AL = new ArrayList<Integer>(); AL.add(25); AL.add(31); AL.add(22); AL.add(18); AL.add(11); // 2. Create a data stream Stream<Integer> stream = AL.stream(); // 3. Specify the handler close the stream MyCloseStream handler = new MyCloseStream(); // 4. Call the onClose() method of stream // and pass it a handler - an instance of handler. stream.onClose(handler); // 5. Close stream: // the stream closing handler is called stream.close(); // Close the stream } }
Program execution result
Close the stream.
⇑
6. Method parallel(). Return the parallel data stream. Example
The parallel() method allows you to get a parallel stream from the calling stream. If the calling stream is already parallel, then this stream is returned. The parallel() method is an intermediate operation and has the following general form:
S parallel()
here
- S – the resulting parallel stream.
The text of the program, demonstrating the parallel() method is as follows:
import java.util.*; import java.util.stream.*; public class StreamAPI { public static void main(String[] args) { // 1. Create a set of numbers ArrayList<Integer> AL = new ArrayList<Integer>(); AL.add(20); AL.add(32); AL.add(12); AL.add(23); AL.add(51); // 2. Create sequential data stream Stream<Integer> stream = AL.stream(); // 3. Check if the stream is parallel if (stream.isParallel()) System.out.println("stream is parallel"); else System.out.println("stream is sequential"); // 4. Call the parallel() method - form a parallel stream Stream<Integer> stream2 = stream.parallel(); // 5. Display stream data if (stream2.isParallel()) System.out.println("stream2 is parallel"); else System.out.println("stream2 is sequential"); } }
Program execution result
stream is sequential stream2 is parallel
⇑
7. Method sequential(). Return a sequential data stream. Example
The sequential() method allows you to get a sequential stream from the calling stream. If the calling stream is sequential, the same stream is returned. The method is an intermediate operation. The method has the following general form:
S sequential()
here
- S – the resulting sequential stream.
The code of the program demonstrating the sequential() method is shown below.
import java.util.*; import java.util.stream.*; public class StreamAPI { public static void main(String[] args) { // 1. Create a set of numbers ArrayList<Integer> AL = new ArrayList<Integer>(); AL.add(20); AL.add(32); AL.add(12); AL.add(23); AL.add(51); // 2. Create the sequential data stream Stream<Integer> stream = AL.stream(); // 3. Check that the stream is sequential if (stream.isParallel()) System.out.println("stream is parallel"); else System.out.println("stream is sequential"); // 4. Call the sequential() method - form a sequential stream Stream<Integer> stream2 = stream.sequential(); // 5. Display data about stream2 if (stream2.isParallel()) System.out.println("stream2 is parallel"); else System.out.println("stream2 is sequential"); } }
Program execution result
stream is sequential stream2 is sequential
⇑
8. Method unordered(). Return the unordered data stream. Example
The unordered() method returns an unordered data stream based on the calling stream. If the calling data stream is unordered, then this stream is returned.
The general form of the method is as follows:
S unordered()
here
- S – the resulting unordered stream.
The method is the intermediate operation.
The text of the program demonstrating the unordered() method is as follows.
import java.util.*; import java.util.stream.*; import java.util.function.*; public class StreamAPI { public static void main(String[] args) { // 1. Create a set of numbers ArrayList<Double> AL = new ArrayList<Double>(); AL.add(1.5); AL.add(2.8); AL.add(-2.3); AL.add(3.4); AL.add(1.1); // 2. Create data stream from set of AL numbers Stream<Double> stream = AL.stream(); // 3. Sort stream stream = stream.sorted(); // 4. Display the stream // 4.1. Declare an action that displays an element of type Double Consumer<Double> action = (n) -> { System.out.print(n + " "); }; // 4.2. Invoke method forEach() System.out.print("Sorted Array AL = ["); stream.forEach(action); System.out.println("]"); // 5. Create a new unordered stream - the unordered() method stream = AL.stream().unordered(); // 6. Display unordered stream System.out.print("Unordered Array AL = ["); stream.forEach(action); System.out.println("]"); } }
⇑
Related topics
- Data streams. Stream API. General information
- The concept of final and intermediate operations. Examples. Methods stream(), parallelStream()
- Interface Stream<T>. Intermediate interface methods: filter(), map(), mapToDouble(), mapToInt(), mapToLong(), sorted(). Examples
- Interface Stream<T>. Final methods collect(), count(), forEach(), max(), min(), reduce(), toArray()
- Examples of solving tasks on data streams
- Examples of solving tasks on data streams, which are objects of the developed classes
⇑