将数据表查询的数据,转成这个格式的json
你的数据量只有两列吗
/**
* 递归省市区
* @author XMF
* @date 2021-05-25 16:15
**/
public class Demo {
public static void main1(String[] args) {
System.out.println(System.currentTimeMillis());
List<City> cityList = new ArrayList<>();
cityList.add(new City(1, 0, "北京", "北京"));
cityList.add(new City(2, 0, "内蒙", "内蒙"));
cityList.add(new City(3, 0, "河北", "河北"));
cityList.add(new City(11, 1, "东城", "北京东城"));
cityList.add(new City(12, 2, "呼市", "内蒙新呼市"));
cityList.add(new City(13, 3, "唐山", "河北唐山"));
cityList.add(new City(14, 1, "西城", "北京西城"));
cityList.add(new City(21, 11, "南锣鼓巷", "北京东城南锣鼓巷"));
cityList.add(new City(22, 12, "新城区", "内蒙新呼市新城区"));
cityList.add(new City(23, 13, "哈哈区", "河北唐山哈哈区"));
cityList.add(new City(24, 11, "南洛惜香", "北京东城南洛惜香"));
cityList.add(new City(25, 14, "西城广场", "北京西城西城广场"));
List<City> collect = cityList.parallelStream().filter(g -> g.getParentId() == 0).collect(Collectors.toList());
System.out.println(JSON.toJSONString(getData(collect, cityList, cityList, 0)));
System.out.println(System.currentTimeMillis());
}
public static List<City> getData(List<City> list, List<City> cityList, List<City> cityOldList, int status){
for (City city : list) {
cityList = cityOldList.parallelStream().filter(f -> f.getParentId() == city.getId()).collect(Collectors.toList());
if (!cityList.isEmpty()){
if (city.getChilds() ==null){
city.setChilds(new ArrayList<>());
}
city.setChilds(cityList);
getData(cityList, cityOldList, cityOldList, 1);
}
}
return list;
}
public static void main(String[] args) {
System.out.println(System.currentTimeMillis());
List<City> cityList = new ArrayList<>();
cityList.add(new City(1, 0, "北京", "北京"));
cityList.add(new City(2, 0, "内蒙", "内蒙"));
cityList.add(new City(3, 0, "河北", "河北"));
cityList.add(new City(11, 1, "东城", "北京东城"));
cityList.add(new City(12, 2, "呼市", "内蒙新呼市"));
cityList.add(new City(14, 1, "西城", "北京西城"));
cityList.add(new City(13, 3, "唐山", "河北唐山"));
cityList.add(new City(21, 11, "南锣鼓巷", "北京东城南锣鼓巷"));
cityList.add(new City(23, 13, "哈哈区", "河北唐山哈哈区"));
cityList.add(new City(22, 12, "新城区", "内蒙新呼市新城区"));
cityList.add(new City(24, 11, "南洛惜香", "北京东城南洛惜香"));
cityList.add(new City(25, 14, "西城广场", "北京西城西城广场"));
for (int a = 0; a < 20; a ++){
System.out.println(cityList.parallelStream().findAny());
}
Map<Integer, List<City>> resultMap1 = cityList.stream().collect(Collectors.groupingBy(City::getParentId, Collectors.toList()))
.entrySet().stream().sorted(Map.Entry.comparingByKey())
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (a, b) -> b, LinkedHashMap::new));
resultMap1.entrySet().forEach(System.out::println);
System.out.println("------------");
Map<Integer, List<City>> resultMap = cityList.stream().collect(Collectors.groupingBy(City::getParentId, Collectors.toList()))
.entrySet().stream().sorted(Collections.reverseOrder(Map.Entry.comparingByKey()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (a, b) -> b, LinkedHashMap::new));
resultMap.entrySet().forEach(System.out::println);
// List<Integer> collect = mapCommodity 排序
// .entrySet()
// .stream()
// .sorted(Collections.reverseOrder(Map.Entry.comparingByValue()))
// .map(Map.Entry::getKey).collect(Collectors.toList());
// .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e2,
// LinkedHashMap::new));
// List<City> sheng = cityList.stream().filter(f -> f.getParentId() == 0).collect(Collectors.toList());
// sheng.forEach(s -> {
// List<City> list = resultMap.get(s.getId());
// s.setChilds(list);
// list.forEach(l -> {
// List<City> list1 = resultMap.get(l.getId());
// l.setChilds(list1);
// });
// });
//parentId 分组
// Map<Integer, List<City>> resultMap = new HashMap<>();
// Map<Integer, List<City>> collect = resultMap.entrySet().stream().sorted(Collections.reverseOrder(Map.Entry.comparingByKey())).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (a, b) -> b));
//
// for (City city : cityList) {
// if(resultMap.containsKey(city.getParentId())){
// resultMap.get(city.getParentId()).add(city);
// }else{
// List<City> newList=new ArrayList<>();
// newList.add(city);
// resultMap.put(city.getParentId(), newList);
// }
// }
// //获取所有省
// List<City> sheng = new ArrayList<>();
// for (City city : cityList) {
// if(city.getParentId() == 0){
// sheng.add(city);
// }
// }
//
// for (City city : sheng) {
// //省对应市
// List<City> shi = resultMap.get(city.getId());
// city.setChilds(shi);
// for (City city1 : shi) {
// List<City> cities = resultMap.get(city1.getId());
// city1.setChilds(cities);
// }
// }
//
//
// System.out.println(JSON.toJSONString(sheng));
// System.out.println(System.currentTimeMillis());
}
}
@Data
class City{
private int id, parentId;
private String cityName, cityAllName;
private List<City> childs;
public City(int id, int parentId, String cityName, String cityAllName){
this.id = id;
this.parentId = parentId;
this.cityAllName = cityAllName;
this.cityName = cityName;
}
}
这是我原先写的类似的省市区树状数据,有看看有思路没