Java8 stream操作

这里记录一下java8的stream语法使用

 /**
 * @author fengzp
 * @date 2017/4/11下午4:49
 * @email fengzp@gzyitop.com
 * @company 广州易站通计算机科技有限公司
 */
public class SteamTest {

    public static void main(String[] args) {
        List<Person> list = new ArrayList<>();
        list.add(new Person("a", 5, Arrays.asList(new String[]{"1", "2"})));
        list.add(new Person("b", 8, Arrays.asList(new String[]{"3", "2"})));
        list.add(new Person("c", 6, Arrays.asList(new String[]{"4", "3"})));
        list.add(new Person("a", 7, Arrays.asList(new String[]{"4", "6"})));

        //查找第一个name="a"的person
        Optional<Person> a = list.stream().filter(person -> person.getName().equals("a")).findFirst();

        System.out.println(a.get().getAge());

        System.out.println("===============================1");

        //查找所有name="a"的person
        List<Person> result = list.stream().filter(person -> person.getName().equals("a")).collect(Collectors.toList());
        System.out.println(result.toString());

        System.out.println("===============================2");

        //把集合按name分组,Collectors.groupingBy(Person::getName)指按person的getName方法分组
        Map<String, List<Person>> map = list.stream().collect(Collectors.groupingBy(Person::getName));
        map.values().stream().forEach(list2 -> {
            System.out.println(list2.size() + " : " + list2);
        });

        System.out.println("===============================3");

        //查找所有person的所有tag
        Set<String> set = list.stream().flatMap(person -> person.getTags().stream()).collect(Collectors.toSet());
        System.out.println(set.toString());

        //map和flatMap的区别:map是接收值, flatMap是接收流
        set = list.stream().map(person -> person.getName()).collect(Collectors.toSet());
        System.out.println(set.toString());

        System.out.println("===============================4");

        //Stream.of()构造多个集合的流, reduce方法用来把集合的数据归纳在一起
        String reduce = Stream.of("1", "2", "3", "4").reduce("0", (s1, s2) -> s1 + "|" + s2);
        System.out.println(reduce);

        System.out.println("===============================5");

        //可使用mapToInt,mapToDouble,mapToLong来实现简单的统计
        //getMax(),getMin(),getSum(),getCount(),getAverage()
        System.out.println("max : " + list.stream().mapToInt(person -> person.getAge()).summaryStatistics().getMax());

        System.out.println("min : " + list.stream().flatMapToInt(person -> IntStream.of(person.getAge())).summaryStatistics().getMin());

        //并行计算 list.stream().parallel()
    }

    static class Person{
        String name;
        Integer age;
        List<String> tags;

        public Person(String name, Integer age) {
            this.name = name;
            this.age = age;
        }

        public Person(String name, Integer age, List<String> tags) {
            this.name = name;
            this.age = age;
            this.tags = tags;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public Integer getAge() {
            return age;
        }

        public void setAge(Integer age) {
            this.age = age;
        }

        public List<String> getTags() {
            return tags;
        }

        public void setTags(List<String> tags) {
            this.tags = tags;
        }

        @Override
        public String toString() {
            return "Person{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    ", tags=" + tags +
                    '}';
        }
    }
}
posted @ 2017-04-11 16:59  fengzp  阅读(1924)  评论(0编辑  收藏  举报