Stream in java
I have mentioned or written down some of the most important stream conversion . This is like the basic operations that u need to know before studying the stream in java
Lets first discuss what stream is so basically stream gives you power to process a collection of data using operations such as filtering, transforming, sorting, and calculating results.
Imagine you have an array of numbers:
int[] nums = {10, 15, 20, 25, 30};Suppose you want to find only the even numbers.
Without a Stream, you might write:
for (int num : nums) {
if (num % 2 == 0) {
System.out.println(num);
}
}Using a Stream, you can write:
Arrays.stream(nums)
.filter(num -> num % 2 == 0)
.forEach(System.out::println);1.
well will work on the Arrays conversion first using the stream
int[] — — → Integer[];
Now if u want to deal or process, or do any operation with all the elements individually u get the functions such as map , filter ,forEach in that case.
The code for the conversion .
import java.util.*;
import java.util.stream.*;
class Main {
public static void main(String[] args) {
int[] nums={1,2,3,4,5};
Integer[] num=Arrays.stream(nums) //it creates the nums into stream based on the datatypes
//for example int[] --> intStream and long[] --> longStream
.boxed() //each of the int value is converted into Integer
.toArray(Integer[]::new); //this line creates the Integer[] objects and saves there
System.out.println(Arrays.toString(num));
System.out.println(num.getClass().getName());
}
}2.
Get Anush Lobo’s stories in your inbox
Join Medium for free to get updates from this writer.
String[] to List<String>
String[] names = {"Anu", "Rahul", "Priya"};
List<String> nameList = Arrays.stream(names)
// Converts the String array into a Stream<String>
.collect(Collectors.toList());
// Collects all the elements from the stream into a List<String>or
String[] names = {"Anu", "Rahul", "Priya"};
List<String> nameList = Arrays.stream(names)
// Converts the String array into a Stream<String>
.toList();
// Collects all the elements from the stream into a List<String>3.
List<String> → String[]
import java.util.*;
import java.util.stream.*;
class Main {
public static void main(String[] args) {
List<String> names = List.of("Anu", "Rahul", "Priya");
String[] nameArray = names.stream()
// Converts the List<String> into a Stream<String>
.toArray(String[]::new);
// Creates a String[] array and stores the stream elements in it
System.out.println(Arrays.toString(nameArray));
System.out.println(nameArray.getClass().getName());
}
}4.
List<Integer> → int[]
import java.util.*;
import java.util.stream.*;
class Main {
public static void main(String[] args) {
List<Integer> nums = List.of(1, 2, 3, 4, 5);
int[] numbers = nums.stream()
// Converts the List<Integer> into a Stream<Integer>
.mapToInt(Integer::intValue)
// Converts each Integer object into a primitive int,
// producing an IntStream
.toArray();
// Collects the IntStream elements into a primitive int[]
System.out.println(Arrays.toString(numbers));
System.out.println(numbers.getClass().getName());
}
}5.
int[] → List<Integer>
import java.util.*;
import java.util.stream.*;
class Main {
public static void main(String[] args) {
int[] nums = {1, 2, 3, 4, 5};
List<Integer> numList = Arrays.stream(nums)
// Converts the int[] into an IntStream
.boxed()
// Converts each primitive int into an Integer object
.toList();
// Collects the Integer elements into an unmodifiable List<Integer>
System.out.println(numList);
System.out.println(numList.getClass().getName());
}
}6.
String[] → Set<String>
import java.util.*;
import java.util.stream.*;
class Main {
public static void main(String[] args) {
String[] names = {"Anu", "Rahul", "Anu", "Priya"};
Set<String> nameSet = Arrays.stream(names)
// Converts the String[] into a Stream<String>
.collect(Collectors.toSet());
// Collects the stream elements into a Set<String>
// Duplicate elements are not stored in the Set
System.out.println(nameSet);
System.out.println(nameSet.getClass().getName());
}
}7.
Set<String> → List<String>
import java.util.*;
import java.util.stream.*;
class Main {
public static void main(String[] args) {
Set<String> names = new HashSet<>(
Arrays.asList("Anu", "Rahul", "Priya")
);
List<String> nameList = names.stream()
// Converts the Set<String> into a Stream<String>
.toList();
// Collects the stream elements into a List<String>
// The resulting list is unmodifiable
System.out.println(nameList);
System.out.println(nameList.getClass().getName());
}
}





