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 +
                '}';
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值