The Artima Developer Community
Sponsored Link

Java Buzz Forum
How to use flatMap method of Java 8 streams | Streams in Java 8

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Ram N

Posts: 2777
Nickname: ramram
Registered: Jul, 2014

Ram N is Java Programmer
How to use flatMap method of Java 8 streams | Streams in Java 8 Posted: Sep 6, 2017 5:04 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Ram N.
Original Post: How to use flatMap method of Java 8 streams | Streams in Java 8
Feed Title: JAVA EE
Feed URL: http://ramj2ee.blogspot.com/feeds/posts/default?alt=rss
Feed Description: This blog has viedo tutorials and Sample codes related to below Technologies. 1.J2EE 2.Java 3.Spring 4.Hibernate 5.Database 6.Oracle 7.Mysql 8.Design Patterns
Latest Java Buzz Posts
Latest Java Buzz Posts by Ram N
Latest Posts From JAVA EE

Advertisement

Click here to watch in Youtube :
https://www.youtube.com/watch?v=b75LVKzjiEc&list=UUhwKlOVR041tngjerWxVccw

Click the below Image to Enlarge
How to use flatMap method of Java 8 streams | Streams in Java 8
How to use flatMap method of Java 8 streams | Streams in Java 8
FlatMapDemo.java
import java.util.Arrays;
import java.util.stream.Stream;

public class FlatMapDemo
{
public static void main(String[] args)
{

String[][] strArray = new String[][] { { "a", "b" }, { "c", "d" }, { "e", "f" } };

// Stream<String[]>
Stream<String[]> strArrayStream = Arrays.stream(strArray);

//Convert Stream<String[]> to Stream<String>
Stream<String> strStream = strArrayStream.flatMap((x -> Arrays.stream(x)));

// filter a stream of string
Stream<String> filterdStream = strStream
.filter(x -> "c".equals(x.toString()));

filterdStream.forEach(System.out::println); //Output is c
}

}
Output
c

NonFlatMapDemo.java
import java.util.Arrays;
import java.util.stream.Stream;

/**
*
* This example will print an empty result, because filter() has
* no idea how to filter a stream of String[].
*
*/

public class NonFlatMapDemo
{
public static void main(String[] args)
{

String[][] strArray = new String[][] { { "a", "b" }, { "c", "d" },{ "e", "f" } };

// Stream<String[]>
Stream<String[]> strArrayStream = Arrays.stream(strArray);

// filter a stream of string[], and return a string[]?
Stream<String[]> filterdStream = strArrayStream
.filter(x -> "c".equals(x.toString()));

filterdStream.forEach(System.out::println); //Output is empty...
}

}
Output
//Output is empty...

Click the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/StreamDemo_flatmap_App.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/StreamDemo_flatMap

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/53e39342c1c41555c6e548783b3cf96ab7bd9ee6/BasicJava/StreamDemo_flatMap/?at=master

See also:

  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • Read: How to use flatMap method of Java 8 streams | Streams in Java 8

    Topic: How to use flatMap method of Java 8 streams | Streams in Java 8 Previous Topic   Next Topic Topic: Lucene’s near-real-time segment index replication

    Sponsored Links



    Google
      Web Artima.com   

    Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use