java使用list实现数据库的like功能
本文导语: 使用list实现类似数据库的like,between,精确查找方法 代码如下:public final class CommonLocalQuery { //传入数据查询后返回list, 第一个参数:被查询列表,第二个参数:查询条件 public List queryData(final List mAllList, final HashHashMap map) { ...
使用list实现类似数据库的like,between,精确查找方法
public final class CommonLocalQuery {
//传入数据查询后返回list, 第一个参数:被查询列表,第二个参数:查询条件
public List queryData(final List mAllList, final HashHashMap map) {
if(mAllList == null | map == null) {
return new ArrayList();
}
if(mAllList.isEmpty()) {
return mAllList;
}
/* 根据实际需要找出符合的查询条件 */
Set key1 = map.getFirstKeys();
for (POLICY policy : key1) {
ConcurrentHashMap tempMap = map.get(policy);
Set 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 endKey1 = map.getFirstKeys();
List listResult = new ArrayList();
for (T resMap : mAllList) {
for (POLICY policy : endKey1) {
ConcurrentHashMap 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 resMap, POLICY policy, Map queryMap);
}
//精确查找方式
class FilterNormal extends Filter {
@Override
boolean doFilter(Map resMap, POLICY policy, Map queryMap) {
// TODO Auto-generated method stub
if (policy.name().equalsIgnoreCase(POLICY.NORMAL.name())) {
Iterator iterator = queryMap.entrySet().iterator();
while(iterator.hasNext()) {
Entry 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 resMap, POLICY policy, Map queryMap) {
// TODO Auto-generated method stub
if (policy.name().equalsIgnoreCase(POLICY.BETWEEN.name())) {
Iterator iterator = queryMap.entrySet().iterator();
while(iterator.hasNext()) {
Entry 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 resMap, POLICY policy, Map queryMap) {
// TODO Auto-generated method stub
if (policy.name().equalsIgnoreCase(POLICY.FUZZY.name())) {
String tempStr = resMap.values().toString().replace(" ", "").replace(",", "");
Iterator iterator = queryMap.entrySet().iterator();
while(iterator.hasNext()) {
Entry 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 {
private ConcurrentHashMap k1k2vMap;
public HashHashMap() {
this.k1k2vMap = new ConcurrentHashMap();
}
public void put(K1 key1, K2 key2, V value) {
if (k1k2vMap.containsKey(key1)) {
ConcurrentHashMap k2vMap = k1k2vMap.get(key1);
k2vMap.put(key2, value);
} else {
ConcurrentHashMap k2vMap = new ConcurrentHashMap();
k2vMap.put(key2, value);
k1k2vMap.put(key1, k2vMap);
}
}
public Set getFirstKeys() {
return k1k2vMap.keySet();
}
public V get(K1 key1, K2 key2) {
ConcurrentHashMap k2_v = k1k2vMap.get(key1);
return k2_v == null ? null : k2_v.get(key2);
}
public ConcurrentHashMap 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 k2vMap : k1k2vMap.values()) {
k2vMap.clear();
}
k1k2vMap.clear();
}
}
}
//具体使用方式
HashHashMap map = new HashHashMap();
for(int i = 0; i < queryKey.length; i++){
map.put(POLICY.NORMAL, queryKey[i], queryValue[i]);
}
List mTempList = new CommonLocalQuery(