package com.test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class Test {
static boolean flag = true;// 默认使用名称去排序
static String msg = "{\"code\":17,\"resut\":1,\"msg\":\"根据商圈管理员ID获取所有的商家\",\"data\":{\"list\":[{\"name\":\"美宜佳\",\"id\":3},{\"name\":\"老王面馆\",\"id\":4},{\"name\":\"大润发\",\"id\":5},{\"name\":\"阿卡丽弄\",\"id\":6},{\"name\":\"555\",\"id\":9},{\"name\":\"好了咯JJ\",\"id\":11},{\"name\":\"路路通\",\"id\":14},{\"name\":\"钟表店\",\"id\":15},{\"name\":\"bloom弄\",\"id\":16},{\"name\":\"上班车啊\",\"id\":18},{\"name\":\"1额考虑欧诺LOL\",\"id\":19}]}}";
public static void main(String[] args) {
// 1、初始化数据
Map<String, Integer> cmap = new HashMap<String, Integer>();
JSONObject fromObject = JSONObject.fromObject(msg);
JSONArray jsonArray = fromObject.getJSONObject("data").getJSONArray("list");
for (Object object : jsonArray) {
JSONObject j = (JSONObject) object;
String name = j.getString("name");
int id = j.getInt("id");
cmap.put(name, id);
}
// 2、加入假数据
for (int i = 0; i < 1000; i++) {
UUID uuid = UUID.randomUUID();
String random = uuid.toString().replaceAll("-", "").substring(0, 8);
int intFlag = (int) (Math.random() * 10000);
cmap.put(random, intFlag);
}
System.out.println("-------------------数据插入完毕----------------------------");
long currentTimeMillis = System.currentTimeMillis();
// 3、模糊匹配数据
Map<String, Integer> likeByMap = getLikeByMap(cmap, "a");
System.out.println("模糊查询到的结果:\n" + likeByMap);
long currentTimeMillis2 = System.currentTimeMillis();
System.out.println("使用时间:" + (currentTimeMillis2 - currentTimeMillis));
}
/**
* Map集合模糊匹配
*
* @param map map集合
* @param keyLike 模糊key
* @return {a1=1,a1899=1899}
*/
public static Map<String, Integer> getLikeByMap(Map<String, Integer> map, String keyLike) {
/** 返回的数据map **/
Map<String, Integer> m = new LinkedHashMap<>();
/** 用于排序数据集合 **/
List<String> l = new LinkedList<>();
// 循环匹配
for (Map.Entry<String, Integer> entity : map.entrySet()) {
String key = entity.getKey();
String integer = map.get(key) + "";
if (key.equals(keyLike)) {// 判断名称完全相等
String a = key + "&" + integer;
l.add(0, a);// 放在第一位
continue;
}
if (integer.equals(keyLike)) {// 判断ID完全相等
String a = key + "&" + integer;
l.add(0, a);// 放在第一位
flag = false;
continue;
}
if (key.indexOf(keyLike) > -1) {// 判断key值
String a = key + "&" + integer;
l.add(a);
}
if (integer.indexOf(keyLike) > -1) {// 判断value
String a = key + "&" + integer;
l.add(a);
}
}
// 将数据长短进行排序
// 将第一个数据取出,不排序
// 判断第一个是否为完全匹配到的
if (null != l) {
if (l.size() > 0) {
String one = l.get(0);
if (one.contains("&")) {
String[] split = one.split("&");
String name = split[0];
String id = split[1];
if (name.equals(keyLike) || id.equals(keyLike)) {
l.remove(0);
} else {
one = null;
}
}
List<String> test = new LinkedList<>();
if (flag) {// 名称
for (String string : l) {
String[] split = string.split("&");
String name = split[0];
test.add(name);
}
} else {// ID
for (String string : l) {
String[] split = string.split("&");
String id = split[1];
test.add(id);
}
}
List<String> sort = Sort(test);
List<String> list = new LinkedList<>();
if (flag) {
for (int i = 0; i < sort.size(); i++) {
String string = sort.get(i).toString();
Integer integer = map.get(string);
list.add(i, string + "&" + integer);
}
} else {
for (int i = 0; i < sort.size(); i++) {
String string = sort.get(i);
for (Map.Entry<String, Integer> entity : map.entrySet()) {
String key = entity.getKey();
String integer = map.get(key) + "";
if (integer.equals(string)) {
list.add(i, key + "&" + integer);
}
}
}
}
if (null != one) {
list.add(0, one);
}
for (String res : list) {
String[] split = res.split("&");
String name = split[0];
String id = split[1];
m.put(name, Integer.valueOf(id));
}
}
}
return m;
}
/**
* 排序
*
* @param stringList
* @return
*/
public static List<String> Sort(List<String> stringList) {
String[] arr = stringList.stream().toArray(String[]::new);
for (int i = 0; i < arr.length - 1; i++) {// 外层循环控制排序趟数
for (int j = 0; j < arr.length - 1 - i; j++) {// 内层循环控制每一趟排序多少次
if (arr[j].length() > arr[j + 1].length()) {
String temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
List<String> succlist = new ArrayList<String>();
for (int i = 0; i < arr.length; i++) {
String string = arr[i];
succlist.add(i, string);
}
return succlist;
}
}
转载于:https://www.cnblogs.com/mojxtang/p/10412230.html