java使用list实现数据库的like功能
使用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>>(
推荐阅读
-
java使用list实现数据库的like功能
-
java实现的连接oracle/mysql数据库功能简单示例【附oracle+mysql数据库驱动包】
-
Java操作Mongodb数据库实现数据的增删查改功能示例
-
Java中使用fileupload组件实现文件上传功能的实例代码
-
JAVA简单链接Oracle数据库 注册和登陆功能的实现代码
-
Java中使用fileupload组件实现文件上传功能的实例代码
-
JAVA简单链接Oracle数据库 注册和登陆功能的实现代码
-
使用谷歌Gson实现Json数据与Java对象、List、Map之间的相互转换笔记
-
在 Laravel 中使用 Laravel Searchy 扩展包实现基于数据库的轻量级搜索功能
-
Java Web 网络商城案例演示十六 订单详情功能(支付功能的实现使用易付宝)