Java Data Structures
Hash Tables
HashMapsAndHashTables.java:
/**
*
*/
package com.it.gnsmind.datastructures;
import java.util.HashMap;
import java.util.Iterator;
/**
* @author Metin ZAFER
*
*/
public class HashMapsAndHashTables {
/**
*
*/
public HashMapsAndHashTables() {
System.out.println("Hash Map application object has been initialized. ");
}
//hashMap and HashTable basicly the same structured objects
public void hashMapsAndhashTables(){
// Hash maps are used mostly in database applications or web services.
// This structure is very useful because of the dynamic allocation.
// We do not have to specify the index number at the beginning of our application like arrays.
// It gives us very flexible array structure to use in our productive applications.
HashMap h = new HashMap( );
h.put("key1", "Value1");
h.put("key2", "Value2");
h.put("key3", "Value3");
h.put("key4", "Value4");
h.put("key5", "Value5");
// Two versions of the "retrieval" phase.
// Version 1: get one pair's value given its key
String queryString = "key1";
System.out.println("You asked : " + queryString + ".");
String resultString = (String)h.get(queryString);
System.out.println("The Value is located in: " + resultString);
System.out.println( );
// Version 2: get all the keys and pairs
Iterator it = h.keySet( ).iterator( );
while (it.hasNext( )) {
String key = (String) it.next( );
System.out.println("The Hash Map Key : " + key + "; " + "- Value : " + h.get(key));
}
}
}
ScreenShot:
HashMapsAndHashTables.java:
/**
*
*/
package com.it.gnsmind.datastructures;
import java.util.HashMap;
import java.util.Iterator;
/**
* @author Metin ZAFER
*
*/
public class HashMapsAndHashTables {
/**
*
*/
public HashMapsAndHashTables() {
System.out.println("Hash Map application object has been initialized. ");
}
//hashMap and HashTable basicly the same structured objects
public void hashMapsAndhashTables(){
// Hash maps are used mostly in database applications or web services.
// This structure is very useful because of the dynamic allocation.
// We do not have to specify the index number at the beginning of our application like arrays.
// It gives us very flexible array structure to use in our productive applications.
HashMap h = new HashMap( );
h.put("key1", "Value1");
h.put("key2", "Value2");
h.put("key3", "Value3");
h.put("key4", "Value4");
h.put("key5", "Value5");
// Two versions of the "retrieval" phase.
// Version 1: get one pair's value given its key
String queryString = "key1";
System.out.println("You asked : " + queryString + ".");
String resultString = (String)h.get(queryString);
System.out.println("The Value is located in: " + resultString);
System.out.println( );
// Version 2: get all the keys and pairs
Iterator it = h.keySet( ).iterator( );
while (it.hasNext( )) {
String key = (String) it.next( );
System.out.println("The Hash Map Key : " + key + "; " + "- Value : " + h.get(key));
}
}
}
ScreenShot:
Comments
Post a Comment