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

时间:2021-02-23 21:46:11

I have a tree map

我有一张地图

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

and

 String searchString;

I want to print all the values of Tree map which Starts with or equals searchString

我要打印所有树映射的值,它以or = searchString开始

For example Tree map has following key value pair

例如,树映射有以下键值对

v1 abc
v2 abd
v3 bcd
v4 bad

and searchString is a

和searchString

So output should be

所以输出应该是

v1 v2 

as both of them start with a v4 should not be part of output as it does not starts with a or equals to a Here is complete code . No output is coming after i enter search key

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

 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 个解决方案

#1


3  

This should work:

这应该工作:

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;
        }
    }
}

Output:

输出:

v1
v2

#2


0  

I think you can do this by iterating over your entry set in the tree map and testing for your search string using the contains method of hash set to identify which keys have values containing your search word.

我认为您可以通过遍历树映射中的条目集并使用包含哈希集的方法测试搜索字符串,以确定哪些键具有包含搜索词的值。

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
  }
}

#1


3  

This should work:

这应该工作:

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;
        }
    }
}

Output:

输出:

v1
v2

#2


0  

I think you can do this by iterating over your entry set in the tree map and testing for your search string using the contains method of hash set to identify which keys have values containing your search word.

我认为您可以通过遍历树映射中的条目集并使用包含哈希集的方法测试搜索字符串,以确定哪些键具有包含搜索词的值。

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
  }
}