package com.expgiga.Java8;

/**
 *
 */
public class Employee {
    private String name;
    private int age;
    private double salary;
    private int id;

    public Employee() {
    }

    public Employee(String name, int age, double salary) {
        this.name = name;
        this.age = age;
        this.salary = salary;
    }

    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 double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    @Override
    public String toString() {
        return "Employee{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", salary=" + salary +
                '}';
    }

    public Employee(int id) {
        this.id = id;
    }

    public Employee(int id, int age) {
        this.id = id;
        this.age = age;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Employee employee = (Employee) o;

        if (age != employee.age) return false;
        if (Double.compare(employee.salary, salary) != 0) return false;
        if (id != employee.id) return false;
        return name != null ? name.equals(employee.name) : employee.name == null;
    }

    @Override
    public int hashCode() {
        int result;
        long temp;
        result = name != null ? name.hashCode() : 0;
        result = 31 * result + age;
        temp = Double.doubleToLongBits(salary);
        result = 31 * result + (int) (temp ^ (temp >>> 32));
        result = 31 * result + id;
        return result;
    }
}

package com.expgiga.Java8;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Stream;

/**
 * Java8 Stream
 *
 * 一、Stream的三个操作步骤:
 * 1. 创建Stream
 * 2. 中间操作
 * 3. 终止操作(终端操作)
 */

public class TestStream2 {
    public static void main(String[] args) {
        /*
         * 中间操作:
         * filter:接受Lambda,从流中排出某些元素。
         * limit:截断流,使其元素不超过给定数量
         * skip:跳过元素,返回一个扔掉了前n个元素的流。若流中元素不足n个,则返回一个空流。与Limit(n)互补
         * distinct:筛选,通过流所生成元素的hashCode()equals()去除重复元素
         *
         */
        //内部迭代:迭代操作由Stream API完成
        List<Employee> employeeList = Arrays.asList(
                new Employee("zhangsan", 18, 19999),
                new Employee("lisi", 28, 29999),
                new Employee("wangwu", 38, 39999),
                new Employee("zhaoliu", 16, 17999),
                new Employee("tianqi", 6, 12999),
                new Employee("tianqi", 6, 12999),
                new Employee("tianqi", 6, 12999)
        );

        /*
         * 映射
         * map
         * flatMap
         */
        //map
        List<String> list = Arrays.asList("aa", "bb", "cc", "dd", "ee");
        list.stream()
                .map((str) -> str.toUpperCase())
                .forEach(System.out::println);

        employeeList.stream()
                .map(Employee::getName)
                .forEach(System.out::println);

        //
        Stream<Stream<Character>> streamStream =  list.stream()
                .map(TestStream2::filterCharacter);
        streamStream.forEach((sm) -> {
            sm.forEach(System.out::println); //{{a,a},{b,b},{c,c},{d,d},{e,e}}
        });

        //flatMap
        list.stream()
                .flatMap(TestStream2::filterCharacter)
                .forEach(System.out::println);//{a,a,b,b,c,c,d,d,e,e}

        //对比add(Object obj) addAll(Collection coll)
        List list2 = new ArrayList<>();
        list2.add(11);
        list2.add(22);
        list2.add(33);
        list2.add(list);
        System.out.println(list2);
        list2.addAll(list);
        System.out.println(list2);
    }

    private static Stream<Character> filterCharacter(String str) {
        List<Character> list = new ArrayList<>();
        for (Character ch : str.toCharArray()) {
            list.add(ch);
        }
        return list.stream();
    }
}