gpt4 book ai didi

java - 如何将字幕结构的 List 转换为 HashMap>

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

我有一个表,显示了按正确顺序从 1、2、3 排序的字幕结构...... TitleID 是唯一的。

TitleID - Title - Level - Order451 - History - Level1 - 1123 - History of America - Level2 - 2465 - History of Cali - Level3 - 3578 - History of NewYork - Level3 - 4765 - History of Europe - Level2 - 5754 - History of West Europe - Level3 - 6

This table was converted into a List<String[]> of {"451", "History", "Level1", "1" }, {"123", "History of America", "Level2", "2" }... also in a right order.

I want to convert this List<String[]> into a HashMap<String, List<String[]>>. If i do it manually it should be like this:

HashMap<String, List<String[]>> titleHashMap = new HashMap<String, List<String[]>>();

List<String[]> subTitleList=new ArrayList<String[]>;
String[] titles1={"123", "History of America", "Level2"};
subTitleList.add(titles1);
String[] titles12={"765", "History of Europe", "Level2"};
subTitleList.add(titles12);
titleHashMap.put("451", subTitleList);

List<String[]> subTitleList2=new ArrayList<String[]>;
String[] titles2={"465", "History of Cali" , "Level3"};
subTitleList2.add(titles2);

String[] titles21={"578", "History of NewYork", "Level3"};
subTitleList2.add(titles21);

titleHashMap.put("123", subTitleList2);

但是我不想手动执行,那么如何自动执行呢?注意:我还想要一个根级别,它是所有 Level1 的父级。例如,titleHashMap.put("root", subTitleListOfLevel1);

最佳答案

您的示例没有解释类(class)如何分组以及 map 的关键是什么。我认为类(class)是按级别分组的,关键是级别。但您可以轻松调整它。

// already exists:
List<String[]> lessons = ...

// final format
final HashMap<String, List<String[]>> titleHashMap = new HashMap<String, List<String[]>>();

for (String[] lesson : lessons) {
final String level = lesson[2];
if (!titleHashMap.containsKey(level)) {
titleHashMap.put(level, new ArrayList<String[]>());
}
titleHashMap.get(level).add(lesson);
}

关于java - 如何将字幕结构的 List<String[]> 转换为 HashMap<String, List<String[]>>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20510825/

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