How to Access the First Element of a Flux
Last Updated :
24 Apr, 2025
The Flux is one of the publishers in Spring WebFlux. Mostly this publisher is used for handling multiple events simultaneously. This Flux publisher belongs to reactive streams in WebFlux in Spring Reactive and this Flux publisher helps us to handle multiple events in an asynchronously non-blocking manner. One more thing is this Flux publisher is designed to handle 0 - N elements and emits data asynchronously as soon as It becomes Available.
In this article, we will learn how to access the first element of a Flux. Here, Flux means a list of elements.
Application of Flux:
- The Flux publisher can be able to handle large amounts of data in a non-blocking manner.
- And also, it can be used to process data from different resources like databases, HTTP requests, and others.
- It can provide lot operators with the required data from large amounts of data by using a filter, map, reduce, and other operators.
- Flux also supports backpressure, which allows a publisher to control the rate of data emitted by the subscriber.

Approaches to Access the First Element of a Flux
For accessing the first element of a Flux we have various approaches. Below, we have listed them. After that we will explain each approach with a good example. Below methods are available in Flux publisher.
- using next() method
- using blockFirst() method
- using take() method
- using collectList() method
Prerequisites:
- Spring WebFlux
- Functionality of Publisher, Subscriber
- Reactive Streams in Spring
Dependencies:
To access the Spring Reactive Streams, we need add below Dependencies in our spring boot project.
dependencies {
// Spring WebFlux
implementation 'org.springframework.boot:spring-boot-starter-webflux'
// Reactor Core
implementation 'io.projectreactor:reactor-core'
// Spring Boot Starter Test (if needed)
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
Program to Access the First Element of a Flux
Flux is a publisher which can handle 0 to N elements. In our case, we need access the first element from list elements in the Flux. For accessing first element from Flux, we have one method next(). It available in Flux publisher.
- To run this logic, we need create a Spring Stater project using Spring Tool Suite.
- While creating project select Dependencies which mention in the above.
- After completion project creation, In project main method class.
- Write this logic then run as spring boot application.
1. Using next() method
In this example,
- We have taken some Integer values from 100 to 500 in the form of Flux.
- Then we have used next method with Flux object, then we call the subscribe().
- After this we have created three lambda function for accessing first element from Flux.
- The next lambda function used for printing error message while any error raises.
- The final lambda expression is used for informing to the end user like the flux operation is completed.
Java
package com.reactive.app;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import reactor.core.publisher.Flux;
@SpringBootApplication
public class SpringreactorApplication {
public static void main(String[] args) {
SpringApplication.run(SpringreactorApplication.class, args);
// Create a Flux with some elements
Flux<Integer> flux = Flux.just(100, 200, 300, 400, 500);
// Access the first element of the Flux
flux.next().subscribe(
value -> System.out.println("First element of Flux: " + value),
error -> System.err.println("Error: " + error),
() -> System.out.println("Flux completed")
);
}
}
Output:

2. Using blockFirst() method
This is another approach for access the first element of a Flux. Here, we have use blockFirst() method, it can access the first element from the flux of item.
Java
package com.reactive.app;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import reactor.core.publisher.Flux;
@SpringBootApplication
public class SpringreactorApplication {
public static void main(String[] args) {
SpringApplication.run(SpringreactorApplication.class, args);
// Create a Flux with some elements
Flux<Integer> flux = Flux.just(100, 200, 300, 400, 500);
Integer firstElement = flux.blockFirst();
System.out.println("First element of Flux: " + firstElement);
}
}
Output:
output3. Using take() method
In this approach, we have used take() method. This method can take positions of Flux items. It's range start from 1 to N.
Java
package com.reactive.app;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import reactor.core.publisher.Flux;
@SpringBootApplication
public class SpringreactorApplication {
public static void main(String[] args) {
SpringApplication.run(SpringreactorApplication.class, args);
// Create a Flux with some elements
Flux<Integer> flux = Flux.just(100, 200, 300, 400, 500);
flux.take(1).subscribe(
value -> System.out.println("First element of Flux: " + value),
error -> System.err.println("Error: " + error),
() -> System.out.println("Flux completed")
);
}
}
Output:

4. Using collectList() method
In this approach, we have used collectList() method. Here, we have used collectList() method after collect all the methods. Then it prints the first element from list of elements by using get method in list.
Java
package com.reactive.app;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import reactor.core.publisher.Flux;
@SpringBootApplication
public class SpringreactorApplication {
public static void main(String[] args) {
SpringApplication.run(SpringreactorApplication.class, args);
// Create a Flux with some elements
Flux<Integer> flux = Flux.just(100, 200, 300, 400, 500);
flux.collectList().subscribe(
list -> {
if (!list.isEmpty()) {
Integer firstElement = list.get(0);
System.out.println("First element of Flux: " + firstElement);
} else {
System.out.println("Flux is empty");
}
},
error -> System.err.println("Error: " + error),
() -> System.out.println("Flux completed")
);
}
}
Output:
Similar Reads
How to Get First Element in Array in Java?
In Java, to get the first element in an array, we can access the element at index 0 using array indexing. Example 1: Below is a simple example that demonstrates how to access the element at index 0 in an array.Javapublic class Geeks { public static void main(String[] args) { // Declare and initializ
2 min read
How to Get First and Last Element of Slice in Golang?
Go language slice is more powerful, flexible, convenient than an array, and is a lightweight data structure. Slice is a variable-length sequence which stores elements of a similar type, you are not allowed to store different type of elements in the same slice. It is just like an array having an inde
2 min read
How to Correctly Access Elements in a 3D Pytorch Tensor?
In this article, we will discuss how to access elements in a 3D Tensor in Pytorch. PyTorch is an optimized tensor library majorly used for Deep Learning applications using GPUs and CPUs. It is one of the widely used Machine learning libraries, others being TensorFlow and Keras. The python supports t
4 min read
How to access the metadata of a tensor in PyTorch?
In this article, we are going to see how to access the metadata of a tensor in PyTorch using Python. PyTorch in Python is a machine learning library. Also, it is free and open-source. It was firstly introduced by the Facebook AI research team. A tensor in PyTorch is similar to a NumPy array. But it
3 min read
How to print elements of a Stream in Java 8
Introduced in Java 8, the Stream API is used to process collections of objects. A stream is a sequence of objects that supports various methods which can be pipelined to produce the desired result. The features of Java stream are â A stream is not a data structure instead it takes input from the Col
5 min read
Find the First Occurrence of a Specific Element in a Vector in R
In this article, we will discuss how to find the first occurrence of a specific element in a vector with its working example in the R Programming Language using R while loop. also explore various examples with detailed explanations to illustrate different concepts, principles, and scenarios across v
3 min read
How to Find Curl of a Vector
Finding the curl of a vector is a crucial concept in vector calculus as The Curl of a Vector tells us how much and in which direction a vector field rotates at a specific point. The curl of a Vector also helps to find the angular momentum of a vector field at a point. This concept is used in many fi
8 min read
How to Get First Row of Pandas DataFrame?
To get the first row of a Pandas Dataframe there are several methods available, each with its own advantages depending on the situation. The most common methods include using .iloc[], .head(), and .loc[]. Let's understand with this example:Pythonimport pandas as pd data = {'Name': ['Alice', 'Bob', '
4 min read
Java Program to Get the First Element from LinkedHashSet
LinkedHashSet is an implementation of Set Abstract Data Type (ADT). It extends from the HashSet class which in-turn implements Set Interface. The difference between the LinkedHashSet and HashSet is the property of maintaining the element ordering. LinkedList is just a container holding sequence of e
3 min read
How to Configure Fluxbox
Fluxbox is an X window manager based on the Blackbox 0.61.1 source code. It uses very few resources, is simple to use, and is packed with features for a quick and simple desktop experience. Each computer user may have a different definition of a desktop. Many people consider their desktop to be thei
3 min read