Don't like ads? PRO users don't see any ads ;-)
Guest

HashingAbrahamLincoln

By: a guest on May 8th, 2012  |  syntax: Java  |  size: 0.66 KB  |  hits: 15  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. package Lesson57;
  2.  
  3. public class HashingAbrahamLincoln {
  4.  
  5.         /**
  6.          * @param args
  7.          */
  8.         public static int hashCode(String s){
  9.                 final int TABLE_SIZE = 180;
  10.                 int hash = 0;
  11.                 s = s.toUpperCase();
  12.                
  13.                 hash += s.charAt(0) * 1000;
  14.                 hash += s.charAt(1) * 100;
  15.                 hash += s.charAt(2) * 10;
  16.                 hash += s.charAt(s.length()-1);
  17.                
  18.                 return hash % TABLE_SIZE;
  19.  
  20.         }
  21.        
  22.        
  23.         public static void main(String[] args) {
  24.                 // TODO Auto-generated method stub
  25.                 String arr[] = new String[] {
  26.                         "Bush, George",
  27.                         "Clinton, Bill",
  28.                         "Lincoln, Abraham",
  29.                         "Washington, George"
  30.                 };
  31.                
  32.                 for(String x : arr){
  33.                         System.out.println(x + ">>>" + hashCode(x));
  34.                 }
  35.         }
  36.  
  37. }