jDataLab

2 minute read

Data is often gathered into a unit with a collection structure prior to further analysis. The collection structure is supported by many programming languages. Java Streams API implements typical collection-based processing operations, such as grouping, sorting and searching that are similar to aggregate functions in SQL.

With Java Streams, only declare what to be expected!

By knowing these Java stream functions, there is no need of writing loops over and over to find maximum, minimum or other aggregate values; only declaring what to be expected and looking up the corresponding stream function to calculate it. Also, the stream API helps speed up large collections processing.

Java Streams follows functional programmming paradigm by using Lambda expression. A lambda expression makes an anonymous function.

In Wikipedia, functional programming is a programming paradigm—a style of building the structure and elements of computer programs—that treats computation as the evaluation of mathematical functions and avoids changing-state and mutable data. It is a declarative programming paradigm, which means programming is done with expressions or declarations instead of statements.

Some Features of Stream

  • A stream is a pipeline of functions that can be evaluated.

  • A stream is not a data structure.

  • Steams won’t mutate data.

Perform same operations to each element in a stream

import java.util.Arrays;

List<String> names = Arrays.asList("Tom", "Jim", "", "Sally", "");
names.stream()
	 .forEach( n -> {System.out.println(n);});
import java.util.Arrays;

List<String> names = Arrays.asList("Tom", "Jim", "", "Sally", "");
names.stream()
	.forEach( n -> {
		int pos = names.indexOf(n);
		System.out.println(pos + ":" + n);
		});

Filter elements

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

List<String> names = Arrays.asList("Tom", "Jim", "", "Sally", "");
List<String> result = names
                .stream()
                .filter(n -> "Tom".equals(n))
                .collect(Collectors.toList());
				
result.forEach(r->System.out.println(r));
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

        List<String> names = Arrays.asList("Tom", "Jim", "", "Sally", "");
        List<String> result = names
                .stream()
                .filter(n -> n.contains("a"))
                .collect(Collectors.toList());
        
		result.forEach(r -> System.out.println(r));

Reduce elements to a single value

import java.util.ArrayList;

        ArrayList<Integer> scores = new ArrayList<>();
        scores.add(20);
        scores.add(30);
        scores.add(90);
        scores.add(30);
        
        scores.forEach(System.out::println);
		
		// To print distinct integers in an integer stream
        scores.stream().distinct().forEach(System.out::println);
        
		// To find a sum from a stream of integers
        int sum = scores.stream().reduce(0, (x,y)->x+y);
        System.out.println(sum);

Mapping

import java.util.ArrayList;

        ArrayList<Integer> scores = new ArrayList<>();
        scores.add(20);
        scores.add(30);
        scores.add(90);
        scores.add(30);
		
		// Map list to a double stream and aggreage to a sum
		double sum = scores.stream().mapToDouble(n->(double)n).sum();
		System.out.println(sum);

Sorting

import java.util.ArrayList;

ArrayList<Integer> scores = new ArrayList<>();
scores.add(20);
scores.add(30);
scores.add(90);
scores.add(30);
     
// Sorting
scores.stream().sorted().forEach(n->System.out.println(n));