在数组列表或列表中过滤自定义对象 [英] Filter Custom Objects in Array list or List

查看:80
本文介绍了在数组列表或列表中过滤自定义对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在数组List中有50个自定义类(类名:Student)对象.

I have 50 Custom Class(class name : Student) objects in array List .

public class Student {

    private String name;
    private String age;
    private String parent;


    public String getName() {
        return name;
    }

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

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public String getParent() {
        return parent;
    }

    public void setParent(String parent) {
        this.parent = parent;
    }
}

我想过滤年龄为20岁的学生数组列表,并将其存储到另一个数组列表中.

I want to filter the students array list whose age is 20 and store it into another array list.

注意:

我尝试过for循环.它的工作正常.但是我想知道,除了循环数组列表以筛选学生年龄之外,还有没有其他最快的方法.

I have tried with for loop. its working fine. But I want to know Is there any fastest way other than looping array list to filter student age.

我有一些iOS经验.在iOS中,NSPredicate可以从NSArray过滤学生年龄.我从上个月开始才开始学习android.除了在互联网上循环数组列表以达到相同目的外,我没有其他答案.

I have some experience in iOS. In iOS, NSPredicate is there to filter student age from NSArray. I just started learning android since last month. I am not getting any answer other than looping array list over internet to achieve the same.

推荐答案

您可以使用 retrolambda 库获取 Java 8流类似的功能:

You can use retrolambda library to get Java 8 stream similar functions:

List<Student> filteredList = StreamSupport
            .stream(yourInputStudentList)
            .filter(item -> item.getAge() == "20")
            .collect(Collectors.toList());

为此,您需要添加gradle文件,如下所示:

To do so, you need add update your gradle file like below:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.4.0'
        classpath 'me.tatarka:gradle-retrolambda:3.2.0' // Add this         
    }
}

这篇关于在数组列表或列表中过滤自定义对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆