gpt4 book ai didi

java - 使用java递归搜索Hashmap列表中的值

转载 作者:行者123 更新时间:2023-12-01 04:33:55 26 4
gpt4 key购买 nike

我试图循环遍历 HashMap 列表并形成树状结构。输入( HashMap 列表)和所需的输出( HashMap 列表)如下:

输入:

[{State=Tamil Nadu, Continent=Asia, Country=India, City=Chennai}, {State=Kerala, Continent=Asia, Country=India, City=Cochin}, {State=Tamil Nadu, Continent=Asia, Country=India, City=Madurai}, {State=Something, Continent=Asia, Country=Srilanka, City=Colombo}, {State=anythig, Continent=Africa, Country=South Africa, City=new}]

预期结果:

[{children=[{children=[{State=Tamilnadu, children=[{City=Madurai}, {City=Chennai}]}, {State=Kerala, children=[{City=Cochin}]}], Country=India}, {Country=Srilanka}], Continent=Asia}, {Continent=Africa}]

(请考虑即使是斯里兰卡和非洲也应该具有相同的结构。我没有添加它,因为它是一个示例)

主要目标是形成如下树状结构:

  Asia
India
TamilNadu
Chennai
Madurai
Kerala
Cochin
Srilanka
Something
Colombo
Africa
South Africa
Anything
new

所有数据都是动态的,唯一给定的输入是组顺序,如 "Continent, Country, State, City"这意味着城市属于州,州属于国家,依此类推。我在下面的代码中尝试的是搜索输入列表,首先获取城市,形成城市 map ,然后找到索引列表,其中 map 包含每个城市的值并获取州,将它们添加到一张 map 并添加该州对应的城市按键children (我想过以递归方式执行此操作,但无法得到一个想法)这很重要,因为 map 的最终列表将转换为树存储的 json 输入。这必须在java代码中完成,我对此感到震惊。任何人都可以提供示例或任何指示...

public void recursiveSearch(List<Map<String, String>> inputList){
String groupOrder = "Continent, Country, State, City";
String[] splitList = groupOrder.split(",");
Map finalMap = new HashMap();
for(int i= splitList.length-1; i >= 0; i--){
//int index = 0;
List searchList = new ArrayList();
String keyToSearch= splitList[i].trim();
for (Map<String, String> map : inputList) {

String searchVal= map.get(keyToSearch);
//index = inputList.indexOf(map);
//System.out.println(index);
if(!searchList.contains(searchVal)){
searchList.add(searchVal);
}
//System.out.println(searchVal);
}
if(searchList.size() > 0){
Map allSearchMap = new HashMap();
Map indSearchMap= new HashMap();
for(int j=0; j < searchList.size(); j++ ){
int index = 0;
//System.out.println(splitList[splitList.length-1] +" "+ keyToSearch);
if(((splitList[splitList.length-1]).trim()).equalsIgnoreCase(keyToSearch)){

Map indexMap = new HashMap();
for (Map<String, String> newMap : inputList) {

boolean result= newMap.containsValue(searchList.get(j));
if(result){
index = inputList.indexOf(newMap);
System.out.println(searchList.get(j)+ " "+index);
}


}
indexMap.putAll(inputList.get(index));
if(indexMap != null){
if(!indSearchMap.containsValue(searchList.get(j))){
indSearchMap.put(keyToSearch, searchList.get(j));
}
allSearchMap.put(keyToSearch+j, indSearchMap);
if(i-1 >= 0){
String parent = (String) indexMap.get(splitList[i-1].trim());

finalMap.put(splitList[i-1].trim(), parent);
finalMap.put("children", allSearchMap.get(keyToSearch+j));
//System.out.println(index+ " "+searchList.get(j)+" "+indexMap.get(splitList[i-1].trim()));
}
}

}



}
}
}
System.out.println("--->"+finalMap.toString());

}

NOTE: The data and the group order are dynamic and can be of anything or any count.

最佳答案

试试这个

public class MyObjectClass implements Comparable<MyObjectClass> {

private String continent;
private String country;
private String state;
private String city;

public MyObjectClass(String continent) {
this.continent = continent;
}

public String getContinent() {
return continent;
}

public void setContinent(String continent) {
this.continent = continent;
}

public String getCountry() {
return country;
}

public void setCountry(String country) {
this.country = country;
}

public String getState() {
return state;
}

public void setState(String state) {
this.state = state;
}

public String getCity() {
return city;
}

public void setCity(String city) {
this.city = city;
}

@Override
public int compareTo(MyObjectClass o) {
if (this.continent.compareTo(o.continent) > 0)
return 1;
else if (this.continent.compareTo(o.continent) < 0)
return -1;
else
return 0;

}
}

然后是我的主课。这将返回您的预期输出。

import java.util.*;

public class MyMain {
public static void main(String[] args) {

List<MyObjectClass> list=new ArrayList<>();
MyObjectClass myObj1=new MyObjectClass("Asia");
myObj1.setCountry("Sri Lanka");
myObj1.setState("Western");
myObj1.setCity("Colombo");

MyObjectClass myObj2=new MyObjectClass("Africa");
myObj2.setCountry("South Africa");
myObj2.setState("Anything");
myObj2.setCity("new");

MyObjectClass myObj3=new MyObjectClass("Asia");
myObj3.setCountry("India");
myObj3.setState("Tamil Nadu");
myObj3.setCity("Chennai");

MyObjectClass myObj4=new MyObjectClass("Asia");
myObj4.setCountry("India");
myObj4.setState("Kerala");
myObj4.setCity("Cochin");

list.add(myObj1);
list.add(myObj2);
list.add(myObj3);
list.add(myObj4);

Collections.sort(list);
String tempCont=new String();
String tempCount=new String();
String tempState=new String();
for(MyObjectClass i:list){
if(tempCont.equals(i.getContinent())){
if(tempCount.equals(i.getCountry())){
if (tempState.equals(i.getState())){
System.out.println("-------------------------"+i.getCity());
}
else {
tempState=i.getState();
System.out.println("----------------"+tempState);
System.out.println("-------------------------"+i.getCity());
}

}
else {
tempCount=i.getCountry();
System.out.println("--------"+tempCount);
System.out.println("----------------"+i.getState());
System.out.println("-------------------------"+i.getCity());
}
}
else {
tempCont=i.getContinent();
System.out.println(tempCont);
System.out.println("--------"+i.getCountry());
System.out.println("----------------"+i.getState());
System.out.println("-------------------------"+i.getCity());
}

}
}
}

关于java - 使用java递归搜索Hashmap列表中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17592781/

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