Conversion of Set To Stream in Java

Java Programming Java8Object Oriented Programming

Being a type of Collection, we can convert a set to Stream using its stream() method.

Example

Live Demo

import java.util.HashSet;
import java.util.Set;
import java.util.stream.Stream;

public class Tester {
   public static void main(String args[]) {
      Set<String> set = new HashSet<String>();

      set.add("a");
      set.add("b");
      set.add("c");
      set.add("d");
      set.add("e");
      set.add("f");

      Stream<String> stream = set.stream();
      stream.forEach(data->System.out.print(data+" "));
   }  
}

Output

a b c d e f
raja
Published on 23-Jul-2018 08:33:44
Advertisements