欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-02-25 15:42:33
使用list实现类似数据库的like,between,精确查找方法 复制代码 代码如下:public final class commonlocalquery

使用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>>(