接到任務 進行一個菜單去重排序的功能 還是比較有意思的
背景:一個用戶 存在 多個角色 一個角色分別有對應的菜單 所以要進行合併 去重 排序處理 並且菜單擁有二級甚至三級菜單
先定義一個Menu類 並且重寫它的hashcode和equals
package com.sitech.smartcity.common.entity; import java.util.List; public class Menu { private String menuName; private String url; private List<Menu> menus; private String openType;//0:iframe嵌套 1: load加載 ;2 :window.location打開新界面 private String logourl; private String code;//app業務編碼 private String classCode;//分類編碼 private String appid; private Integer classPriority;//排序字段 public Integer getClassPriority() { return classPriority; } public void setClassPriority(Integer classPriority) { this.classPriority = classPriority; } public String getMenuName() { return menuName; } public void setMenuName(String menuName) { this.menuName = menuName; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public List<Menu> getMenus() { return menus; } public void setMenus(List<Menu> menus) { this.menus = menus; } public String getOpenType() { return openType; } public void setOpenType(String openType) { this.openType = openType; } public String getLogourl() { return logourl; } public void setLogourl(String logourl) { this.logourl = logourl; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getClassCode() { return classCode; } public void setClassCode(String classCode) { this.classCode = classCode; } public String getAppid() { return appid; } public void setAppid(String appid) { this.appid = appid; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Menu menu = (Menu) o; if (menuName != null ? !menuName.equals(menu.menuName) : menu.menuName != null) return false; if (url != null ? !url.equals(menu.url) : menu.url != null) return false; if (openType != null ? !openType.equals(menu.openType) : menu.openType != null) return false; if (code != null ? !code.equals(menu.code) : menu.code != null) return false; if (classCode != null ? !classCode.equals(menu.classCode) : menu.classCode != null) return false; if (appid != null ? !appid.equals(menu.appid) : menu.appid != null) return false; return classPriority != null ? classPriority.equals(menu.classPriority) : menu.classPriority == null; } @Override public int hashCode() { int result = menuName != null ? menuName.hashCode() : 0; result = 31 * result + (url != null ? url.hashCode() : 0); result = 31 * result + (menus != null ? menus.hashCode() : 0); result = 31 * result + (openType != null ? openType.hashCode() : 0); result = 31 * result + (logourl != null ? logourl.hashCode() : 0); result = 31 * result + (code != null ? code.hashCode() : 0); result = 31 * result + (classCode != null ? classCode.hashCode() : 0); result = 31 * result + (appid != null ? appid.hashCode() : 0); result = 31 * result + (classPriority != null ? classPriority.hashCode() : 0); return result; } }
然後 寫幾個 方法 用來合併 去重 排序的方法
//清除重複數據 利用set集合的 無重複特點 public static List removeDuplicate(List list) { HashSet h = new HashSet(list); list.clear(); list.addAll(h); return list; } //根據排序字段進行排序 public static List compareList(List<Menu> list){ Collections.sort(list, new Comparator<Menu>() { @Override public int compare(Menu m1,Menu m2) { if (m1.getClassPriority() > m2.getClassPriority()) { return 1; } if (m1.getClassPriority() == m2.getClassPriority()) { return 0; } return -1; } }); return list; } //遞歸 合併 去重 public static void trecursive(List<Menu> list){ try { for(Iterator<Menu> oneIter = list.iterator(); oneIter.hasNext();){ Menu one = oneIter.next(); for(Iterator<Menu> twoIter = list.iterator(); twoIter.hasNext();){ Menu two = twoIter.next(); //classcode爲菜單的唯一標識 此處使用剛纔重寫的equals比較兩個Menu對象也可 if(one.getClassCode()!=null&&one.getClassCode().equals(two.getClassCode())){ //如果進入 說明存在兩個相同菜單 子菜單爲空的情況 填充菜單 再去重 if (one.getMenus() == null && two.getMenus() != null) { one.setMenus(two.getMenus()); } if (two.getMenus() == null && one.getMenus() != null) { two.setMenus(one.getMenus()); } if (two.getMenus() == null && one.getMenus() == null) { continue; } //否則合併 去重被合併菜單 one.getMenus().addAll(two.getMenus()); two.setMenus(one.getMenus()); removeDuplicate(one.getMenus()); compareList(one.getMenus()); //遞歸 trecursive(one.getMenus()); } } } }catch (Exception e){ e.printStackTrace(); } }