Professional Documents
Culture Documents
HASH TABLE
1.
Tujuan
Dasar Teori
3.
Tugas Pendahuluan
1.
2.
4.
Praktikum
Percobaan 1. HashAssociation
package packageHashTable;
class HashAssociation<K extends Number, V> {
protected K theKey;
protected V theValue;
protected K key;
protected V value;
89
HashAssociation() {
this.theKey = key;
this.theValue = value;
}
V setValue(V value) {
V oldValue = theValue;
theValue = value;
return oldValue;
}
K getTheKey() {
return theKey;
}
void setTheKey(K theKey) {
this.theKey = theKey;
}
V getTheValue() {
return theValue;
}
void setTheValue(V theValue) {
this.theValue = theValue;
}
}
Percobaan 2. HashTable
package packageHashTable;
import java.util.Vector;
}
}
public int containsValueByKey(K key) {
int hash = locate(key);
if (data.get(hash) != null)
return -99;
else
return hash;
}
public Vector<HashAssociation<Integer, String>> getData() {
return data;
}
public void setDATA(K key, V value) {
put(key, value);
}
public boolean containsValue(K key) {
int hash = locate(key);
return data.get(hash) != null;
}
public String get(K key) {
int hash = locate(key);
if (data.get(hash) == null)
return null;
return data.get(hash).getTheValue();
}
}
Percobaan 3. TesthashTable
package packageHashTable;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
return this;
}
}
Percobaan 5. HashAssociation
package packageTestVectorIterator;
class HashAssociation<K extends Number, V> {
protected K theKey;
protected V theValue;
protected K key;
protected V value;
HashAssociation() {
this.theKey = key;
this.theValue = value;
}
V setValue(V value) {
V oldValue = theValue;
theValue = value;
return oldValue;
}
K getTheKey() {
return theKey;
}
void setTheKey(K theKey) {
this.theKey = theKey;
}
V getTheValue() {
return theValue;
}
void setTheValue(V theValue) {
this.theValue = theValue;
}
}
Percobaan 6. VectorIterator
package packageTestVectorIterator;
95
import java.util.Iterator;
import java.util.Vector;
@SuppressWarnings("hiding")
class VectorIterator<E> extends AbstractIterator<E> {
protected Vector<E> theVector;
protected int current;
public Iterator<E> iterator()
{
return new VectorIterator<E>();
}
public void reset()
{
current = 0;
}
public boolean hasNext()
{
return current < theVector.size();
}
public E get()
{
return theVector.get(current);
}
public E next()
{
return theVector.get(current++);
}
}
Percobaan 7. TestIteratorforVector
package packageTestVectorIterator;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Iterator;
import java.util.Vector;
public class TestIteratorforVector {
static Vector <String> vector = new Vector<String>();
static Iterator<String> i;
public static void main(String[] args) {
96
vector.add(ha);
}
System.out.print("\nData-data sudah masuk. Mau memeriksa
(1)? ");
int periksa = inputData();
if (periksa == 1)
tampilData();
}
private static void tampilData() {
int j = 1;
for (HashAssociation<Integer, String> ha : vector){
System.out.println("Data ke-" + j + " KEY : "
+ ha.getTheKey().toString() + "
VALUE : "
+ ha.getTheValue().toString());
j++;
}
}
private static String inputNilai() {
String data = null;
BufferedReader bfr = new BufferedReader(
new InputStreamReader(System.in));
String stringInput = null;
try {
stringInput = bfr.readLine();
} catch (IOException e) {
e.printStackTrace();
}
data = String.valueOf(stringInput);
return data;
}
private static int inputData() {
BufferedReader bfr = new BufferedReader(
new InputStreamReader(System.in));
String angkaInput = null;
try {
angkaInput = bfr.readLine();
} catch (IOException e) {
e.printStackTrace();
99
}
int Data = Integer.valueOf(angkaInput).intValue();
return Data;
}
}
5.
Tugas Praktikum
Buat Kesimpulan dengan deskripsi tentang penggunaan struktur data hash table
dari beberapa percobaan diatas
100