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

时间:2021-05-19

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

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章