Programming using Java 8 Streams

Aug 5, 2023·

3 min read

Sum of numbers in Array List using Streams

List<Integer> al= Arrays.asList(10,45,20,30);
int x=al.stream().reduce(10,(a,b)->a+b);
System.out.println(x);

in the above code snippet reduce function will calculate the sum of numbers , and first argument value of reduce function is the starting value

Average of numbers using streams

List<Integer> al= Arrays.asList(10,45,20,30);
double x=al.stream().mapToInt(e->e).average().getAsDouble();
System.out.println(x);

we cant directly apply average function on streams, and average cant be double. so we need to change to double

program to calculate the average of squares whose value is greater than 400

    List<Integer> al= Arrays.asList(10,30,22,23);
    double ans=al.stream().map(e->e*e).filter(x->x>400).mapToInt(e->e).average().getAsDouble();
    System.out.println(ans);

In the above code first we converted all numbers into their squares, and filtered the numbers greater than 400, then applied mapToInt() function thenfinally average

Program to print even numbers using streams

List<Integer> al= Arrays.asList(10,30,22,23);
List<Integer> ans=al.stream().filter(x->x%2==0).collect(Collectors.toList());
System.out.println(ans);

In the above code collect method converting the numbers to List

Program to print odd numbers using streams

List<Integer> al= Arrays.asList(10,30,22,23);
List<Integer> ans=al.stream().filter(x->x%2!=0).collect(Collectors.toList());
System.out.println(ans);

In the above code collect method converting the numbers to List

Print numbers start with 2 as prefix

List<Integer> al= Arrays.asList(10,30,22,23,56,278, 23, 55 , 24);
List<Integer> ans=al.stream().map(e->String.valueOf(e))
                            .filter(e->e.startsWith("2"))
                                .map(Integer::valueOf)
                                    .collect(Collectors.toList());
System.out.println(ans);

In the above code

  1. first mapped all numbers to string using String.valueOf() function

  2. Then filtered using Starts With Method

  3. Then again converted to Int using Integer.valueOf()

  4. Then collected those numbers as List

Program to find the duplicates in List

List<Integer> al= Arrays.asList(10,10,2,2,3,4,55,66,66,77,43);
 Set<Integer> dup=al.stream().filter(e->Collections.frequency(al,e)>1).collect(Collectors.toSet());
 System.out.println(dup);

In the above code we have to find the frequency using Collections. frequency(x1,x2) ie x1 is the list name and x2 is the element we need to find

Second Method to find duplicates

Set<Integer> s=new HashSet<>();
List<Integer> al1= al.stream().filter(e->!s.add(e)).collect(Collectors.toList());
System.out.println(al1);

in the above code add method will return true if the number is not there in the set else it will return false. by using that we will filter out the numbers

Program to find max and min numbers from Array List

List<Integer> al= Arrays.asList(10,30,22,23,56,278, 23, 55 , 24);
int max= al.stream().max(Comparator.comparing(e->Integer.valueOf(e))).get();
int min= al.stream().min(Comparator.comparing(e->Integer.valueOf(e))).get();
    System.out.println(max);
    System.out.println(min);

In the above code snippet comparing function will do the comparison returns the max and min value accordingly

Program to sort numbers Array List

List<Integer> al= Arrays.asList(10,10,2,2,3,4,55,66,66,77,43);
List<Integer> asc=al.stream().sorted().collect(Collectors.toList());
List<Integer> dsc=al.stream().sorted(Collections.reverseOrder()).collect(Collectors.toList());

In the above code sorted() will arrange the numbers in the required order , if we pass Collections.reverseOrder() to Sorted function it will sort in descending order

Using Limit and Skip in Streams

        List<Integer> al= Arrays.asList(10,10,2,2,3,4,55,66,66,77,43);
        List<Integer> l1=al.stream().limit(5).collect(Collectors.toList());
        int sum1=al.stream().limit(5).reduce(0,(a,b)->a+b);
        System.out.println(l1);
        System.out.println(sum1);

        List<Integer> l2=al.stream().skip(5).collect(Collectors.toList());
        int sum2=al.stream().limit(5).reduce(0,(a,b)->a+b);
        System.out.println(l2);
        System.out.println(sum2);

In the above code limit function will limit to those many numbers and skip function will skip those many numbers from array list

Program to calculate nth highest/lowest using streams

List<Integer> al= Arrays.asList(10,10,2,2,3,4,55,66,66,77,43);
int thirdHighest=al.stream().distinct().sorted(Collections.reverseOrder()).limit(3).skip(2).findFirst().get();
int thirdLowest= al.stream().distinct().sorted().limit(3).skip(2).findFirst().get();
System.out.println(thirdHighest);
System.out.println(thirdLowest);

in the above code find first will return the first number , and skip and limit will function accordingly