Tuesday 26 September 2017

Grouping using Java 8 Streams without any loops | Java 8 streams | Streams in Java 8


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

Article.java
public class Article
{
    private String title;
    private String author;
    private String tagName;

    public Article(String title, String author, String tagName)
    {
        super();
        this.title = title;
        this.author = author;
        this.tagName = tagName;
    }

    public String getTitle()
    {
        return title;
    }

    public void setTitle(String title)
    {
        this.title = title;
    }

    public String getAuthor()
    {
        return author;
    }

    public void setAuthor(String author)
    {
        this.author = author;
    }

    public String getTagName()
    {
        return tagName;
    }

    public void setTagName(String tagName)
    {
        this.tagName = tagName;
    }

    @Override
    public String toString()
    {
        return "Article [title=" + title + ", author=" + author + ", tagName="
                + tagName + "]";
    }

}
StreamDemo1.java
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Group all the articles based on the author.
 */
public class StreamDemo1
{
    public static void main(String[] args)
    {
        List<Article> articleList = getArticleList();

        Map<String, List<Article>> groupByAuthorMap = groupByAuthorUsingForLoop(
                articleList);

        for (String key : groupByAuthorMap.keySet())
        {
            System.out.println(key + " = " + groupByAuthorMap.get(key));
        }

        System.out.println("-----------------------------");

        groupByAuthorMap = groupByAuthorUsingStream(articleList);
        for (String key : groupByAuthorMap.keySet())
        {
            System.out.println(key + " = " + groupByAuthorMap.get(key));
        }
    }

    private static Map<String, List<Article>> groupByAuthorUsingForLoop(
            List<Article> articleList)
    {
        /*
         * Before JDK8 - Using a for-loop
         */
        Map<String, List<Article>> groupByAuthorMap = new HashMap<>();

        for (Article article : articleList)
        {
            if (groupByAuthorMap.containsKey(article.getAuthor()))
            {
                groupByAuthorMap.get(article.getAuthor()).add(article);
            }
            else
            {
                ArrayList<Article> articles = new ArrayList<>();
                articles.add(article);
                groupByAuthorMap.put(article.getAuthor(), articles);
            }
        }

        return groupByAuthorMap;
    }

    private static Map<String, List<Article>> groupByAuthorUsingStream(
            List<Article> articleList)
    {
        /*
         * With JDK 8 - Using operations from the stream API
         */
        Map<String, List<Article>> groupByAuthorMap = articleList
                .stream()
                .collect(Collectors.groupingBy(Article::getAuthor));
        return groupByAuthorMap;
    }
    

    private static List<Article> getArticleList()
    {
        List<Article>  listOfArticle = new ArrayList<Article>();
        
        listOfArticle.add(new Article("Java complete Reference","John","Java"));
        listOfArticle.add( new Article("Java Programming","John","Java"));
        listOfArticle.add(new Article("RESTful web services","Peter","Web Service"));       
        listOfArticle.add(new Article("Programming Ruby","Peter","Ruby"));
        
        return listOfArticle;       
    }
}
Output
John = [Article [title=Java complete Reference, author=John, tagName=Java], 
        Article [title=Java Programming, author=John, tagName=Java]]
Peter = [Article [title=RESTful web services, author=Peter, tagName=Web Service], 
        Article [title=Programming Ruby, author=Peter, tagName=Ruby]]
-----------------------------
John = [Article [title=Java complete Reference, author=John, tagName=Java], 
       Article [title=Java Programming, author=John, tagName=Java]]
Peter = [Article [title=RESTful web services, author=Peter, tagName=Web Service], 
        Article [title=Programming Ruby, author=Peter, tagName=Ruby]]

StreamDemo2.java
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * Get DistinctTagNames in the collection.
 */
public class StreamDemo2
{
    public static void main(String[] args)
    {
        List<Article> articleList = getArticleList();
        Set<String> distinctTagNamesSet = getDistinctTagNamesUsingForLoop(articleList);     
        System.out.println(distinctTagNamesSet);
        
        System.out.println("------------------------------------------------------------------");
        
        distinctTagNamesSet = getDistinctTagNamesUsingStream(articleList);      
        System.out.println(distinctTagNamesSet);
    }
    
    
    private static Set<String> getDistinctTagNamesUsingForLoop(List<Article> articleList)
    {
        /*
         * Before JDK8 - Using a for-loop
         */
        Set<String> distinctTagNamesSet = new HashSet<>();

        for (Article article : articleList)
        {
            distinctTagNamesSet.add(article.getTagName());
        }

        return distinctTagNamesSet;
    }

    private static Set<String> getDistinctTagNamesUsingStream(List<Article> articleList)
    {
        /*
         * With JDK 8 - Using operations from the stream API
         */
        Set<String> distinctTagNamesSet = articleList.stream()
                .map(article -> article.getTagName()).distinct()
                .collect(Collectors.toSet());

        return distinctTagNamesSet;

    }
    

    private static List<Article> getArticleList()
    {
        List<Article>  listOfArticle = new ArrayList<Article>();
        
        listOfArticle.add(new Article("Java complete Reference","John","Java"));
        listOfArticle.add( new Article("Java Programming","John","Java"));
        listOfArticle.add(new Article("RESTful web services","John","Web Service"));        
        listOfArticle.add(new Article("Programming Ruby","John","Ruby"));
        
        return listOfArticle;       
    }

}
Output
[Java, Ruby, Web Service]
------------------------------------------------------------------
[Java, Ruby, Web Service]

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

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

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/5eb462211ac2fd9fb1bace2049e170a49c5caa69/BasicJava/StreamDemo_2_no_loop_Article_group/?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
  • No comments:

    Post a Comment