Wednesday 26 July 2017

How to filter the names whose length is greater than 4 using Java 8 streams | Streams in Java 8


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

StreamDemo.java
import java.util.Arrays;
import java.util.stream.Stream;

/**
 * If you have an array of strings, and you want to create a subset of
 * it which contains only those strings whose length is more than four
 * characters,then use below program.
 * 
 * How to filter the names whose length is greater than 4?
 */
public class StreamDemo
{
    public static void main(String[] args)
    {
        String[] nameArray = new String[] { "Peter", "Steve", "paul","Ram" };

        Stream<String> stream = Arrays.stream(nameArray);

        /*
         * The filter method expects a lambda expression as its
         * argument. However, the lambda expression passed to it must
         * always return a boolean value, which determines whether or
         * not the processed element should belong to the resulting
         * Stream object.
         */
        Stream<String> filteredStream = stream.filter(name -> name.length() > 4);

        String[] filteredNameArray = filteredStream.toArray(String[]::new);

        for (String name : filteredNameArray)
        {
            System.out.println(name);
        }

    }
}
Output
Peter
Steve

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

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

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

    1. A easy and exciting blog about java learning. Please comment your opinions and share..
      http://foundjava.blogspot.in

      ReplyDelete
      Replies
      1. Great Article android based projects

        Java Training in Chennai Project Center in Chennai Java Training in Chennai projects for cse The Angular Training covers a wide range of topics including Components, Angular Directives, Angular Services, Pipes, security fundamentals, Routing, and Angular programmability. The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer. Angular Training Project Centers in Chennai

        Delete
    2. Interesting blog which attracted me more.Spend a worthful time.keep updating more.
      Informatica Training in Chennai

      ReplyDelete