Java uses list to implement the like function of database _java

Source: Internet
Author: User

Using list to implement similar database Like,between, accurate lookup method

Copy Code code as follows:

Public final class Commonlocalquery<t extends Map<string, object>> {

Returns the list after the incoming data query, the first parameter: The query, the second parameter: query criteria
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;
}

* * According to the actual need to find the appropriate query conditions
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);
}
}
}
Responsibility chain design pattern to find
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}
}

/*======== responsibility Chain abstract Classes ======*/
Abstract class Filter {

 filter filter;

 public void SetFilter (filter filter) {
  this.filter = filter;
&NBSP}

 public filter GetFilter () {
  return filter;
&NBSP}

 abstract boolean dofilter (map<string, object> resmap, POLICY POLICY, map<string, String > QueryMap);
}
//Exact lookup method
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 Lookup method
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;
}
}
Fuzzy Lookup method
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);
&NBSP;&NBSP}
  return true;
 }
}


Help class implementation 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 (k1k2 Vmap.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 ();
       }
   }
}

How to use the concrete
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.