Java8 Flatmap Demo code can be run directly __java

Source: Internet
Author: User
 reference from: http://www.cnblogs.com/shenlanzhizun/p/6027042.html 
Package com.by.interfaces;
Import java.util.ArrayList;
Import Java.util.Arrays;
Import java.util.List;

Import java.util.stream.Collectors; Class Student {public Student (long ID, string name, int age, int grade, string major, String school) {this.
        id = ID;
        THIS.name = name;
        This.age = age;
        This.grade = grade;
        This.major = major;
    This.school = School;
    Public Student (String name) {this.name = name;

    Public Student () {} private list<string> attributes = Arrays.aslist ("111", "222", "333");

    /** * Learning number/private long ID;

    private String name;

    private int age;

    /** * Grade * * private int grade;

    /** * Professional * * Private String major;

    /** * School * * Private String School;
    Public long GetId () {return id;
    public void SetId (long id) {this.id = ID; Public String GetName () {REturn name;
    public void SetName (String name) {this.name = name;
    public int getage () {return age;
    public void Setage (int age) {this.age = age;
    public int Getgrade () {return grade;
    The public void Setgrade (int grade) {This.grade = grade;
    Public String Getmajor () {return major;
    } public void Setmajor (String major) {this.major = major;
    Public String Getschool () {return school;
    } public void Setschool (String school) {This.school = school;
    Public list<string> getattributes () {return attributes;
    public void SetAttributes (list<string> attributes) {this.attributes = attributes; } public class Main {public static void main (string[] args) {list<student> students = new Arraylis

        T<student> (); Students.add (New Student (20160001, "Hung Ming 4", 20, 1, "Civil Engineering", "Wuhan University");
        Students.add (New Student (20160002, "Bobbie", 21, 2, "Information security", "Wuhan University"));
        Students.add (New Student (20160003, "Hyun Tak", 22, 3, "economic management", "Wuhan University"));
        Students.add (New Student (20160004, "Cloud Long", 21, 2, "Information security", "Wuhan University"));
        Students.add (New Student (20161001, "Wing de", 21, 2, "Mechanical and automation", "Hust"));
        Students.add (New Student (20161002, "Yuan Zhi", 23, 4, "Civil Engineering", "Hust"));
        Students.add (New Student (20161003, "filial piety", 23, 4, "Computer Science", "Hust"));
        Students.add (New Student (20162001, "Zhongmo", 22, 3, "Civil Engineering", "Zhejiang University"));
        Students.add (New Student (20162002, "Lu Su", 23, 4, "Computer Science", "Zhejiang University"));

        Students.add (New Student (20163001, "Ding Bong", 24, 5, "Civil Engineering", "Nanjing University")); Maptolong use demo Double value = Students.stream (). Filter (Student-> "Computer Science". Equals (Student.getmajor ()). Mapto


        Long (AAA-> Aaa.getid ()). sum ();


        String[] STRs = {' Java8 ', ' is ', ' easy ', ' to ', ' use '};
      list<string[]> distinctstrs = Arrays.stream (STRs)          . map (str-> str.split (""))//mapping becomes stream<string[]>. DISTINCT (). Collec

        T (Collectors.tolist ());


        Distinctstrs.foreach (Strings-> Arrays.stream (strings). ForEach (S-> System.out.println (s)));

        System.out.println ("==================================map Way"); list<string> Distinctstrsbyflatmap = Arrays.stream (STRs). Map (str-> str.split (""))//mapping becomes Strea
                M<string[]>. Flatmap (Arrays::stream)//flattened to stream<string>. Distinct ()

        . Collect (Collectors.tolist ());

        Distinctstrsbyflatmap.foreach (S-> System.out.println (s));


        System.out.println ("==================================flatmap Way");
        list<list<student>> studentss = new arraylist<> ();
        Studentss.add (students);
        Studentss.add (students);

        System.out.println (Studentss.size ()); System.out.println ("Through the Flatmap place.After the stream to see what size is "+ studentss.stream (). Flatmap (Students1-> students1.stream ()). Count ()); System.out.println (Studentss.stream (). Flatmap (Students1-> students1.stream ()). Max (O1, O2)-> (O1.getname ().


        Length ()-o2.getname (). Length ())-get (). GetName ()); System.out.println ("After flatmap a thorough flow process to see how many streams" + studentss.stream (). Flatmap (Students1-> students1.stream ()).


        Flatmap (Student-> student.getattributes (). Stream ()). Count ()); Studentss.stream (). Flatmap (Students1-> students1.stream ()). Flatmap (Student-> ().


        

    Stream ()). ForEach (S-> System.out.println (s));
 }
}

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.