我是靠谱客的博主 不安花生,最近开发中收集的这篇文章主要介绍JAVA lambda使用总结(循环、过滤、排序、分组,内存分页),觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

package com.company;

import org.omg.Messaging.SyncScopeHelper;

import java.io.UnsupportedEncodingException;
import java.util.*;
import java.util.stream.Collectors;

public class Student {
    public static void main(String[] args) {
        // lambda 使用学习总结
        ArrayList<Student> studentList = new ArrayList<>();
        Random ageRandom = new Random(25);
        Random sexRandom = new Random(25);
        for (int i = 0; i < 100; i++) {
            Student student = new Student();
            student.setAge(ageRandom.nextInt(25));
            student.setBirthday(new Date(new Random().nextInt()));
            student.setSex(sexRandom.nextInt(2));
            student.setName(getRandomJianHan(3));
            System.out.println(student);
            studentList.add(student);
        }
        //foreach循环
        studentList.forEach(c-> {
            c.setAge(c.getAge()+1);
            System.out.println(c.toString());
        });
        //filter过滤
        List<Student> filterList = studentList.stream().filter(c -> c.age > 20).collect(Collectors.toList());
        System.out.println(filterList);

        //sorted排序
        List<Student> sortedList = studentList.stream().sorted(Comparator.comparing(Student::getAge)).collect(Collectors.toList());
        System.out.println(sortedList);
        //list分组
        Map<String, List<Student>> collect = studentList.stream().collect(Collectors.groupingBy(Student::getName));
        //排序+内存分页
        List<Student> collect1 = studentList.stream()
                .sorted(Comparator.comparing(o -> new Double(o.getAge())))
                .skip(0 * 10)
                .limit(10).collect(Collectors.toList());
        System.out.println("ss"+collect1);
        System.out.println("ss"+collect1.size());
    }


    public static String getRandomJianHan(int len) {
        String ret = "";
        for (int i = 0; i < len; i++) {
            String str = null;
            int hightPos, lowPos; // 定义高低位
            Random random = new Random();
            hightPos = (176 + Math.abs(random.nextInt(39))); // 获取高位值
            lowPos = (161 + Math.abs(random.nextInt(93))); // 获取低位值
            byte[] b = new byte[2];
            b[0] = (new Integer(hightPos).byteValue());
            b[1] = (new Integer(lowPos).byteValue());
            try {
                str = new String(b, "GBK"); // 转成中文
            } catch (UnsupportedEncodingException ex) {
                ex.printStackTrace();
            }
            ret += str;
        }
        return ret;
    }

    private String name;
    private Integer age;
    private Integer sex;
    private Date birthday;

    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 Integer getSex() {
        return sex;
    }

    public void setSex(Integer sex) {
        this.sex = sex;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + ''' +
                ", age=" + age +
                ", sex=" + sex +
                ", birthday=" + birthday +
                '}';
    }
}

最后

以上就是不安花生为你收集整理的JAVA lambda使用总结(循环、过滤、排序、分组,内存分页)的全部内容,希望文章能够帮你解决JAVA lambda使用总结(循环、过滤、排序、分组,内存分页)所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(41)

评论列表共有 0 条评论

立即
投稿
返回
顶部