java8新特性之stream

/** 总结 java8新特性stream api对集合的各种操作  **/
package com.ldj.java8;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;


/**
* @Title: java8新特性之stream总结,类似jquery强大的链式写法
* @author  TODO
* @since   JDK1.6
* @history 2018年6月13日 TODO 新建
*/
public class Stream {


    public static void main(String[] args) {
        List<Student> students = new ArrayList<Student>();
        Student s1 = new Student(1100820317, "lidajiu", "lidajiu2010@163.com", 11008203, 25);
        Student s2 = new Student(1100820321, "qinge", "qinge2010@163.com", 11008203, 24);
        Student s3 = new Student(1100820314, "laijinsheng", "laijinsheng@163.com", 11008203, 26);
        Student s4 = new Student(1100820412, "huangjian", "huangjian@163.com", 11008204, 20);
        Student s5 = new Student(1100820415, "lizipeng", "lizipeng@163.com", 11008204, 17);
        
        students.add(s1);
        students.add(s2);
        students.add(s3);
        students.add(s4);
        students.add(s5);
        
//        groupingBy(students);
//        toMap(students);
//        toMap2(students);
//        toList(students);
//        toSet(students);
//        forEach(students);
//        filter(students);
//        sorted(students);
//        allMatch(students);
//        anyMatch(students);
//        noneMatch(students);
//        count(students);
//        max(students);
//        sum(students);
//        skipAndLimit(students);
//        distinct(students);
        
        
        
    }
    
    /**
     * 分组groupingBy使用
     * @param alist
     */
    public static void groupingBy(List<Student> alist) {
        Map<Integer, List<Student>> result = alist.stream().collect(Collectors.groupingBy(Student::getCla));
        for(Map.Entry<Integer, List<Student>> entry :  result.entrySet()) {
            System.out.println(entry.getKey());
            for(Student s : entry.getValue()) {
                System.out.println(s.getId()+" "+s.getCla());
            }
        }
    }
    
    public static void par(List<Student> alist) {
//        alist.stream().collect(Collectors.partitioningBy(s -> s.));
    }
    
    /**
     * 将集合转换成map
     * @param alist
     */
    public static void toMap(List<Student> alist) {
        Map<Integer, String> result = alist.stream().collect(Collectors.toMap(Student::getId, Student::getName));
        for(Map.Entry<Integer, String>entry : result.entrySet()) {
            System.out.println(entry.getKey()+" "+entry.getValue());
        }
    }
    
    public static void toMap2(List<Student> alist) {
        Map<Integer, Student> result = alist.stream().collect(Collectors.toMap(Student::getId, s ->s));
        for(Map.Entry<Integer, Student> entry : result.entrySet()) {
            System.out.println(entry.getKey()+" "+entry.getValue().getName());
        }
    }
    
    /**
     * 抽取集合中学生的id
     * @param alist
     */
    public static void toList(List<Student> alist){
        List<Integer> ids = alist.stream().map(s -> s.getId()).collect(Collectors.toList());
        for(Integer id : ids) {
            System.out.print(id+" ");
        }
    }
    
    /**
     * toSet使用方法
     * @param alist
     */
    public static void toSet(List<Student> alist) {
        Set<Integer> sets = alist.stream().map(Student::getCla).collect(Collectors.toSet());
        for(Integer i : sets) {
            System.out.print(i+" ");
        }
    }
    
    /**
     * forEach操作
     * @param alist
     */
    public static void forEach(List<Student> alist) {
        alist.stream().forEach(s ->System.out.println(s.getName()));
    }
    
    /**
     * filter
     * @param alist
     */
    public static void filter(List<Student> alist) {
        List<Student> result = alist.stream().filter(s -> (s.getAge() > 18 && s.getAge() < 26)).collect(Collectors.toList());
        for(Student s : result) {
            System.out.println(s.getName()+" "+s.getAge());
        }
    }
    
    /**
     * sorted操作 (排序后原数据源alist是不会被修改的)
     * @param alist
     */
    public static void sorted(List<Student> alist) {
        List<Student> result = alist.stream().sorted((p1, p2) -> p1.getAge() - p2.getAge() ).collect(Collectors.toList());
        for(Student s : result){
            System.out.println(s.getName()+" "+s.getAge());
        }
    }
    
    /**
     * allMatch所有都匹配成功就返回true, 否则返回false
     * @param alist
     */
    public static void allMatch(List<Student> alist) {
        boolean result  = alist.stream().allMatch(s -> s.getAge() > 18);
        System.out.println(result);
    }
    
    /**
     * anyMatch  只要有一个匹配多久返回true,否则返回false
     * @param alist
     */
    public static void anyMatch(List<Student> alist) {
        boolean result = alist.stream().anyMatch(s -> s.getAge() > 18);
        System.out.println(result);
    }
    
    /**
     * noneMatch 没有一个匹配就返回true, 否则返回false
     * @param alist
     */
    public static void noneMatch(List<Student> alist) {
        boolean result = alist.stream().noneMatch(s -> s.getAge() > 18);
        System.out.println(result);
    }
    
    /**
     * 计数操作,计算年龄小于18的人的个数
     * @param alist
     */
    public static void count(List<Student> alist) {
        long count = alist.stream().filter(s -> s.getAge() < 18).count();
        System.out.println(count);
    }
    
    /**
     * 查找出最大的年龄
     * @param alist
     */
    public static void max(List<Student> alist) {
        int maxAge = alist.stream().mapToInt(s -> s.getAge()).max().getAsInt();
        System.out.println(maxAge);
    }
    
    /**
     * sum求和
     * @param alist
     */
    public static void sum(List<Student> alist) {
        int sum = alist.stream().filter(s -> s.getAge() > 18 ).mapToInt(s -> s.getAge()).sum();
        System.out.println(sum);
    }
    
    /**
     * limit 返回 Stream 的前面 n 个元素;skip 则是跳过前 n 个元素只要后面的元素 
     * @param alist
     */
    public static void skipAndLimit(List<Student> alist) {
        List<Student> result = alist.stream().skip(1).limit(2).collect(Collectors.toList());
        for(Student s : result) {
            System.out.println(s.getName());
        }
    }
    
    /**
     * distinct
     * @param alist
     */
    public static void distinct(List<Student> alist) {
        List<Integer> result = alist.stream().map(s -> s.getAge()).distinct().collect(Collectors.toList());
        for(Integer i : result) {
            System.out.println(i);
        }
    }
    
    
    


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值