[英]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:我会建议以下流程:
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.