---
title: "通过KEY模糊搜索MAP取值"
author: "wangxuliangboy"
date: "2008-12-12T11:29:36Z"
tags: []
categories: ["企业架构"]
slug: "key-map"
draft: false
lastmod: "2008-12-12T11:36:19Z"
id: 291268
url: "https://wangxuliangboy.iteye.com/blog/291268"
---

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&lt;Object&gt; get(String key,boolean like) {  
  List&lt;Object&gt; value = new ArrayList&lt;Object&gt;();  
  //是否为模糊搜索  
  if(like){  
   List&lt;String&gt; keyList = new ArrayList&lt;String&gt;();  
   TreeSet&lt;String&gt; 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 &lt; 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);  
 }

}