【问题标题】:how to extract list elements that meet specific criteria and get count for those elements in android如何提取满足特定条件的列表元素并获取android中这些元素的计数
【发布时间】:2016-11-07 10:05:21
【问题描述】:

我有一个列表,其中包含有关车辆的信息,我想提取里程超过 10 000 的车辆的数量(总数)。实现这一目标的最佳方法是什么?

 private List<MyList> myListItems;

 myListItems= MyList.LoadListData();

 for (int i = 0; i <= myListItems.size(); i++)
{
            {  //myListItems.get(i).mileage().count()
                 myListItems.get(i).mileage() > 10 000;

            }

}

【问题讨论】:

    标签: java android list sorting


    【解决方案1】:
    int total = 0;
    for (int i = 0, size = myListItems.size(); i < size; i++){
         if(myListItems.get(i).mileage() > 10 000){
             total++;
         }
    }
    

    【讨论】:

      【解决方案2】:

      使用 Java 8:

      long count = myListItems.stream()
                              .filter(v -> v.mileage() > 10_000)
                              .count();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-02-15
        • 2011-04-16
        • 1970-01-01
        • 1970-01-01
        • 2020-01-22
        相关资源
        最近更新 更多