Tuesday 26 September 2017

Java 8 no more loops using Java 8 Streams | Java 8 streams | Streams in Java 8


Click here to watch in Youtube :
https://www.youtube.com/watch?v=5hreHBd5Xkg&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.List;
import java.util.Optional;

/**
 * In Java 8 No more loops:
 * 
 * Find the first article in the collection that has the tagName “Java”.
 */
public class StreamDemo1
{
    public static void main(String[] args)
    {
        List<Article> articleList = getArticleList();
        
        
        Article article = getFirstArticleUsingForLoop(articleList);
        System.out.println(article);
        
        System.out.println("------------------------------------------------------------------");
        
        article = getFirstArticleUsingStream(articleList);
        System.out.println(article);
    }

    private static Article getFirstArticleUsingForLoop(List<Article> articleList)
    {
        /*
         * Before JDK8 - Using a for-loop
         */
        for (Article article : articleList)
        {
            if (article.getTagName().contains("Java"))
            {
                return article;
            }
        }
        return null;
    }

    private static Article getFirstArticleUsingStream(List<Article> articleList)
    {
        /*
         * With JDK 8 - Using operations from the stream API
         */
        Optional<Article> optional = articleList.stream()
                .filter(article -> article.getTagName().contains("Java"))
                .findFirst();

        Article article = optional.get();
        return article;
    }
    

    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
Article [title=Java complete Reference, author=John, tagName=Java]
------------------------------------------------------------------
Article [title=Java complete Reference, author=John, tagName=Java]

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

/**
 * Get all articles in the collection that has the tagName “Java”.
 */
public class StreamDemo2
{
    public static void main(String[] args)
    {
        List<Article> articleList = getArticleList();
        List<Article> javaArticleList = getAllArticleUsingForLoop(articleList);     
        System.out.println(javaArticleList);
        
        System.out.println("------------------------------------------------------------------");
        
        /*
         * With JDK 8 - Using operations from the stream API
         */
        javaArticleList = getAllArticleUsingStream(articleList);    
        System.out.println(javaArticleList);
    }
    
    
    private static List<Article> getAllArticleUsingForLoop(List<Article> articleList)
    {
        /*
         * Before JDK8 - Using a for-loop
         */
        List<Article> javaArticleList = new ArrayList<Article>(); 
        for (Article article : articleList)
        {
            if (article.getTagName().contains("Java"))
            {
                javaArticleList.add(article);
            }
        }
        return javaArticleList;
    }

    private static List<Article> getAllArticleUsingStream(List<Article> articleList)
    {
        /*
         * With JDK 8 - Using operations from the stream API
         */
        List<Article> javaArticleList = articleList.stream()
                .filter(article -> article.getTagName().contains("Java"))
                .collect(Collectors.toList());      
        
        return javaArticleList;

    }
    

    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
[Article [title=Java complete Reference, author=John, tagName=Java], 
Article [title=Java Programming, author=John, tagName=Java]]
------------------------------------------------------------------
[Article [title=Java complete Reference, author=John, tagName=Java], 
Article [title=Java Programming, author=John, tagName=Java]]

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

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

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