gpt4 book ai didi

java - 当通过 For 循环返回 List 时,无法在 Java 中将 List 转换为 Map

转载 作者:行者123 更新时间:2023-12-01 16:22:18 28 4
gpt4 key购买 nike

我目前面临一个场景,我想将列表转换为 Map。列表通过 for 循环返回,并进行分区,假设列表返回为:

[1,2,3,4,5,6] , and i have partitioned in the format of:
[1,2,3]
[4,5,6]

我想将 [1,2,3] 和 [4,5,6] 转换为 Map<Key,Value>对,其中我将传递 key ,值将是 [1,2,3] 和 [4,5,6]。例如,对于下面给定的代码:

List<List<Object>> subSets = ListUtils.partition(ls, 3);
for(int i=0;i<subSets.size();i++) {
System.out.println(subSets.get(i));

我得到的输出是:

no of rows:3
[test1, test2, 8]
[qa1, qa2, 9]

现在我想将其传递给循环中的 Map,以便它采用以下格式的列表:

map.put("firstname",test1);
map.put("lastname",test2);
map.put("subjectid",8);

map 格式应为Map<String,Object>因为我传递的最后一个值是一个整数。稍后,我想将此映射传递给 Json 请求,以便我可以执行 POST 请求。

JSONObject request = new JSONObject(map);
System.out.println(request);
System.out.println(request.toJSONString()); //to prevent serialization error

// so we have our body created, so we have to test this
//for post we have 201

given().
header("Content-Type","application/json")
.contentType(ContentType.JSON).accept(ContentType.JSON).
body(request.toJSONString())
.when()
.post("https://reqres.in/api/users").then()
.statusCode(201);

到目前为止,我无法将 List 转换为 Map ,在 for 循环中,每个列表值都映射到映射中的键。请提出建议。

完整代码如下:

class ExcelUtils {

static XSSFWorkbook workbook;
static XSSFSheet sheet;
ArrayList<Object> ls = new ArrayList<Object>();
HashMap<String,Object> map = new HashMap<String,Object>();

public ExcelUtils(String excelPath, String sheetName) throws IOException {

workbook = new XSSFWorkbook(excelPath);
sheet = workbook.getSheet(sheetName);

}


@Test
public void getRowCount() throws IOException {

int rowCount = sheet.getPhysicalNumberOfRows();
System.out.println("no of rows:" + rowCount);


}



@Test(dependsOnMethods = "getRowCount")
public void getCellData() throws IOException {

// String s = sheet.getRow(rownum).getCell(colnum).getStringCellValue();
// System.out.println("cell value:" + s);

//double i = sheet.getRow(rownum).getCell(colnum).getNumericCellValue();
//System.out.println("numeric cell value:" + i);

//data formatter
DataFormatter format=new DataFormatter();

// dynamic code
int rowcount =sheet.getLastRowNum();

for(int i=1;i<=rowcount;i++) {
Row r= sheet.getRow(i);
int cellcount = r.getLastCellNum();

for(int j=0;j<cellcount;j++) {

Cell c=r.getCell(j);
//System.out.println(format.formatCellValue(c));

ls.add(format.formatCellValue(c));
//ls.add("\""+format.formatCellValue(c)+"\"");
}

}

List<List<Object>> subSets = ListUtils.partition(ls, 3);

for(int i=0;i<subSets.size();i++) {

System.out.println(subSets.get(i));
}

}

代码的输出是这样的:

[RemoteTestNG] detected TestNG version 6.14.3
no of rows:3
[test1, test2, 8]
[qa1, qa2, 9]

我需要下一步的帮助。

最佳答案

    List<Object> ex = new ArrayList<Object>();
ex.add("test1");
ex.add("test2");
ex.add("8");
List<Object> ex2 = new ArrayList<Object>();
ex2.add("qa1");
ex2.add("qa2");
ex2.add("5");
List<List<Object>> subSets = new ArrayList<List<Object>>();
subSets.add(ex);
subSets.add(ex2);
List<Object> listOfMap = new ArrayList<Object>();
Map<String, Object> mapOfSubsetList = new HashMap<>();

for (int i = 0; i < subSets.size(); i++) {
System.out.println("List at position ::: "+i+subSets.get(i));
mapOfSubsetList.put("firstname", subSets.get(i).get(0));
mapOfSubsetList.put("lastname", subSets.get(i).get(1));
mapOfSubsetList.put("subjectid", subSets.get(i).get(2));
listOfMap.add(mapOfSubsetList);
}

System.out.println("final list of map ::: "+listOfMap);

根据我的理解,您将获得一个 map 列表,该列表将以 JSONArray 形式发送。如果您仍想将 map 作为 JSONObject 发送,则需要一个包含此 map 列表的键 { "finalrersult": "[{...}]"}

上述程序的结果是

List at position 0[test1, test2, 8]
List at position 1[qa1, qa2, 5]
final list of map[{firstname=qa1, subjectid=5, lastname=qa2}, {firstname=qa1, subjectid=5, lastname=qa2}]

关于java - 当通过 For 循环返回 List 时,无法在 Java 中将 List 转换为 Map,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62236520/

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