
- 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 to use intermediate stream operations in JShell in Java 9?
JShell is a tool introduced in Java 9, and it accepts simple statements like expressions, variables, methods, classes, etc.. as input and produces immediate results.
A Stream is a sequence of values. An intermediate stream operation is an operation that takes a stream. For instance, it can be applied to a lambda expression and produces another stream of elements as its result.
The most popular intermediate stream operations are mentioned below:
- 1) sorted(): This method preserves the elements of the consumed stream as a result but also puts them in natural sorted order.
- 2) distinct(): This method returns a stream retaining only unique elements of the input stream, and it can maintain the relative order of retained elements.
- 3) filter(): This method can be used to filter stream elements based on some logic.
- 4) map(): This method applies lambda expression to compute new results from input stream elements. Then, it returns a stream of these results as output.
In the below code snippet, we can implement different intermediate stream operations in the JShell tool.
Snippet
jshell> List<Integer> numbers = List.of(3, 10, 23, 200, 77, 9, 32); numbers ==> [3, 10, 23, 200, 77, 9, 32] jshell> numbers.stream().sorted().forEach(elem -> System.out.println(elem)); 3 9 10 23 32 77 200 jshell> List<Integer> numbers = List.of(3, 5, 54, 280, 5, 9, 40); numbers ==> [3, 5, 54, 280, 5, 9, 40] jshell> numbers.stream().distinct().forEach(elem -> System.out.println(elem)); 3 5 54 280 9 40 jshell> numbers.stream().distinct().sorted().forEach(elem -> System.out.println(elem)); 3 5 9 40 54 280 jshell> numbers.stream().distinct().map(num -> num * num).forEach(elem -> System.out.println(elem)); 9 25 2916 78400 81 1600
- Related Articles
- How to use terminal stream operations in JShell in Java 9?
- How to debug JShell in Java 9?
- How to use the collect() method in Stream API in Java 9?
- JShell in Java 9?
- How to get JShell documentation in Java 9?
- Difference between intermediate and terminal operations in Java 8
- How to create JShell instance programmatically in Java 9?
- How to reset the JShell session in Java 9?
- How to implement java.time.LocalDate using JShell in Java 9?
- How to import external libraries in JShell in Java 9?
- How to handle an exception in JShell in Java 9?
- How to create scratch variables in JShell in Java 9?
- How to implement a String in JShell in Java 9?
- How to create a thread in JShell in Java 9?
- How to create wrapper objects in JShell in Java 9?

Advertisements