Tree的实现
系統中有一個三級下拉的Tree
下拉最多三級
數據表信息
一級的PID,為空
二級的PID,為一級的ID
三級的PID,為二級的ID
返回所有
@Override public List<AssayItemClassExt> selectAllList() {List<AssayItemClassExt> list = assayItemClassExtMapper.selectAllList();// 一級、二級、三級List<AssayItemClassExt> listOne = new ArrayList<>();List<AssayItemClassExt> listTwo = new ArrayList<>();List<AssayItemClassExt> listThree = new ArrayList<>();for (int i = 0; i < list.size(); i++) {AssayItemClassExt assayItemClassExt = list.get(i);Short index = assayItemClassExt.getTier();if (index == 1) {listOne.add(assayItemClassExt);}if (index == 2) {listTwo.add(assayItemClassExt);}if (index == 3) {listThree.add(assayItemClassExt);}}// 一級PIDMap<String, List<AssayItemClassExt>> parentIdOne = new HashMap<>();for (int i = 0; i < listOne.size(); i++) {String key = listOne.get(i).getId();List<AssayItemClassExt> parentIdOneList = new ArrayList<>();parentIdOne.put(key, parentIdOneList);}// 過濾二級節點for (Map.Entry<String, List<AssayItemClassExt>> entry : parentIdOne.entrySet()) {String key = entry.getKey();List<AssayItemClassExt> value = entry.getValue();for (int i = 0; i < listTwo.size(); i++) {AssayItemClassExt entity = listTwo.get(i);if (key.equals(entity.getPid())) {value.add(entity);}}}// 二級PIDMap<String, List<AssayItemClassExt>> parentIdTwo = new HashMap<>();for (int i = 0; i < listTwo.size(); i++) {String key = listTwo.get(i).getId();List<AssayItemClassExt> parentIdTwoList = new ArrayList<>();parentIdTwo.put(key, parentIdTwoList);}// 過濾三級節點for (Map.Entry<String, List<AssayItemClassExt>> entry : parentIdTwo.entrySet()) {String key = entry.getKey();List<AssayItemClassExt> value = entry.getValue();for (int i = 0; i < listThree.size(); i++) {AssayItemClassExt entity = listThree.get(i);if (key.equals(entity.getPid())) {value.add(entity);}}}// 清空listlist.clear();// 重組listfor (int i = 0; i < listOne.size(); i++) {AssayItemClassExt entityOne = listOne.get(i);list.add(entityOne);String keyOne = entityOne.getId();List<AssayItemClassExt> valueOne = parentIdOne.get(keyOne);if (valueOne.size() > 0) {for (AssayItemClassExt entityTwo : valueOne) {list.add(entityTwo);String keyTwo = entityTwo.getId();List<AssayItemClassExt> valueTwo = parentIdTwo.get(keyTwo);if (valueTwo.size() > 0) {list.addAll(valueTwo);}}}}return list; }根據三級ids,返回所有
@Override public List<AssayItemClassExt> selectListByIds(String[] ids){if (ids != null && !ids.equals("")) {List<AssayItemClassExt> list = assayItemClassExtMapper.selectAllList();// 一級、二級、三級List<AssayItemClassExt> listOne = new ArrayList<>();List<AssayItemClassExt> listTwo = new ArrayList<>();List<AssayItemClassExt> listThree = new ArrayList<>();for (int i = 0; i < list.size(); i++) {AssayItemClassExt assayItemClassExt = list.get(i);Short index = assayItemClassExt.getTier();if (index == 1) {listOne.add(assayItemClassExt);}if (index == 2) {listTwo.add(assayItemClassExt);}if (index == 3) {listThree.add(assayItemClassExt);}}List<AssayItemClassExt> listNew=new ArrayList<>();for(String id:ids){for(AssayItemClassExt assayItemClassExt:listThree){if(id.equals(assayItemClassExt.getId())){listNew.add(assayItemClassExt);}}}listThree.clear();listThree.addAll(listNew);// 二級Map<String, AssayItemClassExt> mapTwo = new HashMap<>();for (int i = 0; i < listThree.size(); i++) {String key = listThree.get(i).getPid();if (!mapTwo.containsKey(key)) {AssayItemClassExt assayItemClassExt = null;mapTwo.put(key, assayItemClassExt);}}for (Map.Entry<String, AssayItemClassExt> entry : mapTwo.entrySet()) {String key = entry.getKey();for (int i = 0; i < listTwo.size(); i++) {if (key.equals(listTwo.get(i).getId())) {mapTwo.put(key, listTwo.get(i));}}}listTwo.clear();for (AssayItemClassExt value : mapTwo.values()) {listTwo.add(value);}// 一級Map<String, AssayItemClassExt> mapOne = new HashMap<>();for (Map.Entry<String, AssayItemClassExt> entry : mapTwo.entrySet()) {AssayItemClassExt value = entry.getValue();String key = value.getPid();if (!mapOne.containsKey(key)) {AssayItemClassExt assayItemClassExt = null;mapOne.put(key, assayItemClassExt);}}for (Map.Entry<String, AssayItemClassExt> entry : mapOne.entrySet()) {String key = entry.getKey();for (int i = 0; i < listOne.size(); i++) {if (key.equals(listOne.get(i).getId())) {mapOne.put(key, listOne.get(i));}}}listOne.clear();for (AssayItemClassExt value : mapOne.values()) {listOne.add(value);}// 清空listlist.clear();// 重組listfor (AssayItemClassExt entityOne : listOne) {list.add(entityOne);String idOne = entityOne.getId();for (AssayItemClassExt entityTwo : listTwo) {if (idOne.equals(entityTwo.getPid())) {list.add(entityTwo);String idTwo = entityTwo.getId();for (AssayItemClassExt entityThree : listThree) {if (idTwo.equals(entityThree.getPid())) {list.add(entityThree);}}}}}return list;}return null; }總結
- 上一篇: Filter获取Spring Bean对
- 下一篇: Tree的前台解析