How to use Collectors in java8?

    When a java8 program runs in the stream, the ending process is with collectors. It shows the way to collect the data from stream and put it into a structure which stores the data. The structure is a list, a set or a map.

It collects the data, organize it and arrange the data according to the criteria given by the user.

Here, the list of collectors is given below.

  • 1.       List
  • 2.       Set
  • 3.       Map

Next, the java programs to implement these collectors are coded with simple examples.

1.Java Program to display a stream of data using List:

              This program creates a stream of data as a string. Create a list and store the data from the stream.

Print the list.

// Java Program to display a stream of data using List

import java.util.stream.Collectors;

import java.util.List;

import java.util.stream.Stream;

public class CollectorsEg {

public static void main(String[] args) {

Stream<String> stringSt = Stream.of("HTML", "JavaScript", "Web Server", "JScript");

List<String> sList = stringSt.collect(Collectors.toList());

System.out.println(sList);

}

}

When you execute this program, you will get the below output.

C:\raji\blog>javac CollectorsEg.java

C:\raji\blog>java CollectorsEg

[HTML, JavaScript, Web Server, JScript]

2.Java Program to display a stream of data using Set:

              A stream of data is collected as a set. print the set.

 // Java Program to display a stream of data using set

import java.util.stream.Collectors;

import java.util.Set;

import java.util.stream.Stream;

public class CollectorsEg1 {

public static void main(String[] args) {

Stream<String> stringSt = Stream.of("HTML", "JavaScript", "Web Server", "JScript");

Set<String> set = stringSt.collect(Collectors.toSet());

System.out.println(set);

}

}

The output is given below.

C:\raji\blog>javac CollectorsEg1.java

C:\raji\blog>java CollectorsEg1

[JScript, JavaScript, Web Server, HTML]

3.Java Program to display a stream of data using Map:

              This program collects the data as map. Here, creates a string with expansions. Next, convert it into map by using  toMap() function. Print the data.

// Java Program to display a stream of data using Map

import java.util.stream.Collectors;

import java.util.stream.Stream;

import java.util.*;

public class CollectorsEg2{

    public static void main(String[] args)

    {

      Stream<String[]>

            str1 = Stream

                      .of(new String[][] { { "HTML", "HyperText Markup Language" },

                                           { "CSS", "Cascading Stylesheets" },

                                           { "WWW", "World Wide Web" } });

          Map<String, String>

            map1 = str1.collect(Collectors.toMap(p -> p[0], p -> p[1]));

                 System.out.println("Map:" + map1);

    }

}

Compile and execute the program to get the output.

C:\raji\blog>javac CollectorsEg2.java

C:\raji\blog>java CollectorsEg2

Map:{CSS=Cascading Stylesheets, WWW=World Wide Web, HTML=HyperText Markup Language}

These are the ways to implement collectors in java8. Happy Coding!!!!

No comments:

Post a Comment