java應用list完成數據庫的like功效。本站提示廣大學習愛好者:(java應用list完成數據庫的like功效)文章只能為提供參考,不一定能成為您想要的結果。以下是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>>(