The Artima Developer Community
Sponsored Link

Java Buzz Forum
How to use flatMap method to convert Stream of Set to Stream of String | Streams in Java

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: 2649
Nickname: ramram
Registered: Jul, 2014

Ram N is Java Programmer
How to use flatMap method to convert Stream of Set to Stream of String | Streams in Java Posted: Sep 6, 2017 10: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 to convert Stream of Set to Stream of String | Streams in Java
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=ruVGaln6rJs&list=UUhwKlOVR041tngjerWxVccw

Student.java
import java.util.HashSet;
import java.util.Set;

public class Student
{
private String name;
private Set<String> bookSet;

public void addBook(String bookName)
{
if (this.bookSet == null)
{
this.bookSet = new HashSet<>();
}
this.bookSet.add(bookName);
}

public String getName()
{
return name;
}

public void setName(String name)
{
this.name = name;
}

public Set<String> getBookSet()
{
return bookSet;
}

public void setBookSet(Set<String> bookSet)
{
this.bookSet = bookSet;
}

}
FlatMapDemo1.java
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

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

Student student1 = new Student();
student1.setName("Peter");
student1.addBook("Java in Action");
student1.addBook("Spring in Action");
student1.addBook("Ruby in Action");

Student student2 = new Student();
student2.setName("John");
student2.addBook("Java in Action");
student2.addBook("Learning Java Script");

List<Student> studentList = new ArrayList<>();
studentList.add(student1);
studentList.add(student2);

/*
* Stream<Set<String>>
*/

Stream<Set<String>> setStream = studentList.stream()
.map(student -> student.getBookSet());

/*
* Stream<Set<String>> to Stream<String>
*/

Stream<String> stringStream = setStream // Stream<Set<String>>
.flatMap(student -> student.stream()); // Stream<String>

List<String> bookList = stringStream.distinct()
.collect(Collectors.toList());

bookList.forEach(bookName -> System.out.println(bookName));
}

}
Output
Ruby in Action
Java in Action
Spring in Action
Learning Java Script

FlatMapDemo2.java
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class FlatMapDemo2
{
public static void main(String[] args)
{
Student student1 = new Student();
student1.setName("Peter");
student1.addBook("Java in Action");
student1.addBook("Spring in Action");
student1.addBook("Ruby in Action");

Student student2 = new Student();
student2.setName("John");
student2.addBook("Java in Action");
student2.addBook("Learning Java Script");

List<Student> studentList = new ArrayList<>();
studentList.add(student1);
studentList.add(student2);

List<String> bookList = studentList.stream()
.map(student -> student.getBookSet()) // Stream<Set<String>>
.flatMap(student -> student.stream()) // Stream<String>
.distinct().collect(Collectors.toList());

bookList.forEach(bookName -> System.out.println(bookName));
}

}
Output
Ruby in Action
Java in Action
Spring in Action
Learning Java Script

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

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

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/53e39342c1c41555c6e548783b3cf96ab7bd9ee6/BasicJava/StreamDemo_flatmap_streamofset/?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 to convert Stream of Set to Stream of String | Streams in Java

    Topic: How to use flatMapToInt method to convert Stream of intArray to IntStream | Streams in Java 8 Previous Topic   Next Topic Topic: How to use flatMap method of Java 8 streams | Streams in Java 8


    Sponsored Links



    Google
      Web Artima.com   

    Copyright © 1996-2017 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us