package org.yoking.stream;

import com.google.common.collect.Lists;
import org.yoking.utils.RandomUtils;
import org.yoking.vo.Gender;
import org.yoking.vo.User;

import java.util.*;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
 * Created by zhangyuqing on 2017/3/20.
 */
public class StreamTest {

    public static List<User> randomUsers(int number) {
        List<User> users = Lists.newArrayList();

        Random r = new Random();
        for (long i = 1; i <= number; i++) {
            users.add(new User(i, RandomUtils.randomEmail(8), RandomUtils.randomName(3, 5), null, Gender.of(r.nextInt(2))));
        }
        return users;
    }

    public static void streamFilter() {
        List<User> users = randomUsers(5);
        System.out.println("Original users:" + users.toString());

        List<User> filteredUsers = new ArrayList<>();
        users.stream().filter(u -> u.getGender() == Gender.FEMALE).forEach(filteredUsers::add);
        System.out.println("Filtered users:" + filteredUsers.toString());
    }

    public static void reduceStringList() {
        List<String> list = Lists.newArrayList("YW-A3389-D.iqiyi.pps", "20170320", "1610", "10.1.33.116");
        Optional<String> optional = list.stream().reduce((s1, s2) -> s1 + "#" + s2);
        optional.ifPresent(System.out::println);
    }

    public static void main(String[] args) {
        streamFilter();
        reduceStringList();


        System.out.println("Sum: " + Lists.newArrayList(1, 1, null, 2, 3, 4, null, 5, 6, 7, 8, 9, 10).stream().filter(num -> num != null).
                distinct().mapToInt(num -> num * 2).
                peek(System.out::println).skip(2).limit(4).sum());
    }

}

@@ -21,16 +21,55 @@
 tream;%0A%0A
+import com.google.common.base.Strings;%0A
 import c
@@ -286,16 +286,48 @@
 lectors;
+%0Aimport java.util.stream.Stream;
 %0A%0A/**%0A *
@@ -1769,21 +1769,233 @@
 (4).sum());%0A
+%0A%0A        String%5B%5D array = %7B%22a%22, %22b%22, %22c%22%7D;%0A        for (Integer i : Lists.newArrayList(1, 2, 3)) %7B%0A            Stream.of(array).map(item -%3E Strings.padEnd(item, i, '@')).forEach(System.out::println);%0A        %7D%0A%0A
     %7D%0A%0A%7D%0A

add a function example