Stream skip() Method in Java With Examples7 Jan 2025 | 6 min read The Stream.skip(long n) method in Java is an important part of the Stream API which was introduced in Java 8. It enables developers to build pipelines of operations on data sets. The skip() method is especially useful for skipping a certain number of elements in a stream while processing the remaining elements. Method SignatureStream<T>: The method returns a new stream, which is also of the type T just as the original stream. skip(long n): The name of the method skip itself indicates that it skips over elements in the stream. The parameter n indicates how many elements should be skipped. long n: It is a single parameter of type long that shows the number of items to skip from the starting of the stream. If n is greater than the number of elements in the stream, the resulting stream will be empty. Example 1: Skipping Elements in a List of IntegersIn this example, we will create a list of integers named numbers containing the elements from 1 to 10. Then we will convert the list numbers into a stream the use of stream operations. The skip(5) method is called on the stream which skips the first 5 elements of the stream. The collect(Collectors.toList()) method collects the remaining elements of the stream into a new list. Filename: SkipExample.java Output: [6, 7, 8, 9, 10] Example 2: Skipping Even Numbers after Filtering and Squaring the Remaining NumbersIn this example, we will create a list of integers named numbers containing the elements from 1 to 10. The filter(n -> n % 2 == 0) method filters the stream to include only even numbers. The skip(2) method is called on the filtered stream which skips the first 2 elements of the filtered stream. The map(n -> n * n) method is used to square each remaining number in the stream. The collect(Collectors.toList()) method collects the squared numbers into a new list. Filename: SkipWithConditionExample.java Output: [36, 64, 100] Example 3: Skipping Strings that Match a ConditionIn this example, we will create a list of strings named words containing various country names. Then convert the list words into a stream, enabling the use of stream operations. The filter(word -> word.startsWith("A")) method filters the stream to include only strings starting with the letter 'A'. The skip(2) method is called on the filtered stream which skips the first 2 elements of the filtered stream. Filename: SkipWithConditionExample1.java Output: [Austria] Example 4: Skipping Complex Objects Based on a ConditionIn this example, we will create a list of Person objects named people, each initialized with a name and an age. The people.stream() converts the list of Person objects into a stream. The filter(person -> person.age > 30) filters the stream to include only persons older than 30 years. Then skip(1) method is called to skip the first person in the filtered stream. Filename: SkipWithConditionExample2.java Output: [Tom (40), Bob (50)] Example 5: Skipping Elements in a List of Strings and Converting to UppercaseIn this example, we will create a list of strings named words, containing the elements "one", "two", "three", "four", "five", "six", and "seven". The List<String> result = words.stream() converts the list words into a stream. The skip(3) method is called on the stream to skip the first 3 elements. The map(String::toUpperCase) method is used to convert each remaining string in the stream to uppercase. Filename: SkipStringExample.java Output: [FOUR, FIVE, SIX, SEVEN] Applications1. PaginationWhen dealing with large datasets, you often need to break the data into manageable chunks or pages. Skip elements to access a specific page of data. For example, if each page displays 10 items, to get the 3rd page, you would skip the first 20 items. 2. Load BalancingIn distributed systems or parallel processing where work needs to be divided among multiple processors or servers. Skip a portion of tasks to balance the load across different nodes, ensuring that each node processes a unique subset of tasks. 3. Sequential Data ProcessingIn time-series data or any sequential data processing where certain initial entries are to be disregarded. Skip a predefined number of initial entries to focus on a specific range within the data sequence. |
A basic polyalphabetic substitution technique is used in the Vigenere Cypher to encrypt alphabetic text. It is more secure than a conventional Caesar cipher since it employs a keyword to move letters in the plaintext by varying amounts. In this section, we will explain the Vigenere...
5 min read
In this section, we will learn about the Morris traversal for inorder in Java. In Morris traversal, we do the traversal of a tree without the help of recursion or stack. The Morris traversal is based on the threaded binary tree. In this traversal, we do...
4 min read
In Java, inverting the bits of a number means flipping each bit from 0 to 1 and vice versa. It can be achieved using the bitwise NOT (~) operator. It's commonly used in tasks like binary manipulation and bitwise operations, providing a simple way to toggle...
7 min read
In this tutorial, we will see how one finds the index of an array element in Java. To avoid confusion, we will assume that all the elements of the array are unique. In other words, no element will occur more than once. In the input, an...
9 min read
The java.lang.Class class serves as a fundamental element of Java's Reflective Application Programming Interface (API), enabling software engineers to examine and modify classes through their coding process. As part of the java.lang package, it specifically refers to a particular class within a Java application. Objects instantiated...
33 min read
A thread is a program in execution created to perform a specific task. Life cycle of a Java thread starts with its birth and ends on its death. The start() method of the Thread class is used to initiate the execution of a thread and it goes...
5 min read
? In this section, we will learn why we use a constructor in Java and what is the purpose and need of the constructor. Along with this, we will also see the types of the constructor. In Java, the constructor is similar to the method. The property of...
3 min read
How to Convert a String to String Array in Java? In Java, a String is an object that represents the sequence of characters. In order to use Strings, we need to import the String class defined in java.lang package. The String array is an array of strings...
6 min read
In Java, both final and immutability are vital concepts related to object state and modification. Both concepts address different aspects, how objects and their states are managed. In this section, we will discuss the differences between final and immutability in Java. Java final Keyword The final keyword in...
4 min read
All the classes which are related for management of security related concerns in the java program are provided under this package. The various classes are discussed below: Class Description AccessControlContext Various decisions related to the access provision of system resources is taken by this class. The class is declared...
6 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India