Partial Search from Hash Table in Java

This section illustrates you how to put some string in the Hash Table through the Java program and get these string easily.

Ads

Tutorials   
How to read properties file in Java? Generate random numbers in Java Cloneable Interface in Java What is the flaw with the Stack class? List In Java Java Stack Example HashSet In Java Enumeration in java How to generate random number in java Iterator in java Queue in java Transient Java Keyword clone method in Java Java Queue example Logger in Java Java current date - How to get current date in Java Collection : LinkedList Example Collection : ArrayList Example Random alpha numeric string of fixed size Java List Iterator Example The Hashtable Class The BitSet class Java Vector Java Enumeration Data Structures in Java Java Transient Variable Java Date Format Example Java Properties File Example Map.Entry Java Interface Java Property File java preferences,Change the value of Preferences by user Retrieving a Preference Node Java hasNext Write Keys and Values to the Properties file in Java Get All Keys and Values of the Properties files in Java Properties file in Java Properties file in Java Determining If a Preference Node Contains a Specific Value Change the value of Preferences by user Clone method example in Java Implementing a Simple Event Notifier Determining When a Preference Node Is Added or Removed Listening for Changes to Preference Values in a Preference Node Importing Preferences Finding a Preference in a Preference Tree Retrieving the Parent and Child Nodes of a Preference Node Determining If a Preference Node Exists Removing a Preference Node Creating a Preference Node Getting the Maximum Size of a Preference Key and Value
Ads

Partial Search from Hash Table in Java

     

This section illustrates you how to put some string in the Hash Table through the Java program and get these string easily. Here, you will see that the given string which is mentioned with the command "java <file name>" in the command line.

Program Description:

In this section, an example is given with the complete code, which provides you to put some string in the Hash Table of Java and get it for searching. This is done by mentioning a string that has to be searched. This program searches these strings, which are started with the given string or character. For example, if you mention the string "h" then the program searches those strings which are started with the given character or string "h" like "hello" etc.

Here is the code of the program:

import java.util.*;

public class PartialSearcher{
  Hashtable hash;
  String[] sortedArray;
  public static void main(String args[]){
  Hashtable<String,Object> h = new Hashtable<String,Object>();
  h.put("hello"new Integer(1));
  h.put("hell"new Integer(2));
  h.put("alpha"new Integer(3));
  h.put("bye"new Integer(4));
  h.put("hello2"new Integer(5));
  h.put("solly"new Integer(6));
  h.put("sally"new Integer(7));
  h.put("silly"new Integer(8));
  h.put("zorro"new Integer(9));
  h.put("hi"new Integer(10));
  PartialSearcher p = new PartialSearcher(h);
  if(args.length == 0){
  System.out.println("Usage: Search string missing.");
  return;
  }
  else{
  Object[] objs = p.match(args[0]);
  for(int i = 0; i < objs.length; i++)
  System.out.println(objs[i]);
  }
  }
  
  public PartialSearcher(Hashtable h){
  hash = h;
  createSortedArray();
  }
  
  public Object[] match(String s){
  int startIdx = 
  binarySearch
(sortedArray, s, 0, sortedArray.length-1);
  int endIdx = 
binarySearch
(sortedArray, s+ '\uFFFF'0, sortedArray.length-1);
  Object[] objs = new Object[endIdx-startIdx];
  for (int i = startIdx ; i < endIdx; i++)
  objs[i-startIdx= sortedArray[i];
  return objs;
  }
  
  public void createSortedArray(){
  sortedArray = new String[hash.size()];
  Enumeration e = hash.keys();
  for(int i = 0; e.hasMoreElements(); i++)
  sortedArray[i(String)e.nextElement();
  quicksort(sortedArray, 0, sortedArray.length-1);
  }

  public static int binarySearch(String[] arr, 
  String elem, 
int fromIndex, int toIndex){
  int mid,cmp;
  while (fromIndex <= toIndex){
  mid =(fromIndex + toIndex)/2;
  if((cmp = arr[mid].compareTo(elem)) 0)
  fromIndex = mid + 1;
  else if(cmp > 0)
  toIndex = mid - 1;
  else
  return mid;
  }
  return fromIndex;
  }

  public void quicksort(String[] arr, int lo, int hi){
  if(lo >= hi)
  return;
  int mid = (lo + hi2;
  String tmp;
  String middle = arr[mid];
  if(arr[lo].compareTo(middle0){
  arr[mid= arr[lo];
  arr[lo= middle;
  middle = arr[mid];
  }
  if(middle.compareTo(arr[hi]) 0){
  arr[mid= arr[hi];
  arr[hi= middle;
  middle = arr[mid];
  if(arr[lo].compareTo(middle0){
  arr[mid= arr[lo];
  arr[lo= middle;
  middle = arr[mid];
  }
  }
  int left = lo + 1;
  int right = hi - 1;
  if(left >= right)
  return;
  for;; ){
  while(arr[right].compareTo(middle 0){
  right--;
  }
  while(left < right && arr[left].compareTo(middle<= 0){
  left++;
  }
  if(left < right){
  tmp = arr[left];
  arr[left= arr[right];
  arr[right= tmp;
  right--;
  }
  else{
  break;
  }
  }
  quicksort(arr, lo, left);
  quicksort(arr, left + 1, hi);
  }
}  

Download this example.

Advertisements

Share on Google+Share on Google+

Partial Search from Hash Table in Java

Posted on: April 16, 2007 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

Ads
 

Discuss: Partial Search from Hash Table in Java  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:1
kyla
November 26, 2012
java

thankSs

Ads

 

Ads