Search code examples
javaregextreemap

How to match Tree map value which starts with or equals search string


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

For example Tree map has following key value pair

v1 abc
v2 abd
v3 bcd
v4 bad

and searchString is a

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

 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());

                }
        }   
 }

Solution

  • 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