/** * 获取所有行政区划代码的最高级 用于sql的like查询 * @param regionCodeLevels * @return */ public static List<String> getAllFirstRegionCode(String regionCodeLevels){ List<String> regionCodeList = new ArrayList<>(); LinkedHashSet<String> regionCodeLinkList = new LinkedHashSet<>();//自动去重 if(StringUtils.isNotEmpty(regionCodeLevels)){ String[] regionLevelArrays = regionCodeLevels.split(","); for (String regionLevels:regionLevelArrays) { String[] regionLevelArray = regionLevels.split("_"); String reginCode = regionLevelArray[0]; String regionLevel = regionLevelArray[1]; switch (regionLevel){ case "1":{ regionCodeLinkList.add(reginCode.substring(0,2));//35% break; } case "2":{ String proveCode = reginCode.substring(0,2); List<String> proveList = regionCodeLinkList.stream().filter(d->d.equals(proveCode)).collect(Collectors.toList()); if(proveList != null && proveList.size()>0){ //如果已经存在上一级则跳过添加 continue; }else { regionCodeLinkList.add(reginCode.substring(0,4));//3501% } break; } case "3":{ String proveCode = reginCode.substring(0,2); List<String> proveList = regionCodeLinkList.stream().filter(d->d.equals(proveCode)).collect(Collectors.toList()); String cityCode = reginCode.substring(0,4); List<String> cityList = regionCodeLinkList.stream().filter(d->d.equals(cityCode)).collect(Collectors.toList()); if((cityList != null && cityList.size()>0) || (proveList != null && proveList.size()>0)){ //如果已经存在上一级、上上级则跳过添加 continue; }else { regionCodeLinkList.add(reginCode); } break; } default:break; } } for (String str:regionCodeLinkList) { regionCodeList.add(str); } } return regionCodeList; }
标签:String,regionCodeLinkList,最高级,substring,reginCode,sql,proveList,List,like From: https://www.cnblogs.com/ht-privete-logs/p/17174310.html