ITeye博客

通过KEY模糊搜索MAP取值

wangxuliangboy · 2008-12-12 11:29 · 企业架构 · Markdown

public class LikeHashMap extends HashMap {

 public Set keySet() {
  Set set = super.keySet();
  TreeSet tSet = null;
  if (set != null) {
   // 对已存在的key进行排序
   tSet = new TreeSet(set);
  }
  return tSet;
 }

 public List<Object> get(String key,boolean like) {
  List<Object> value = new ArrayList<Object>();
  //是否为模糊搜索
  if(like){
   List<String> keyList = new ArrayList<String>();
   TreeSet<String> treeSet = (TreeSet) this.keySet();
   for (String string : treeSet) {
    //通过排序后,key是有序的.
    if (string.indexOf(key) != -1) {
     keyList.add(string);
     value.add(this.get(string));
    } else if (string.indexOf(key) == -1 && keyList.size() == 0) {
     //当不包含这个key时而且key.size()等于0时,说明还没找到对应的key的开始
     continue;
    } else {
     //当不包含这个key时而且key.size()大于0时,说明对应的key到当前这个key已经结束.不必要在往下找
     break;
    }
   }
   keyList.clear();
   keyList=null;
  }else{
   value.add(this.get(key));
  }
  return value;
 }

 public static void main(String[] args) {
  LikeHashMap hMap = new LikeHashMap();
  for (int i = 0; i < 100000; i++) {
   hMap.put("A_"+i, "AAAAAA"+i);
  }
  long time=System.currentTimeMillis();
  System.out.println(hMap.get("A",true).size());
  System.out.println(System.currentTimeMillis()-time);
 }

}

评论(5)

  1. #5 楼 · rmn190 · 2009-02-20 09:11

    xbmujfly writes:

    我试了一下,在我机子上运行你的代码平均时间是219,我自己改了一下,平均运行时间187,代码如下(机子配置:4G内存,AMD4000+): package cn.xbmu.lib.jfly.test;

    import java.util.*;

    /**

    • 模糊搜索HashMap
    • @author JFly */ public class MyLikeHashMap<K, V> extends HashMap<K, V> {

        public List<V> get(String key, boolean like) {         List<V> list = null;         if (like) {             list = new ArrayList<V>();             K[] a = null;             Set<K> set = this.keySet();             a = (K[])set.toArray();             Arrays.sort(a, null);

                for (int i = 0; i < a.length; i++) {                 if (a[i].toString().indexOf(key) == -1) {                     continue;                 } else {                     list.add(this.get(a[i]));                 }             }         }         return list;     }

        public static void main(String[] args) {         MyLikeHashMap<String, String> mh = new MyLikeHashMap<String, String>();         for (int i = 0; i < 100000; i++) {             mh.put("A_" + i, "AAAAAA" + i);         }         long time = System.currentTimeMillis();         System.out.println(mh.get("A", true).size());         System.out.println(System.currentTimeMillis() - time);     } }

    呵呵,我在现在公司用的机子上跑你们两的程序, 楼主的耗时1437, 你的耗时765.

    公司的机子还在原始社会呀.

  2. #4 楼 · rmn190 · 2009-02-20 08:29

    自己研究研究.

  3. #3 楼 · wangxuliangboy · 2008-12-18 11:21

    Arrays.sort(a, null); 不能注释掉呀...如果不是有序的话..取得结果会有问题

  4. #2 楼 · xbmujfly · 2008-12-18 02:38

    呵呵,注释掉Arrays.sort(a, null); 运行时间都到47了!

  5. #1 楼 · xbmujfly · 2008-12-18 02:26

    我试了一下,在我机子上运行你的代码平均时间是219,我自己改了一下,平均运行时间187,代码如下(机子配置:4G内存,AMD4000+): package cn.xbmu.lib.jfly.test;

    import java.util.*;

    /**

    • 模糊搜索HashMap

    • @author JFly */ public class MyLikeHashMap<K, V> extends HashMap<K, V> {

      public List get(String key, boolean like) { List list = null; if (like) { list = new ArrayList(); K a = null; Set set = this.keySet(); a = (K)set.toArray(); Arrays.sort(a, null);

           for (int i = 0; i < a.length; i++) {
               if (a*.toString().indexOf(key) == -1) {
                   continue;
               } else {
                   list.add(this.get(a*));
               }
           }
       }
       return list;
      

      }

      public static void main(String args) { MyLikeHashMap<String, String> mh = new MyLikeHashMap<String, String>(); for (int i = 0; i < 100000; i++) { mh.put("A_" + i, "AAAAAA" + i); } long time = System.currentTimeMillis(); System.out.println(mh.get("A", true).size()); System.out.println(System.currentTimeMillis() - time); } }

评论功能维护中,当前仅展示历史评论。