Java8 stream练习

package test;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author Jaye
 * @date 2019/4/8 15:56
 */
public class Java8Test {


    public static void main(String[] args) {
//        Comparator comparable = (p1, p2) -> p1.firstName.compareTo(p2.firstName);
//        Person p1 = new Person("John", "Doe");
//        Person p2 = new Person("Alice", "Wonderland");
//
//        int cp = comparable.compare(p1,p2);
//        System.out.println(cp); //>0


//        Optional optional = Optional.of("Jw");
//        System.out.println(optional.isPresent());
//        optional.get();
//        optional.orElse("fallback");

        List stringCollection = new ArrayList<>();
        stringCollection.add("ddd2");
        stringCollection.add("aaa2");
        stringCollection.add("bbb1");
        stringCollection.add("aaa1");
        stringCollection.add("bbb3");
        stringCollection.add("ccc");
        stringCollection.add("bbb2");
        stringCollection.add("ddd1");

        //filter
//        stringCollection.stream().filter(string -> string.startsWith("a")).forEach(System.out::println);

        //sorted
//        stringCollection.stream().sorted((a1,a2)->a2.compareTo(a1)).forEach(System.out::println);

        //Match
//        boolean flag = stringCollection.stream().anyMatch(string -> string.startsWith("a"));
//        boolean flag1 = stringCollection.stream().allMatch(string -> string.length() > 0);
//        boolean flag2 = stringCollection.stream().noneMatch(string -> string.endsWith("GGG"));
//        System.out.println("flag:" + flag + ";flag1:" + flag1 + ";flag2:" + flag2); //flag:true;flag1:true;flag2:true

        //Map
//        stringCollection.stream().map(string->string.toCharArray()).forEach((char[] detail) -> {for (char c : detail) {
//            System.out.println(c);
//        }
//        });

//        d
//        d
//        d
//        2
//        a
//        a
//        a
//        2

        //count
//        long num = stringCollection.stream().filter(s -> s.startsWith("a") || s.startsWith("b")).count();
//        System.out.println(num);


        //reduce
//        Optional optional = stringCollection.stream().reduce((s1, s2) -> new StringBuilder(s1).append("、").append(new StringBuilder(s2)).toString());
//        System.out.println(optional.get()); //ddd2、aaa2、bbb1、aaa1、bbb3、ccc、bbb2、ddd1


        //collect
        stringCollection = stringCollection.stream().filter(s -> s.startsWith("a")).collect(Collectors.toList());
        Optional optionalS = stringCollection.stream().reduce((s1, s2) -> s1 + "," + s2);
        System.out.println(optionalS.get());


//        map 是不支持流操作的。Map接口本身没有可用的stream()方法,
//        但是你可以根据键-值对或项通过map.keySet().stream,map.values().stream()和map.entrySet().stream()来创建指定的流。
//        Map map = new HashMap<>();
//        map.putIfAbsent("city", "HA");
//        map.putIfAbsent("name", "JW");

//        map.keySet().stream().forEach(key -> System.out.println(map.get(key)));
//            HA
//           JW

//        map.values().stream().forEach(System.out::println);
        //            HA
//           JW
    }


}

class Person {
    String firstName;
    String secondName;

    public Person(String firstName, String secondName) {
        this.firstName = firstName;
        this.secondName = secondName;
    }
}

你可能感兴趣的:(Java8 stream练习)