gpt4 book ai didi

java - 如何为多个列表创建一个条件,仅显示具有相同值的数组

转载 作者:行者123 更新时间:2023-12-02 01:31:24 25 4
gpt4 key购买 nike

我想在多个数组中设置一个条件,以获取仅包含“已交付”的数组列表。有人可以帮我解决这个问题吗?

我已经尝试过“myOrderItemModelList.contains("Delivered")”,但我总是得到空白结果。

    List<MyOrderItemModel> myOrderItemModelList = new ArrayList<>();

myOrderItemModelList.add(new MyOrderItemModel(R.drawable.tide_prod, 2, "Tide Detergent", "149.50", "Ren's Grocery", "Juan Dela cruz", "Order_Received", 4, 3));
myOrderItemModelList.add(new MyOrderItemModel(R.drawable.tide_prod, 2, "Tide Detergent", "149.75", "Ren's Grocery", "Juan Dela cruz", "On_Process", 0, 5));
myOrderItemModelList.add(new MyOrderItemModel(R.drawable.tide_prod, 3, "Tide Detergent", "50", "Ren's Grocery", "Juan Dela cruz", "Order_Processed", 1, 0));
myOrderItemModelList.add(new MyOrderItemModel(R.drawable.tide_prod, 1, "Tide Detergent", "70", "Ren's Grocery", "Juan Dela cruz", "On_Delivery", 1, 1));
myOrderItemModelList.add(new MyOrderItemModel(R.drawable.tide_prod, 5, "Tide Detergent", "30", "Ren's Grocery", "Juan Dela cruz", "Delivered", 3, 2));
myOrderItemModelList.add(new MyOrderItemModel(R.drawable.tide_prod, 6, "Tide Detergent", "56", "Ren's Grocery", "Juan Dela cruz", "Delivered", 2, 4));
myOrderItemModelList.add(new MyOrderItemModel(R.drawable.tide_prod, 1, "Tide Detergent", "75", "Ren's Grocery", "Juan Dela cruz", "Cancelled", 5, 2));

if (myOrderItemModelList.contains("Delivered")) {
MyOrderItemOnProcessAdapter myOrderItemOnProcessAdapter = new MyOrderItemOnProcessAdapter(myOrderItemModelList);
my_orders_recyclerview.setAdapter(myOrderItemOnProcessAdapter);
myOrderItemOnProcessAdapter.notifyDataSetChanged();
}

最佳答案

myOrderItemModelList.contains("Delivered") 不会给您想要的东西,因为您的列表包含 MyOrderItemModel 实例,而不是字符串。

如果您正在寻找仅包含状态为“已交付”的项目的过滤列表,则情况如下,假设 MyOrderItemModel 上有一个 status() 方法:

List<MyOrderItemModel> delivered = myOrderItemModelList.stream().filter( 
item -> "Delivered".equals(item.status())).collect(Collectors.toList());

或者,如果您想对整个列表执行某些操作,但前提是至少有一项具有“已送达”状态:

if (myOrderItemModelList.stream().anyMatch(item -> "Delivered".equals(item.status()))) {
MyOrderItemOnProcessAdapter myOrderItemOnProcessAdapter = new
MyOrderItemOnProcessAdapter(myOrderItemModelList);
my_orders_recyclerview.setAdapter(myOrderItemOnProcessAdapter);
myOrderItemOnProcessAdapter.notifyDataSetChanged();
}

关于java - 如何为多个列表创建一个条件,仅显示具有相同值的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57554422/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com