↓ Skip to main content
Java

map and FlatMap

·2 mins
Table of Contents

package java.util.stream;
#

Referring to the original Java 8 Documentation.

<R> Stream<R> flatMap(Function<? super T,? extends Stream<? extends R>> mapper)
  • Returns a stream consisting of
    1. Results of replacing each element of this stream with the contents of a mapped stream produced by applying the provided mapping function to each element.
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

public class Main {

    public static void main(String[] args) {
	    // write your code here
        int myArray[] = { 1, 1, 2, 3, 4 , 5,6, 6,6, 7, 7, 7, 7, 7,7,3, 4, 3, 3, 2, 2, 2, 2, 2 };

        // Simple map
        // Multiply each element by 5
        Arrays.stream(myArray).map(index -> index * 5).forEach(System.out::println);
    }
}

The stream above multiplies each element by 5.

public class Main {

    public static void main(String[] args) {
	    // write your code here
        int myArray[] = { 1, 1, 2, 3, 4 , 5,6, 6,6, 7, 7, 7, 7, 7,7,3, 4, 3, 3, 2, 2, 2, 2, 2 };

        // Will create a map of
        // Item that appears and the number of times it appears.
        Map<Integer, Integer> myMap = Arrays.stream(myArray).mapToObj(i -> Integer.valueOf(i)).sorted().collect(Collectors.groupingBy(Function.identity(), Collectors.summingInt(e -> {
            System.out.println(" Element read: " + e);
            return 1;
        })));

        myMap.keySet().forEach( e -> {
            System.out.println(" [ " + e + " ]  = " + myMap.get(e).intValue());
        });

        //[[1,2], [2,6], [3,4]]
        //[1,2,2,6....]
        int[] myInts = myMap.keySet().stream().mapToInt(i -> Integer.valueOf(i)).flatMap(i -> IntStream.of(i, myMap.get(i))).toArray();

        Stream.of(myInts).forEach(System.out::println);
    }
}

flatMap as used in the above example allows creation of one stream from individual elements of the original stream which had.

[[1,2], [2,6], [3,4]]

FlatMap

Author
Samarthya
Engineer, learner, tinkerer. This is my trophy shelf of everyday work and leisure.

Related

JavaStream: Characters

·1 min
Continuing from the last blog how about checking a pangram using streams. public class PangramChecker { private String allChars = "abcdefghijklmnopqrstuvwxyz"; public boolean isPangram(String input) { if (input.length() <= 0 || input.isBlank()){ return false; } return (allChars.chars().map(x -> (char) x).filter(x -> input.toLowerCase().indexOf(x) >= 0).count() == 26); } } All the for loops have been squeezed into a simple line

Java Streams

·2 mins
Stream is an new abstraction that lets one process data in a declarative way. A stream does not store data and, in that sense, is not a data structure. It also never modifies the underlying data source. The easiest way to learn a new API or a construct is by trying it. So here is an exercise that I tried to learn the Stream API. This is an exercise from exercism.io.

PS: Process Status

·2 mins
MAN Pages are one source of truth for all curious queries. man ps Name ps - report a snapshot of the current processes. It is used to display instant information about the active processes in the system.