简体   繁体   English

在 Java 中存储多个列表的最佳方式是什么?

[英]What is the best way to store multiple lists in Java?

I am extracting data using some xyz logic.我正在使用一些 xyz 逻辑提取数据。 After extraction I am getting multiple list in each iteration.提取后,我在每次迭代中得到多个列表。

   for (int i= 0; i<= 5; i++) 
   {
          for (int j= 0; j<= 5; j++) 
          {
              //data extraction logic
             lList1.add(value1);
             lList2.add(value2);
             lList3.add(value3);
          }
         //in each iteration I am getting list different 
          System.out.println(" lList1 for iteration "+i+"is: "+lList1);
          System.out.println(" lList2 for iteration "+i+"is: "+lList2);
          System.out.println(" lList3 for iteration "+i+"is: "+lList3);
   }

I need to pass these list to database.我需要将这些列表传递给数据库。 Each list is associated with one column in db.每个列表都与数据库中的一列相关联。
Example: lList1 is for column1, lList2 is for column2, lList3 is for column3 etc示例:lList1 用于 column1,lList2 用于 column2,lList3 用于 column3 等

What is best way to pass these list to db or to write these values row by row in java output should be like将这些列表传递给 db 或在 java 输出中逐行写入这些值的最佳方法应该是

row 1 lList1 lList2 lList3 etc of first iteration list values第 1 行 lList1 lList2 lList3 等第一次迭代列表值
row 2 lList1 lList2 lList3 etc of second iteration list values etc第 2 行 lList1 lList2 lList3 等第二次迭代列表值等

Can anyone help?谁能帮忙?

You can use Multidimensional Collections您可以使用多维集合

ArrayList<ArrayList<Object>> a = new ArrayList<ArrayList<Object>>();

Result can be like that (based on your logic):结果可能是这样的(根据您的逻辑):

Multidimensional ArrayList: [[3, 4], [12, 13], [22, 23], [33,23]]

here is a full example:这是一个完整的例子:

ArrayList<ArrayList<Object>> list = new ArrayList<ArrayList<Object>>(); 
for (int i= 0; i<= 5; i++) {
    //if you got all the values 
    list.add(new ArrayList<Object>(Arrays.asList(value1, value2, value3))); 
    //else you have to fetch them from another loop
   for (int j= 0; j<= 5; j++) {
            list.add(new ArrayList<Object>()); 
            list.get(j).add(value1); 
          }
   }

Create a class which will contain three members col1,col2 and col3 and make list of that class and use it.创建一个包含三个成员 col1、col2 和 col3 的类,并列出该类并使用它。

class MyRow
{
public {col1datatype} col1;
public {col2DataType} col2;
public {col3DataType} col3;
}

your function你的功能

 List<MyRow> lstRows = new ArrayList<MyRow>();

         for (int i= 0; i<= 5; i++) 
           {
                  for (int j= 0; j<= 5; j++) 
                  {
                    MyRow row = new MyRow(); 
                     row.col1 = value1;
                     row.col2 = value2;
                     row.col3 = value3;
                     llstRows.Add(row); 
                  }
            }

And use that lstRows accordingly at database transaction.并在数据库事务中相应地使用该 lstRows。

I would suggest the following flow:我会建议以下流程:

  • First of all check the size of each listing and take the size of the biggest listing present, lets say its lList3 with 10 elements in it.首先检查每个列表的大小并获取最大列表的大小,假设它的 lList3 中有 10 个元素。
  • Make sure that your columns in DB are nullable meaning they can be NULL确保您在数据库中的列可以为空,这意味着它们可以为 NULL
  • Make a for loop to run 10 times (length of the biggest list) and insert lList1[i], lList2[i] and lList3[i] to the DB in each loop.使 for 循环运行 10 次(最大列表的长度)并在每个循环中将 lList1[i]、lList2[i] 和 lList3[i] 插入到 DB。 Of course you will need to check first if the current index exists in the list, if not then insert NULL for that list column.当然,您需要先检查列表中是否存在当前索引,如果不存在,则为该列表列插入 NULL。

This way you will have each row in DB, the same way you need to list them out later in your front end.这样您将在数据库中拥有每一行,就像您稍后需要在前端列出它们一样。 Also it will be easier to do any data search.此外,进行任何数据搜索也将更加容易。

I would suggest in that case to serialize it to Json and store it in the column (since you use this approach with columns for lists).在这种情况下,我建议将其序列化为 Json 并将其存储在列中(因为您将这种方法与列表的列一起使用)。

When retrieving data from the DB, you can just deserialize it from Json to a List object.从数据库中检索数据时,您可以将其从 Json 反序列化为 List 对象。

For example Entity Framework is doing the same in .NET例如 Entity Framework 在 .NET 中做同样的事情

if all list contains same length then make one common function for all ArrayList and iterate it with the loop of the maximum size ArrayList: It should be like: for(int i=0;i<lList1.size();i++) { String insertQueryMainProduct = "INSERT INTO " + "" + tableName + "" + "(ColumnOneName,ColumnSecondName,ColumnThirdName)" + " VALUES('" +lList1.get(i)+ "', '" + lList2.get(i)+ "', '" + lList3.get(i)+ "')"; System.out.println("Query: " + insertQueryMainProduct); statement.executeUpdate(insertQueryMainProduct);如果所有列表包含相同的长度,则为所有 ArrayList 创建一个通用函数,并使用最大大小 ArrayList 的循环对其进行迭代:它应该像: for(int i=0;i<lList1.size();i++) { String insertQueryMainProduct = "INSERT INTO " + "" + tableName + "" + "(ColumnOneName,ColumnSecondName,ColumnThirdName)" + " VALUES('" +lList1.get(i)+ "', '" + lList2.get(i)+ "', '" + lList3.get(i)+ "')"; System.out.println("Query: " + insertQueryMainProduct); statement.executeUpdate(insertQueryMainProduct); for(int i=0;i<lList1.size();i++) { String insertQueryMainProduct = "INSERT INTO " + "" + tableName + "" + "(ColumnOneName,ColumnSecondName,ColumnThirdName)" + " VALUES('" +lList1.get(i)+ "', '" + lList2.get(i)+ "', '" + lList3.get(i)+ "')"; System.out.println("Query: " + insertQueryMainProduct); statement.executeUpdate(insertQueryMainProduct); Hope this will help you希望对你有帮助

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

相关问题 在 Java 中以相同方法迭代多个列表的最佳方法是什么 - What is the best way to iterate over multiple lists in the same method in Java 在 Java 中存储多个有距离的城市的最佳方法是什么? - What is the best way to store multiple cities with distances in Java? 在Java中存储SQL查询的最佳方法是什么 - What is the best way to store sql queries in java 用Java在磁盘上存储对象的最佳方法是什么? - What is the best way to store objects on disk in java? 将操作存储为java中的数据的最佳方法是什么? - What is the best way to store actions as data in java? 将两个列表组合成地图(Java)的最佳方法是什么? - What is the best way to combine two lists into a map (Java)? 在Java中,存储大列表最有效的内存方式是什么 - In Java, what is the most memory efficient way to store large lists 在java中按多个字段过滤的最佳方法是什么 - What is the best way of filtering by multiple fields in java 从Java中的多个列表合并和删除重复项的最佳方法 - Best way to merge and remove duplicates from multiple lists in Java java-将多个列表写回到一个JsonArray的最佳方法 - java - the best way to write multiple lists back to back to a single JsonArray
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM