private static Set<String> getNodeNames(Long id) { Set<String> names = new HashSet<String>(); CityToken parent = addMyName(id, names); addChildrenNames(id, names); while (parent != null) { parent = addMyName(parent.getId(), names); } return names; }
/** 把id对应节点的所有子节点的名字(递归到所有下级子节点)加入到names中 */ private static void addChildrenNames(Long id, Set<String> names) { List<CityToken> ctList; Set<Long> addeds = new HashSet<Long>(); // 已处理的id集合 Deque<Long> pids = new ArrayDeque<Long>(); // 未处理的id栈 pids.push(id); while (!pids.isEmpty()) { Long pid = pids.pop(); if (addeds.add(pid)) { ctList = DataCache.pIdMap.get(pid); if (ctList != null) { for (int i = 0, n = ctList.size(); i < n; ++i) { CityToken ct = ctList.get(i); names.add(ct.getName()); pids.push(ct.getId()); } } } } }