To convert a Java 8 Stream to an Array, we can make use of the toArray() method on the Stream object.
Let's take a look at it with a few examples.
Example 1:package org.code2care.java.examples;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Java8StreamToArrayExample {
public static void main(String[] args) {
List<Integer> numbersList = new ArrayList<>();
numbersList.add(10);
numbersList.add(20);
numbersList.add(30);
numbersList.add(40);
numbersList.add(50);
int[] intArray = numbersList.stream()
.mapToInt(Integer::intValue)
.toArray();
System.out.println(Arrays.toString(intArray));
}
}
Example 2:
package org.code2care.java.examples;
public class Employee {
private String empName;
private int empInt;
public Employee(String empName, int empInt) {
this.empName = empName;
this.empInt = empInt;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public int getEmpInt() {
return empInt;
}
public void setEmpInt(int empInt) {
this.empInt = empInt;
}
@Override
public String toString() {
return "Employee{" +
"empName='" + empName + '\'' +
", empInt=" + empInt +
'}';
}
}
package org.code2care.java.examples;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;
public class StreamToArrayExample2 {
public static void main(String[] args) {
List<Employee> employeeList = Arrays.asList(
new Employee("Mike", 101),
new Employee("Sam", 202),
new Employee("Andrew", 303)
);
Stream<Employee> stream = employeeList.stream();
Employee[] array = stream.toArray(Employee[]::new);
System.out.println(Arrays.toString(array));
}
}
Output:
-
Facing issues? Have Questions? Post them here! I am happy to answer!
More Posts related to Java,
- Java equals method - Tutorial
- Unbound classpath container: JRE System Library [JavaSE-1.7]
- Spring Boot: @RequestBody not applicable to method
- Java 8: Steam map with Code Examples
- Java Program: Random Number Generator
- Java java.time.Clock class code examples [Java Date Time API]
- Fix: type argument is not within bounds of type-variable T
- [Fix] java.net.MalformedURLException: unknown protocol
- Java 7 addSuppression() and getSuppression() Exception Handling
- Convert Java Array to ArrayList Code Example
- How to Word-Warp Console logs in IntelliJ
- Ways Compare Dates in Java Programming with Examples
- Remove Trailing zeros BigDecimal Java
- CRUD operations in Spring Boot + JDBC
- [Java Threads] Should we extend Thread Class or implement Runnable interface
- Json Serialization and Deserialization using Java Jackson
- Create simple struts2 project using maven commands
- How to install Java OpenJDK 11 on Alpine Linux
- Unsupported major.minor version 52.0 in java
- Error: Can not find the tag library descriptor for
- Java: Convert String to Binary
- How to run Java Unit Test cases with Apache Maven?
- Java: Testing Private Methods in JUnit using reflection API Example
- Java JDBC Connection with MySQL Driver in VS Code + Troubleshooting
- Java Join Strings with Comma Separator
More Posts:
- How to make ZSH as the default shell on Ubuntu - zsh
- How to view the desktop when using macOS Stage Manager? - MacOS
- Eclipse : Workspace was written with an older version of the product and will be updated - Eclipse
- Make Android TextView Clickable like Buttons - Android
- [Solved] Dynamic Web Module 3.0 requires Java 1.6 or newer Mac OSX - Mac-OS-X
- Find installed Gradle version command - Gradle
- SharePoint Server 2016 setup error - A system restart from a previous installation or update is pending. Restart your computer and run setup to continue. - SharePoint
- You're not on Teams yet, but you can set it up for your organization. [Microsoft Teams Login Error] - Microsoft