服务器之家

服务器之家 > 正文

java使用list实现数据库的like功能

时间:2019-11-17 14:17     来源/作者:java教程网

使用list实现类似数据库的like,between,精确查找方法

 

复制代码代码如下:

public final class CommonLocalQuery<T extends Map<String, Object>> {

 //传入数据查询后返回list, 第一个参数:被查询列表,第二个参数:查询条件
 public List<T> queryData(final List<T> mAllList, final HashHashMap<POLICY, String, String> map) {

  if(mAllList == null | map == null) {
   return new ArrayList<T>();
  }

  if(mAllList.isEmpty()) {
   return mAllList;
  }

  /* 根据实际需要找出符合的查询条件 */
  Set<POLICY> key1 = map.getFirstKeys();
  for (POLICY policy : key1) {
   ConcurrentHashMap<String, String> tempMap = map.get(policy);
   Set<String> key2 = tempMap.keySet();
   for (String key : key2) {
    if(key.startsWith("-") || tempMap.get(key) == null ||
      (tempMap.get(key) != null && tempMap.get(key).equals(""))) {
     tempMap.remove(key);
    }
   }
  }
// 责任链设计模式进行查找
  Filter filterNormal, filterBetween, filterFuzzy;
  filterNormal = new FilterNormal();
  filterBetween = new FilterBetween();
  filterFuzzy = new FilterFuzzy();
  filterNormal.setFilter(filterBetween);
  filterBetween.setFilter(filterFuzzy);
  Set<POLICY> endKey1 = map.getFirstKeys();
  List<T> listResult = new ArrayList<T>();
  for (T resMap : mAllList) {
   for (POLICY policy : endKey1) {
    ConcurrentHashMap<String, String> queryMap = map.get(policy);
    if (filterNormal.doFilter(resMap, policy, queryMap) && listResult.contains(resMap)) {
     listResult.add(resMap);
    }
   }
  }
  return listResult;
 }

 public static enum POLICY { NORMAL, BETWEEN, FUZZY }
}

 

/*======== 责任链抽象类 ======*/
abstract class Filter {

 Filter filter;

 public void setFilter(Filter filter) {
  this.filter = filter;
 }

 public Filter getFilter() {
  return filter;
 }

 abstract boolean doFilter(Map<String, Object> resMap, POLICY policy, Map<String, String> queryMap);
}
//精确查找方式
class FilterNormal extends Filter {

 @Override
 boolean doFilter(Map<String, Object> resMap, POLICY policy, Map<String, String> queryMap) {
  // TODO Auto-generated method stub
  if (policy.name().equalsIgnoreCase(POLICY.NORMAL.name())) {
   Iterator<Entry<String, String>> iterator = queryMap.entrySet().iterator();
   while(iterator.hasNext()) {
    Entry<String, String> entry = iterator.next();
    if(!resMap.get(entry.getKey()).toString().contains(entry.getValue())) {
     return false;
    }
   }
  } else if (filter != null) {
   return filter.doFilter(resMap, policy, queryMap);
  }
  return true;
 }
}
//between查找方式
class FilterBetween extends Filter {

 @Override
 boolean doFilter(Map<String, Object> resMap, POLICY policy, Map<String, String> queryMap) {
  // TODO Auto-generated method stub
  if (policy.name().equalsIgnoreCase(POLICY.BETWEEN.name())) {
   Iterator<Entry<String, String>> iterator = queryMap.entrySet().iterator();
   while(iterator.hasNext()) {
    Entry<String, String> entry = iterator.next();
    if(!Pattern.matches(".+?Φ.+?", entry.getValue())) 
     throw new UnknownError("Values should be .+?Φ.+? pattern");
    String firstValue = entry.getValue().split("Φ")[0];
    String secondValue = entry.getValue().split("Φ")[1];
    if(resMap.get(entry.getKey()).toString().compareTo(firstValue) < 0 
      || resMap.get(entry.getKey()).toString().compareTo(secondValue) > 0) {
     return false;
    }
   }
  } else if (filter != null) {
   return filter.doFilter(resMap, policy, queryMap);
  }
  return true;
 }
}
//模糊查找方式
class FilterFuzzy extends Filter {

 @Override
 boolean doFilter(Map<String, Object> resMap, POLICY policy, Map<String, String> queryMap) {
  // TODO Auto-generated method stub
  if (policy.name().equalsIgnoreCase(POLICY.FUZZY.name())) {
   String tempStr = resMap.values().toString().replace(" ", "").replace(",", "");
   Iterator<Entry<String, String>> iterator = queryMap.entrySet().iterator();
   while(iterator.hasNext()) {
    Entry<String, String> entry = iterator.next();
    if(tempStr.substring(1, tempStr.length()-1).contains(entry.getValue())) {
     return true;
    }
   }
  } else if (filter != null) {
   return filter.doFilter(resMap, policy, queryMap);
  }
  return true;
 }
}


//帮助类实现 k1-k2-v
public class HashHashMap<K1, K2, V> {

    private ConcurrentHashMap<K1, ConcurrentHashMap<K2, V>> k1k2vMap;

    public HashHashMap() {
        this.k1k2vMap = new ConcurrentHashMap<K1, ConcurrentHashMap<K2, V>>();
    }

    public void put(K1 key1, K2 key2, V value) {
        if (k1k2vMap.containsKey(key1)) {
            ConcurrentHashMap<K2, V> k2vMap = k1k2vMap.get(key1);
            k2vMap.put(key2, value);
        } else {
            ConcurrentHashMap<K2, V> k2vMap = new ConcurrentHashMap<K2, V>();
            k2vMap.put(key2, value);
            k1k2vMap.put(key1, k2vMap);
        }
    }

    public Set<K1> getFirstKeys() {
        return k1k2vMap.keySet();
    }

    public V get(K1 key1, K2 key2) {
        ConcurrentHashMap<K2, V> k2_v = k1k2vMap.get(key1);
        return k2_v == null ? null : k2_v.get(key2);
    }

    public ConcurrentHashMap<K2, V> get(K1 key1) {
        return k1k2vMap.get(key1);
    }

    public boolean containsKey(K1 key1, K2 key2) {
        if (k1k2vMap.containsKey(key1)) {
            return k1k2vMap.get(key1).containsKey(key2);
        }
        return false;
    }

    public boolean containsKey(K1 key1) {
        return k1k2vMap.containsKey(key1);
    }

    public void clear() {
        if (k1k2vMap.size() > 0) {
            for (ConcurrentHashMap<K2, V> k2vMap : k1k2vMap.values()) {
                k2vMap.clear();
            }
            k1k2vMap.clear();
        }
    }
}

//具体使用方式
HashHashMap<POLICY, String, String> map = new HashHashMap<CommonLocalQuery.POLICY, String, String>();
  for(int i = 0; i < queryKey.length; i++){
   map.put(POLICY.NORMAL, queryKey[i], queryValue[i]);
  }
List<Map<String, Object>> mTempList = new CommonLocalQuery<HashMap<String, Object>>(

 

标签:

相关文章

热门资讯

玄元剑仙肉身有什么用 玄元剑仙肉身境界等级划分
玄元剑仙肉身有什么用 玄元剑仙肉身境界等级划分 2019-06-21
男生常说24816是什么意思?女生说13579是什么意思?
男生常说24816是什么意思?女生说13579是什么意思? 2019-09-17
配置IIS网站web服务器的安全策略配置解决方案
配置IIS网站web服务器的安全策略配置解决方案 2019-05-23
Nginx服务器究竟是怎么执行PHP项目
Nginx服务器究竟是怎么执行PHP项目 2019-05-24
运维必须知道的关于云服务器的十个问题
运维必须知道的关于云服务器的十个问题 2019-05-24
返回顶部