/** * 转换JSONObject * * @param list * @return */ private JSONObject parseJson(LocationIndex index) { JSONObject location = new JSONObject(); location.put("id", index.getId()); location.put("x", index.getX()); location.put("y", index.getY()); location.put("description", StringUtils.join(index.getDescription(), " ")); return location; }
/** * 构建结果,去掉重复结果,从前向后优先级降低 * * @param exactResult * @param leadResult * @param fuzzyResult * @return */ private JSONObject buildResult( List<LocationIndex> exactList, List<LocationIndex> leadList, List<LocationIndex> fuzzyList) { JSONArray exactResult = parseJson(exactList); JSONArray leadResult = new JSONArray(); JSONArray fuzzyResult = new JSONArray(); // 去掉重复结果 int length = exactList.size(); Set<String> ids = new HashSet<String>(length); for (int i = 0; i < length; i++) { ids.add(exactList.get(i).getId()); } length = leadList.size(); Set<String> idsPlus = new HashSet<String>(); for (int i = 0; i < length; i++) { LocationIndex index = leadList.get(i); if (!ids.contains(index.getId())) { leadResult.add(parseJson(index)); idsPlus.add(index.getId()); } } ids.addAll(idsPlus); length = fuzzyList.size(); for (int i = 0; i < length; i++) { LocationIndex index = fuzzyList.get(i); if (!ids.contains(index.getId())) { fuzzyResult.add(parseJson(index)); } } idsPlus = null; ids = null; exactList = null; leadList = null; fuzzyList = null; JSONObject result = new JSONObject(); result.put("exactResult", exactResult); result.put("leadResult", leadResult); result.put("fuzzyResult", fuzzyResult); return result; }