简体   繁体   English

迭代嵌套列表并提取项目

[英]Iterating over nested list and extracting the items

I have the below two list having the data as shown below我有以下两个列表,其数据如下所示

the very first list as shown below第一个列表如下所示

storeIdList storeIdList

 0 = "1"
 1 = "10"
 2 = "12"
 3 = "44"

the second list data is coming is of orderList即将到来的第二个列表数据是 orderList

 0  ="[1]"
 1  ="[]"
 2  ="[]"
 3= "[46,47]"

so basically each store has certain orders and as shown store 44 has two orders that is 46 and 47 so they are declared as shown below of type string所以基本上每个商店都有一定的订单,如图所示,商店 44 有两个订单,即 46 和 47,因此它们被声明为如下所示的字符串类型

 List<String> storeIdList
 List<String> orderList

Now basically I want to iterate over a storeList first and from within that store I want to extract the orders with in that store if orders are there for that particular store, for example store 44 has two orders 46 and 47 and store 10 has no orders at all,现在基本上我想首先迭代一个 storeList 并从该商店中提取该商店中的订单,如果该特定商店有订单,例如商店 44 有两个订单 46 和 47,商店 10 没有订单总之,

so to achieve this I have used two nested for loops所以为了实现这一点,我使用了两个嵌套的 for 循环

for (int i=0 ; i<storeIdList.size();i++)
 { 
   for (int j=0 ; j<orderList.size();j++)
   {
     // do buisness logic
   }
  } 
   

Now the issue is that for store 44, I am not able to extract both independently that is 46 and 47 please advise how can i extract th orders indepednetly with in the storeId loop现在的问题是,对于商店 44,我无法独立提取 46 和 47,请告知我如何在 storeId 循环中独立提取订单

First of all I would not use such data structure, you could use Map<String, List<String>> for example, where key is a store id and value is a list of orders.首先,我不会使用这样的数据结构,您可以使用Map<String, List<String>>例如,其中 key 是商店 ID,value 是订单列表。 However, if you are forced to keep the provided structure, you could extract the orders in the following way:但是,如果您被迫保留提供的结构,您可以通过以下方式提取订单:

for (int i=0 ; i<storeIdList.size();i++) {
     String storeId = storeIdList.get(i);
     String orders = orderList.get(i);
}

But you should ensure that orderList has the appropriate data.但是您应该确保 orderList 具有适当的数据。

You can find the corresponding string in orderList using orderList.get(storeIdList.indexOf(storeId)) and then split it on ,\s* after replacing [ and ] with "" , and finally return the resulting String[] .您可以使用orderList.get(storeIdList.indexOf(storeId))orderList中找到对应的字符串,然后将[]替换为""后在,\s*上拆分,最后返回结果String[]

Note: I have also provided you with a variant of the functions which returns List<String> .注意:我还为您提供了返回List<String>的函数的变体。

Demo:演示:

import java.util.Arrays;
import java.util.List;

public class Main {

    public static void main(String[] args) {
        List<String> storeIdList = List.of("1", "10", "12", "44");
        List<String> orderList = List.of("[1]", "[]", "[]", "[46,47]");
        System.out.println(storeIdList);
        System.out.println(orderList);

        // Get orderIds for storeId, 44
        System.out.println(Arrays.toString(getOrderIdArray(storeIdList, orderList, "44")));
        System.out.println(getOrderIdList(storeIdList, orderList, "44"));
    }

    static String[] getOrderIdArray(List<String> storeIdList, List<String> orderList, String storeId) {
        return orderList.get(storeIdList.indexOf(storeId))
                        .replace("[", "")
                        .replace("]", "")
                        .split(",\\s*");
    }

    static List<String> getOrderIdList(List<String> storeIdList, List<String> orderList, String storeId) {
        return Arrays.asList(orderList.get(storeIdList.indexOf(storeId))
                                        .replace("[", "")
                                        .replace("]", "")
                                        .split(",\\s*")
                            );
    }
}

Output: Output:

[1, 10, 12, 44]
[[1], [], [], [46,47]]
[46, 47]
[46, 47]

Explanation of the regex, ,\s* at regex101 :正则表达式 , ,\s*regex101的解释:

在此处输入图像描述

According to your sample lists, there are corresponding store elements and order elements.根据您的示例列表,有相应的商店元素和订单元素。 In other words, the first element of orderList are the orders for the first element of storeIdList .换句话说, orderList 的第一个元素是orderList的第一个元素的storeIdList And you want to extract all the orders for each store.并且您想提取每个商店的所有订单。

Consider the following.考虑以下。

List<String> storeIdList = List.of("1", "10", "12", "44");
List<String> orderList = List.of("[1]", "[]", "[]", "[46,47]");
for (int i = 0; i < storeIdList.size(); i++) {
    String order = orderList.get(i).replace("[", "").replace("]", "");
    if (order.length() > 0) {
        String[] orders = order.split(",");
        System.out.printf("Store %s orders: %s%n",
                          storeIdList.get(i),
                          java.util.Arrays.toString(orders));
    }
}

First I remove the brackets from the string containing the orders and then I split the resulting string on each comma and thus I get an array where each array element is a single order.首先,我从包含订单的字符串中删除括号,然后在每个逗号上拆分结果字符串,因此我得到一个数组,其中每个数组元素都是一个订单。

Then you can perform your business logic on the array orders .然后您可以在数组orders上执行您的业务逻辑。

When I run the above code, I get the following output:当我运行上面的代码时,我得到以下 output:

Store 1 orders: [1]
Store 44 orders: [46, 47]
for(int i=0; i< storeIdList.size() ; i++){
        String[] orderIds = orderIdList.get(i).split(",");
        for(int j = 0; j< orderIds.length ; j++){
            System.out.println("Store Id : "+ i + ", Order Id : " j);
        }
   }

Orderlist can have multiple list, You need to split it to retrieve individually Orderlist 可以有多个列表,需要拆分才能单独检索

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM