【问题标题】:How to fetched filtered data from array list?如何从arraylist中获取过滤后的数据?
【发布时间】:2016-03-25 03:10:54
【问题描述】:

我有两个ArrayList 一个列表是自定义class 列表,另一个是Integer 列表。 Integer 列表包含 id,我想从自定义类列表中获取数据,其 id 在Integer 列表中可用。并且两个列表都有 1000 条记录。

所以请告诉我优化的方法

【问题讨论】:

  • 在这里发布您的自定义班级列表,我们可以更好地了解。

标签: android list filter custom-lists


【解决方案1】:

此代码将让您大致了解您可能要考虑的方法,此示例使用ArrayList 搜索列表中的数据,

我认为很少有像 CustomClass 这样的东西,因为我不知道它是什么。

即使此代码有效,您也应该考虑使用 HashMap 的代码,它会比这快得多

import java.util.ArrayList;

public class FindTest {

    public static void main(String[] args) {

        /**
         * this initializes you integer list
         * */
        ArrayList<Integer> integers = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            integers.add(i);
        }

        /**
         * this initializes you MyCustomeData list
         * */
        ArrayList<MyCustomeData> customeDatas = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            customeDatas.add(new MyCustomeData(i));
        }


        /**
         * here you will collect the serach result list
         * */
        ArrayList<MyCustomeData> tempMyCustomeDatas = searchDate(integers, customeDatas);


    }

    /**
     * this is the important method as it looks for the relevant ids
     * */
    private static ArrayList<MyCustomeData> searchDate(ArrayList<Integer> integers, ArrayList<MyCustomeData> customeDatas) {
        // TODO Auto-generated method stub
        ArrayList<MyCustomeData> list = new  ArrayList<>();

        for (int i = 0; i < integers.size(); i++) {
            for (int j = 0; j < customeDatas.size(); j++) {
                if (integers.get(i)== customeDatas.get(i).getId()) {
                    list.add(customeDatas.get(i));
                    break;
                }
            }
        }

        return list;
    }




}

 /**
  * I will assume that this is your custom Class who'es Id you want to find out
  * */
 class MyCustomeData{

    private int id;

    public MyCustomeData(int id){
        // TODO Auto-generated constructor stub
        this.id = id;
    }


    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-28
    • 2019-09-11
    • 2019-03-18
    • 2014-06-01
    • 1970-01-01
    • 2016-01-15
    相关资源
    最近更新 更多