java List 操作 记录(过滤,截取,去重)

	/**
     * 过滤
     */
    public static void forListTest(){

        List<Pension<String,Integer>> list = new ArrayList<>();
        Pension pension = new Pension("小红", 25);
        Pension pension2 = new Pension("小红dfssdf", 20);
        Pension pension3 = new Pension("小红dfs", 21);
        Pension pension4 = new Pension<>("小sdf红", 20);
        Pension pension5 = new Pension("小sdf红", 25);
        Pension pension6 = new Pension("小红", 20);
        list.add(pension);
        list.add(pension2);
        list.add(pension5);
        list.add(pension3);
        list.add(pension6);
        list.add(pension3);
        list.add(pension6);
        list.add(pension4);
        list.add(pension);
        list.add(pension2);
        list.add(pension5);
        list.add(pension3);
        list.add(pension6);
        list.add(pension6);


        list = list.stream().filter((Pension<String,Integer> p)->p.getAge()>20&&!p.getName().equals("小红")).collect(Collectors.toList());

        List<Pension<String,Integer>> resultList = new ArrayList<>();
        for (Pension<String,Integer> p:list){
            if (p.getAge()>20&&!p.getName().equals("小红")){
                resultList.add(p);
            }
        }


        resultList.stream().forEach((Pension<String,Integer> p)->{
            System.out.println(p.getName());
        });
        System.out.println(resultList.size());
    }

    /**
     * 截取
     */
    public static void subListTest(){

        List list = new ArrayList<>();
        Pension pension = new Pension("小红", 25);
        Pension pension2 = new Pension("小红dfssdf", 20);
        Pension pension3 = new Pension("小红dfs", 21);
        Pension pension4 = new Pension<>("小sdf红", 20);
        Pension pension5 = new Pension("小sdf红", 25);
        Pension pension6 = new Pension("小红", 20);
        list.add(pension);
        list.add(pension2);
        list.add(pension5);
        list.add(pension3);
        list.add(pension6);
        list.add(pension3);
        list.add(pension6);
        list.add(pension4);
        list.add(pension5);
        list.add(pension);
        list.add(pension2);
        list.add(pension5);
        list.add(pension3);
        list.add(pension6);
        list.add(pension6);

        int num = 5;
        int size = list.size();
        int count = (size/num)+1;

        for (int i=0;i<count;i++){
            int from = i*num;
            int to = (i+1)*num;
            to = to>size?size:to;
            if (from==to) break;
            List list1 = list.subList(from,to);
            System.out.println(list1.size());
        }
    }

    /**
     * 去重
     */
    public static void testquchong() {
        List<Pension> list = new ArrayList<>();
        Pension pension = new Pension("小红", 25);
        Pension pension2 = new Pension("小红dfssdf", 20);
        Pension pension3 = new Pension("小红dfs", 21);
        Pension<String, Integer> pension4 = new Pension<>("小sdf红", 20);
        Pension pension5 = new Pension("小sdf红", 25);
        Pension pension6 = new Pension("小红", 20);
        list.add(pension);
        list.add(pension2);
        list.add(pension5);
        list.add(pension3);
        list.add(pension6);
        list.add(pension3);
        list.add(pension6);
        list.add(pension4);
        list.add(pension5);
        //把自己添加进去
        list.addAll(list);
        // 去重
        Set<Pension> set = new TreeSet<>(Comparator.comparing(Pension<String, Integer>::getAge));
        set.addAll(list);
        list = new ArrayList<>(set);
        System.out.println(list.size());


        Map<Object, Object> map = new HashMap();
        List<Pension> list2 = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            if (map.get(list.get(i).getAge()) == null) {
                list2.add(list.get(i));
                map.put(list.get(i).getAge(), "true");
            }
        }
        System.out.println(list2.size());
    }

class Pension<E, T> {
    private E name;
    private T age;

    public Pension() {
    }

    public Pension(E name, T age) {
        this.name = name;
        this.age = age;
    }

    public E getName() {
        return name;
    }

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

    public T getAge() {
        return age;
    }

    public void setAge(T age) {
        this.age = age;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值