一、Jdk8stream操作

package com.hf.mypractice.Javabase;
 
import com.hf.mypractice.model.User;
 
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
 
/**
 * @Description: jdk1.8 stream
 * @Date: 2019/1/31
 * @Auther: 
 */
public class Jdk8stream {
 
    /**
     * 获取stream的方式:
     *     // 1、数组
     *     String[] arr = new String[]{"ab", "cd", "ef"};
     *     Stream<String> arrStream = Arrays.stream(arr);
     *     // 2、集合
     *     List<String> list = Arrays.asList("ab", "cd", "ef");
     *     Stream<String> colStream = list.stream();
     *     // 3、值
     *     Stream<String> stream = Stream.of("ab", "cd", "ef");
     * @param args
     */
    public static void main(String[] args) {
 
        List<User> list = Arrays.asList(
                new User("tom", 13),
                new User("jack", 12),
                new User("huahua", 18),
                new User("huahua", 18),
                new User("huahua", 30)
        );
 
        /**遍历集合**/
        list.stream().forEach(user -> {
            System.out.println("name:" + user.getName() + ",age:" + user.getAge());
        });
 
        /**遍历数组**/
        String[] arr = new String[]{"ab", "cd", "ef","ef"};
        Stream<String> arrStream = Arrays.stream(arr);
        arrStream.forEach(value -> {
            System.out.println("value:" + value);
        });
 
        /**遍历值**/
        Stream.of(1,2,3,4,5).forEach(i -> {
            System.out.println("i:" + i);
        });
 
        /**排序操作**/
        /**1.正常的lambada操作**/
        Collections.sort(list,(user1,user2) -> {
            return user1.getAge() - user2.getAge();
        });
 
        list.forEach(user -> {
            System.out.println("排序后name:" + user.getName() + ",age:" + user.getAge() );
        });
 
        /**stram使用sorted()方法排序数据**/
        /**指定字段自然排序,按照Comparator定义好的顺序**/
        list.stream().sorted(Comparator.comparing(User::getAge)).forEach(user -> {
            System.out.println("排序后name:" + user.getName() + ",age:" + user.getAge() );
        });
        /**指定字段逆序排序**/
        list.stream().sorted(Comparator.comparing(User::getAge).reversed()).forEach(user -> {
            System.out.println("逆序排序后name:" + user.getName() + ",age:" + user.getAge() );
        });
 
        /**指定字段自定义排序的方法**/
        list.stream().sorted(Comparator.comparing(User::getAge,(age1,age2) -> {
            return age2 - age1;
        })).forEach(user -> {
            System.out.println("what:" + user.toString());
        });
 
        /**filter():使用该方法过滤**/
        /**user.getAge() > 13 是过滤条件,相当于if()括号中的内容**/
        list.stream().filter((User user) -> user.getAge() > 12).forEach(user -> {
            System.out.println("filter -> name:" + user.getName() + ",age:" + user.getAge());
        });
 
        /**limit():使用该方法截断**/
        /**输出集合前2个元素**/
        list.stream().limit(2).forEach(user -> {
            System.out.println("limit -> name:" + user.getName() + ",age:" + user.getAge());
        });
 
        /**skip():与limit互斥,使用该方法跳过元素,指跳过几个元素**/
        list.stream().skip(2).forEach(user -> {
            System.out.println("skip -> name:" + user.getName() + ",age:" + user.getAge());
        });
 
        /**distinct():使用该方法去重,注意:必须重写对应泛型的hashCode()和equals()方法**/
        Arrays.stream(arr).distinct().forEach(value -> {
            System.out.println("after distinct:" + value);
        });
        list.stream().distinct().forEach(user -> {
            System.out.println("distinct -> name:" + user.getName() + ",age:" + user.getAge());
        });
 
 
     /***根据上述方法,完成去重+按照年龄大于15以后从小到大+只取前二**/
     list.stream().distinct().filter((User user) -> user.getAge() > 15).sorted(Comparator.comparing(User::getAge,(age1,age2) -> {
         return age1 - age2;
     })).limit(2).forEach(user -> {
         System.out.println("输出的结果是name:" + user.getName() + ",age:" + user.getAge());
     });
 
 
     /**对一个集合中去掉所有的奇数**/
        List<Integer> numList = new ArrayList<Integer>() {{
            for (int i = 0; i < 10; i++) {
                add(i);
            }
        }};
        //注意:过滤条件中的表示是要保存的
        numList = numList.stream().filter(num -> (num % 2 == 0)).collect(Collectors.toList());
        numList.forEach(num -> {
            System.out.println("偶数为:" + num);
        });
        /**max,min,sum,avg,count **/
        IntSummaryStatistics statistics = list.stream().mapToInt(user -> user.getAge()).summaryStatistics();
        System.out.println("最大的:" + statistics.getMax());
        System.out.println("平均的:" + statistics.getAverage());
        System.out.println("最小的:" + statistics.getMin());
        System.out.println("总数:" + statistics.getCount());
        System.out.println("总和:" + statistics.getMax());
        
        /**获取第一个元素**/
        User user = list.stream().findFirst().get();
        System.out.println("第一个元素:" + user.toString());
    }
}

转自: https://blog.csdn.net/chenhao_c_h/article/details/80691284

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐