How to Print Multiples of 3 in Java 8 Using Stream API

Learn How to Print Multiples of 3 in Java 8 Using Stream API , filter method, modulus operator, and Collectors.toList() with output.

Code Explanation (Step-by-Step)

  • First, create a list of integer numbers using the Arrays.asList() method.
  • Convert the list into a stream using the stream() method.
  • Use the filter() method to check whether each number is divisible by 3.
  • The condition number % 3 == 0 checks if the number is a multiple of 3.
  • Collect the filtered numbers into a new list using Collectors.toList().
  • Finally, print the list of multiples of 3.
				
					import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class MultipleCheck {
	public static void main(String[] args) {
		List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
		
		List<Integer> numbers = list.stream()
		        .filter(number -> rwnumber % 3 == 0)
		        .collect(Collectors.toList());
		System.out.println("Multiples of 3 numbers :- " + numbers);
	}
}
				
			

Output :
Multiples of 3 numbers :- [3, 6, 9]

Java Program to Print Multiples of 3

				
					import java.util.stream.IntStream;

public class MultiplesOf3Example {
    public static void main(String[] args) {

        IntStream.iterate(3, number -> number + 3)
                .limit(5)
                .forEach(System.out::println);
    }
}
				
			

Output :
3
6
9
12
15

FAQ
How do you check multiples and divisible of 3 in Java?
You can check multiples of 3 in Java using the modulus operator. If number % 3 == 0, then the number is a multiple of 3.
How do you print multiples of 3 using Java 8?
You can use the Java 8 Stream API with the filter() method to filter numbers divisible by 3 and collect them using Collectors.toList().
What is the use of filter() in Java Stream?
The filter() method is used to select elements from a stream based on a given condition.