JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Checkout for Promo Codes

Java 8 Stream flatmap method example.


In a very simple note, Stream.flatMap() is used to convert a Stream of Stream into a list of values.

The flatMap() operation has the effect of applying a one-to-many transformation to the elements of the stream, and then flattening the resulting elements into a new stream.

This page gives a simple example on getting count of distinct elements in a file. In this example, each line represents a stream, each steam(line) is broken into words which represents again a independent stream. Now all these multiple streams will be aggregated to a single stream using flatMap method.

package com.java2novice.streams;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;

public class StreamFlatMapEx {

	public static void main(String a[]) {

		try {
			long noOfWords = Files.lines(Paths.get("C:\testfile.txt"))
									.flatMap(l->Arrays.stream(l.split(" +")))
									.distinct()
									.count();
			System.out.println("noOfWords: "+noOfWords);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

Output:
noOfWords: 14
<< Previous Program | Next Program >>

Java 8 Streams Examples

  1. How Java 8 Streams work?
  2. Java 8 Streams parallelism introduction.
  3. Explain non-interference behavior of Java 8 Streams.
  4. Create Java 8 Stream using Stream.of() method example.
  5. Create Java 8 Stream using List example.
  6. Create Java 8 Stream using Stream.generate() method.
  7. Java 8 Stream.filter() example.
  8. Java 8 Stream.map() example.
  9. Java 8 Stream flatmap method example.
  10. Java 8 Stream peek method example.
  11. Java 8 Stream distinct method example.
  12. Java 8 Stream sorted method example.
  13. Java 8 Stream limit method example.
  14. Java 8 Stream forEach method example.
  15. Java 8 Stream toArray method example.
  16. Java 8 Stream reduce method example.
  17. Java 8 Stream collect method example.
  18. Java 8 Stream concat method example.
  19. Java 8 Stream anyMatch(), allMatch() and noneMatch() example.
  20. Java 8 Stream findFirst(), findAny() example.
  21. Primitive type Stream example.
Knowledge Centre
String Vs StringBuffer
We know that String is immutable object. We can not change the value of a String object once it is initiated. If we try to change the value of the existing String object then it creates new object rather than changing the value of the existing object. So incase, we are going to do more modificatios on String, then use StringBuffer. StringBuffer updates the existing objects value, rather creating new object.
Famous Quotations
It is amazing what you can accomplish if you do not care who gets the credit.
-- Harry Truman

About Author

I'm Nataraja Gootooru, programmer by profession and passionate about technologies. All examples given here are as simple as possible to help beginners. The source code is compiled and tested in my dev environment.

If you come across any mistakes or bugs, please email me to [email protected].

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java™ Platform Standard Ed. 8 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2020 by Nataraja Gootooru. All Rights Reserved.