How to search as LIKe operator in sql in hash map in java

I want to search for a hash map based on user input. Suppose the user gives the value "A", I have to show starting with the name of the company, and if the user gives the value "AB", I have to show starting with the name of the company AB. I store the company name in a hash map

+5
source share
4 answers
  • Use NavigableSet .

    Example:

    NavigableSet<String> company=new TreeSet<String>(); 
    Set<String> filteredSet=company.tailSet(prefix);
    for(String str:filteredSet) {
     if(str.startsWith(prefix))
      //add to list
     else
      break;
    }
    
  • Use the tree [wiki] or trie [wiki] if you are interested in performance. The radix base is more efficient than trie.

+10
source

, , .

:

  • . , , .
  • trie ( ), node - " " node, .
+3

. :

final String searchPrefix = "AB";
for(String key : map.keySet()){
    if(key.startsWith(searchPrefix)){
        System.out.println(map.get(key));
    }
}

.

final String searchPrefix = "AB";
for(Entry<String,String> e : map.entrySet()){
    if(e.getKey().startsWith(searchPrefix)){
        System.out.println(e.getValue());
    }
}
+3

( )

http://download.oracle.com/javase/tutorial/essential/regex/

your company names are strings you can use

String regex = "A*";
myString.matches(regex);
0
source

All Articles