How to use stream and IntStream from Java 8?

IntStream class is a specialization of Stream interface.

  • It is a stream primitive int-valued elements.
  • This class is a part of java.util.stream package.
  • IntStream of(int t) returns a sequential IntStream containing a single element.

            Syntax :  static IntStream of(int t)

Stream API is used to process collections of objects.

  • Stream is use to perform operations like filtering, mapping, reducing and sorting.
  • This interface is collect of data list.
  • Stream<Integer> is a stream of Integer objects.

            Syntax :-     Stream<T> stream

				
					import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

public class TestMethod {
	public static void main(String[] args) {
		Stream<String> stream = Stream.of("well", "hello", "right");
		List<String> list = stream.collect(Collectors.toList());
		list.forEach(value -> System.out.println(value));

		IntStream iStream = IntStream.of(1, 2, 3, 4, 5, 6);
		List iList = iStream.boxed().collect(Collectors.toList());
		iList.forEach(System.out::println);
	}
}
				
			

output:- well
hello
right
1
2
3
4
5
6

Leave a Comment

Your email address will not be published. Required fields are marked *