如何匹配以或等于搜索字符串开头的树映射值

时间:2015-05-26 17:32:31

标签: java regex treemap

我有一张树图

    TreeMap<String,HashSet<String>> kewordVideo = new TreeMap<String,HashSet<String>>();

 String searchString;

我想打印Tree map的所有值,其中Starts with或等于searchString

例如,树形图具有以下键值对

v1 abc
v2 abd
v3 bcd
v4 bad

和searchString是

所以输出应该是

v1 v2 

因为它们都以v4开头不应该是输出的一部分,因为它不以a或等于a开头 这是完整的代码。输入搜索键后没有输出

 package cultureMachine;

 import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.InputStreamReader;
 import java.util.HashSet;
 import java.util.Map;
 import java.util.Map.Entry;
 import java.util.Set;
 import java.util.TreeMap;

 public class CultureMachineAssignment {

 TreeMap<String,HashSet<String>> kewordVideo = new      TreeMap<String,HashSet<String>>();
TreeMap<String,HashSet<String>> videoKeyword =  new TreeMap<String,HashSet<String>>();
TreeMap<String,Integer> keywordLength = new TreeMap<String,Integer>();

   public static void main(String args[]) throws IOException {

     CultureMachineAssignment obj1 = new CultureMachineAssignment();


     Integer previousVal=0;
     InputStreamReader ip = new InputStreamReader(System.in);
     BufferedReader br = new BufferedReader(ip);

     for(int i=0;i<5;i++){
        System.out.println("Enter Video name");
        String video =br.readLine();


        if(!obj1.videoKeyword.containsKey(video)){
            obj1.videoKeyword.put(video,new HashSet<String>());
        }

        System.out.println("Enter keywords for video");
        String keyword =br.readLine();

        if(!obj1.keywordLength.containsKey(video))
            obj1.keywordLength.put(video, 0);

        if((obj1.keywordLength.get(video)+keyword.length())<5){
            obj1.videoKeyword.get(video).add(keyword);
            previousVal=obj1.keywordLength.get(video);
            obj1.keywordLength.put(video, previousVal+keyword.length());
        }
        else{
            System.out.println("Maximum length exceeded for video "+ video);
            break;
        }
        if(!obj1.kewordVideo.containsKey(keyword)){
            obj1.kewordVideo.put(keyword,new HashSet<String>());
        }
        obj1.kewordVideo.get(keyword).add(video);


    }
    for(Map.Entry m:obj1.videoKeyword.entrySet()){  
        System.out.println(m.getKey()+" "+m.getValue());  
    }
    System.out.println("Enter keyword to search video");
    String searchKey = br.readLine();
    for(Entry<String,HashSet<String>> entry : obj1.kewordVideo.entrySet()){    
                  if(entry.getValue().contains(searchKey))
                     System.out.println(entry.getKey());

                }
        }   
 }

2 个答案:

答案 0 :(得分:3)

这应该有效:

TreeMap<String,HashSet<String>> videoKeyword = new TreeMap<String,HashSet<String>>();
videoKeyword.put("v1", new HashSet<String>(Arrays.asList("abc")));
videoKeyword.put("v2", new HashSet<String>(Arrays.asList("abd")));
videoKeyword.put("v3", new HashSet<String>(Arrays.asList("bcd")));
videoKeyword.put("v4", new HashSet<String>(Arrays.asList("bad")));

String searchString = "a";

for (Entry<String, HashSet<String>> entry : videoKeyword.entrySet()) {
    for (String s : entry.getValue()) {
        if (s.startsWith(searchString)) {
            System.out.println(entry.getKey());
            break;
        }
    }
}

输出:

v1
v2

答案 1 :(得分:0)

我认为您可以通过迭代树形图中的条目集并使用哈希集的contains方法测试搜索字符串来识别哪些键包含包含搜索词的值。

for(Entry<String,HashSet<String>> entry : kewordVideo.entrySet()){     
  if(entry.getValue.contains(searchString){
    //Do something with entry.getKey(), ie add to a list or output to console
  }
}