In this Java 8 example, we will take a look at how to iterate over an Array using Java 8 Stream and foreach method,
Code Example 1:1 | public static void main(String[] args) {
2 | int[] numbers = {1,2,3,4,5,6};
3 | Arrays.stream(numbers).forEach(System.out::println);
4 | }
Output:
1
2
3
4
5
6
As you see int array at the line number 2 and on line 3 we convert this array to stream using Arrays.stream(int[] arr) method and then use foreach over it and print the values out using System.out::println
Code Example 2:
public static void main(String[] args) {
String[] languages = {"Java","Python","Php","C#","Go"};
Arrays.stream(languages).forEach(System.out::println);
}
Output:
Java
Python
Php
C#
Go
More Posts related to Java,
- CRUD operations in Spring Boot + JDBC
- Java Check Leap Year - Programs with Code Examples
- [fix] Java JDBC ConnectException: Connection refused
- How to add hours and minutes to Java Instant
- Java Program: Random Number Generator
- Java: The value of the local variable string is not used
- How to get list of all Java versions installed on macOS
- Java SE JDBC with Prepared Statement Parameterized Select Example
- Java + Spring JDBC Template + Gradle Example
- Convert String to LocalDate in Java
- Remove Trailing zeros BigDecimal Java
- Java 8 Predicate Functional Interface isEqual() Method Example
- How to Hardcode Date in Java with Examples
- Java 8: Predicate negate() default Function Example
- Java: Collect Stream as ArrayList or LinkedList
- The Motivation Behind Generics in Java Programming
- How to Add/Subtract Days to the Current Date in Java
- Error: Can not find the tag library descriptor for
- Setting up JUnit 5 dependency with Maven Example
- Run Java Code Every Second
- How to create a tar.gz file using Java
- [Fix] java: integer number too large compilation error
- Java 8: Find the Max value in a List
- Your JBoss Application Server 7 is running However you have not yet added any users to be able to access the admin console
- Convert Java Array to ArrayList Code Example
More Posts:
- Install Eclipse IDE on M1/M2 Mac Natively - Eclipse
- How to remove unwanted Java imports in IntelliJ (alternative of eclipse Ctrl + Shift + O) - Eclipse
- Take Screenshot on Mac OS X (Keyboard Shortcuts) - Mac-OS-X
- CSS: Apply opacity only for div background and not text - CSS
- How to Make Android TextView Text Bold - Android
- Advanced print() Function Tutorial and Techniques for Python Developers - Python
- Android Studio : Change FAB icon color : FloatingActionButton - Android-Studio
- Android : Accidental Octal Lint Warning - Android