Streams Filter Example

Source: Internet
Author: User

in this tutorial, we will show you some Java 8 examples to illustrate stream filter (), collect () use, Findany () and OrElse ().
1. Streams filter () and collect ()

Before 1.1 Java 8, the filter List looks like this: Beforejava8.java

package Com.mkyong.java8;
Import java.util.ArrayList;
Import Java.util.Arrays;

Import java.util.List; public class BeforeJava8 {public static void main (string[] args) {list<string> lines = Arrays.aslist
        ("Spring", "Node", "Mkyong");
        list<string> result = Getfilteroutput (lines, "Mkyong");    for (String Temp:result) {System.out.println (temp); output:spring, node}} private static list<string> Getfilteroutput (list<string> lines,
        String Filter {list<string> result = new arraylist<> (); for (String line:lines) {if (!)
            Mkyong ". Equals (line)) {//We dont like mkyong result.add (line);
    } return result; }

}

Output

Spring
Node

1.2 In Java 8, an example of a stream equivalent. Filter () Filters the list, and collect () is converted to flow into one list. Nowjava8.java

Package com.mkyong.java8;

Import Java.util.Arrays;
Import java.util.List;
Import java.util.stream.Collectors;

public class NowJava8 {public

    static void Main (string[] args) {

        list<string> lines = arrays.aslist ("Spring "," Node "," Mkyong ");

        list<string> result = Lines.stream ()                //convert list to stream
                . Filter (line->!) Mkyong ". Equals (line))     //We dont like Mkyong
                . Collect (Collectors.tolist ());              Collect the output and convert streams to a List

        Result.foreach (system.out::p rintln);                output:spring, Node

    }

}

Output

Spring
Node
2. Streams filter (), Findany () and OrElse () Person.java
Package com.mkyong.java8;

public class Person {

    private String name;
    private int age;

    Public person (String name, int age) {
        this.name = name;
        This.age = age;
    }

    GETTERSM Setters, ToString
}

2.1 Before Java 8, you get a person object as shown in the following example: Beforejava8.java

Package com.mkyong.java8;

Import Java.util.Arrays;
Import java.util.List;

public class BeforeJava8 {public

    static void Main (string[] args) {

        list<person> persons = Arrays.aslist (
  new person ("Mkyong", "The New Person" ("
                Jack"), the new person (
                "Lawrence")
        ;

        Person result = getstudentbyname (persons, "Jack");
        SYSTEM.OUT.PRINTLN (result);

    }

    private static person Getstudentbyname (list<person> persons, String name) {person result

        = null;
        for [person Temp:persons] {
            if (name.equals (Temp.getname ())) {result
                = temp;
            }
        }
        return result;
    }

Output

person{name 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.