Java HashMap中的树数据结构
我试图根据从查询中获得的结果集创建一个树结构。下面的代码块仅在结果集有两列时有效。第一列是父列,第二列是子列。我遇到的问题是试图重新设计它,以便它可以包含多个不同的级别(每列表示一个级别)。我现在唯一能做的就是复制代码,检查层数并硬编码以添加新的层数。我正在按行然后按列处理结果集。(例如,获取第一行,然后获取第一列并存储该值。) 关于如何重新设计此功能,使其在多个级别上具有灵活性,有什么想法吗?最终,根被传递到一个哈希表中,并被处理成一个JSON文件。仅供参考,在代码的第二部分中,我将遍历HashMap并对其进行格式化,以满足JSON的需要Java HashMap中的树数据结构,java,json,data-structures,tree,hashmap,Java,Json,Data Structures,Tree,Hashmap,我试图根据从查询中获得的结果集创建一个树结构。下面的代码块仅在结果集有两列时有效。第一列是父列,第二列是子列。我遇到的问题是试图重新设计它,以便它可以包含多个不同的级别(每列表示一个级别)。我现在唯一能做的就是复制代码,检查层数并硬编码以添加新的层数。我正在按行然后按列处理结果集。(例如,获取第一行,然后获取第一列并存储该值。) 关于如何重新设计此功能,使其在多个级别上具有灵活性,有什么想法吗?最终,根被传递到一个哈希表中,并被处理成一个JSON文件。仅供参考,在代码的第二部分中,我将遍历Has
HashMap<String, HashSet<String>> level1TwoColumn = new HashMap<String, HashSet<String>>();
Object[] listElementCalcColumnNumber = list.get(0);
Hashtable allHash = new Hashtable();
//this will store all of the values in a hashmap and hashset
for(int i=0; i<list.size(); i++){
Object[] listElement = list.get(i);
//the reason we added ""+ between (String) and listElement is because listElement can be something other than a String (e.g. double, integer, etc)
//by adding "" to it, it becomes a String value automatically so then we can cast double/integer/etc into a String
String currentLevel1 = (String) ""+listElement[0];
currentLevel1 = currentLevel1.replace("\"", "");
//this prevents variables from duplicating. if the new element does not exist in hashmap, a new record in the hashmap will be created.
if((level1TwoColumn.get(currentLevel1) = null)){
HashSet<String> level2 = new HashSet<String>();
level1TwoColumn.put(currentLevel1, level2);
}
//stores all of the lowest level into a hashset because they're all going to be unique. no need to check to see if they exist in the hashset.
String currentLevel2 = (String) ""+listElement[1];
currentLevel2 = currentLevel2.replace("\"", "");
level1TwoColumn.get(currentLevel1).add(currentLevel2);
}
HashSet completeTree = new HashSet();
//now that the structure of the data is complete. iterate through level1 to get and store keys as values and "name" as key
for(String key1 : level1TwoColumn.keySet()){
HashSet levelOneSet = new HashSet();
HashMap levelOne = new HashMap();
levelOne.put("name", key1);
levelOneSet.add(levelOne);
//this for-statement does the same thing as previous for-statement; stores the unique values and give them "name" and then put them in hashset and give the set a "children" key
for(String key2 : level1TwoColumn.get(key1)){
HashSet levelTwoSet = new HashSet();
HashMap levelTwo = new HashMap();
levelTwo.put("name", key2);
levelTwoSet.add(levelTwo);
levelOne.put("children", levelTwoSet);
}
//once it loops through once, all level 2 items will be stored under a unique level 1. then it gets added into our dendrogram one by one until all level 1 elements are added.
completeTree.add(levelOne);
}
//this is assigning what our first node is, which is "VA"
allHash.put("name", "VA");
//put everything under "VA" node
allHash.put("children", completeTree);
HashMap level1TwoColumn=newhashmap();
对象[]listElementCalcColumnNumber=list.get(0);
Hashtable allHash=新的Hashtable();
//这将在hashmap和hashset中存储所有值
对于(int i=0;我想我对您的问题有点困惑。HashMap是树的一个“分支连接”,可以使用HashMaps轻松递归地构造任意复杂度的树。HashMap自然地映射到JSON“对象”--如果使用正确的JSON工具包,则不需要转换/手动操作--只需将根传递给JSON工具包并说“serialize”。如果让人困惑,请道歉。我的问题是如何在HashMap上动态生成。目前,我将其设置为HashMap level1TwoColumn。这意味着它只能有两层。我正在尝试重新设计代码,以便它根据用户定义的列数不断生成分支。唯一的方法是就是使用映射
,然后使用大量难看的强制转换。最好使用适当的数据结构。忘记尝试让一般垃圾开心。无论如何,你需要使HashMap成为
,因为“leaves”不会是HashMaps。开始使用强制转换和实例
@boristesspider-什么时候强制转换“难看”了?