0
我有一張表,顯示了一個字幕結構,該字幕結構以1,2,3的正確順序排序..... TitleID是唯一的。如何將字幕結構的列表<String[]>轉換爲HashMap <String,List <String[]>>
TitleID - Title - Level - Order 451 - History - Level1 - 1 123 - History of America - Level2 - 2 465 - History of Cali - Level3 - 3 578 - History of NewYork - Level3 - 4 765 - History of Europe - Level2 - 5 754 - History of West Europe - Level3 - 6
此表被轉換成{"451", "History", "Level1", "1" }
,{"123", "History of America", "Level2", "2" }
一個List<String[]>
...也是一個正確的順序。我想將List<String[]>
轉換爲HashMap<String, List<String[]>>
。如果我做手工,它應該是這樣的:
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);
在'for-each'循環中執行此操作。 –
如果字符串數組的大小:「{」123「,」美國曆史「,」Level2「}'是有限的,只反映了一些屬性,爲什麼不用OOP風格? – Sage
更復雜 – Tum