- Член од
- 28 мај 2008
- Мислења
- 3.966
- Поени од реакции
- 4.073
Здраво дечки, задачкава е со хеш, ама по се изгледа hashCode() не ми го зима и покрај тоа што ми е override-нато, т.е си ги сместува елементите во хешот со помош на default-ното, а сакам со моја hashCode() која се наоѓа во класата Lekche, ама не ги внесува. Некој идеа? во прилог цела задача
Код:
package lekovi;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
class SLLNode<E> {
protected E element;
protected SLLNode<E> succ;
public SLLNode(E elem, SLLNode<E> succ) {
this.element = elem;
this.succ = succ;
}
@Override
public String toString() {
return element.toString();
}
}
class MapEntry<K extends Comparable<K>, E> implements Comparable<K> {
// Each MapEntry object is a pair consisting of a key (a Comparable
// object) and a value (an arbitrary object).
K key;
E value;
public MapEntry(K key, E val) {
this.key = key;
this.value = val;
}
public int compareTo(K that) {
// Compare this map entry to that map entry.
@SuppressWarnings("unchecked")
MapEntry<K, E> other = (MapEntry<K, E>) that;
return this.key.compareTo(other.key);
}
public String toString() {
return "<" + key + "," + value + ">";
}
}
class CBHT<K extends Comparable<K>, E> {
// An object of class CBHT is a closed-bucket hash table, containing
// entries of class MapEntry.
private SLLNode<MapEntry<K, E>>[] buckets;
@SuppressWarnings("unchecked")
public CBHT(int m) {
// Construct an empty CBHT with m buckets.
buckets = (SLLNode<MapEntry<K, E>>[]) new SLLNode[m];
}
private int hash(K key) {
// Translate key to an index of the array buckets.
return Math.abs(key.hashCode()) % buckets.length;
}
public SLLNode<MapEntry<K, E>> search(K targetKey) {
// Find which if any node of this CBHT contains an entry whose key is
// equal
// to targetKey. Return a link to that node (or null if there is none).
int b = hash(targetKey);
for (SLLNode<MapEntry<K, E>> curr = buckets[b]; curr != null; curr = curr.succ) {
if (targetKey.equals(((MapEntry<K, E>) curr.element).key))
return curr;
}
return null;
}
public void insert(K key, E val) { // Insert the entry <key, val> into this
// CBHT.
System.out.println("Insert begin");
MapEntry<K, E> newEntry = new MapEntry<K, E>(key, val);
int b = hash(key);
for (SLLNode<MapEntry<K, E>> curr = buckets[b]; curr != null; curr = curr.succ) {
if (key.equals(((MapEntry<K, E>) curr.element).key)) {
// Make newEntry replace the existing entry ...
curr.element = newEntry;
return;
}
}
// Insert newEntry at the front of the 1WLL in bucket b ...
buckets[b] = new SLLNode<MapEntry<K, E>>(newEntry, buckets[b]);
System.out.println("Insert end");
}
public void delete(K key) {
// Delete the entry (if any) whose key is equal to key from this CBHT.
int b = hash(key);
for (SLLNode<MapEntry<K, E>> pred = null, curr = buckets[b]; curr != null; pred = curr, curr = curr.succ) {
if (key.equals(((MapEntry<K, E>) curr.element).key)) {
if (pred == null)
buckets[b] = curr.succ;
else
pred.succ = curr.succ;
return;
}
}
}
public String toString() {
String temp = "";
for (int i = 0; i < buckets.length; i++) {
temp += i + ":";
for (SLLNode<MapEntry<K, E>> curr = buckets[i]; curr != null; curr = curr.succ) {
temp += curr.element.toString() + " ";
}
temp += "\n";
}
return temp;
}
}
class Lekche implements Comparable<Lekche> {
public String ime;
public int pozitivnaLista;
public int cena;
public int kolicina;
public Lekche(String ime, int pozitivnaLista, int cena, int kolicina) {
this.ime = ime;
this.pozitivnaLista = pozitivnaLista;
this.cena = cena;
this.kolicina = kolicina;
}
public Lekche(String ime) {
super();
this.ime = ime;
}
@Override
public int hashCode() {
System.out.println("hashCode from Lekche");
int hash = 0;
for (int i = 0; i < ime.length(); i++)
hash += 7 * 31 + ime.charAt(i);
return hash;
}
public boolean equals(Object obj) {
Lekche lek = (Lekche) obj;
return ime.equals(lek.ime);
}
public void presmetaj(int kolic) {
if (kolic <= kolicina) {
kolicina = kolicina - kolic;
System.out.println("Napravena e naracka");
} else {
System.out.println("Nema dovolno lekovi");
}
}
@Override
public int compareTo(Lekche o) {
return this.ime.compareTo(o.ime);
}
}
public class Lekovi {
/**
* @param args
* the command line arguments
* @throws InterruptedException
*/
public static void main(String[] args) throws IOException,
InterruptedException {
// TODO code application logic here
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String ime;
int kolicina;
int N = 10;// /Integer.parseInt(br.readLine());
CBHT<Lekche, Lekche> tabela = new CBHT<Lekche, Lekche>(N * 2);
for (int i = 0; i < N; i++) {
String[] niza = new String[] { "lek" + i, "1", "1", "1" };// br.readLine().split(" ");
Lekche val = new Lekche(niza[0], Integer.parseInt(niza[1]),
Integer.parseInt(niza[2]), Integer.parseInt(niza[3]));
tabela.insert(val, val);
}
// System.out.println(tabela);
int i = 0;
while (true) {
ime = "lek" + i;// br.readLine();
if (i == 20)
ime = "KRAJ";
if (ime.matches("KRAJ"))
break;
kolicina = 1;// Integer.parseInt(br.readLine());
SLLNode<MapEntry<Lekche, Lekche>> l = tabela
.search(new Lekche(ime));
if (l == null)
System.out.println("Ne postoi takov lek");
else {
Lekche lek = l.element.value;
lek.presmetaj(kolicina);
System.out
.println("od lekot: " + lek.ime
+ " ima preostanato uste " + lek.kolicina
+ " kolicina");
}
Thread.currentThread().sleep(2000);
i++;
}
}
}
1. int b = hash(key);
2. Math.abs(key.hashCode()) % buckets.length;
Во првиот чекор бараш хаширање на key-то и во вториот се извршува. Бидејќи тебе key-то ти е декларирано како String, се повикува hashCode од класата String, a не од Lekche.