如何使用哈希表和桶哈希
How to use hashtable and bucket hashing
所以最近我一直在尝试在 table 中练习散列和使用链表来存储值。我理解这个概念,但我无法将其付诸实践,而且似乎无法在网上找到我要找的东西。
例如:
假设我想使用散列 table 来存储诸如显示器、鼠标等计算机之类的东西。我想要这样的方法:
boolean addMonitor(String id, String description, double price, int units, String size)
boolean addMouse(String id, String description, double price, int units, int buttons)
我不明白如何使用这些方法将它们存储在散列中table。显然,我以后也想使用其他方法来访问和更改每个方法中的值。任何帮助表示赞赏。谢谢。
即使它的名字是 "table",HashTable 也不像 "database table" 你有列,每个列存储值...你似乎想使用 hashtable 作为数据库 table.
Hashtable 存储对象!所以你的方法应该看起来更好:
public class Example {
public static void main(String[] args) {
ItemStore store;
Monitor monitor;
Mouse mouse;
store = new ItemStore();
monitor = new Monitor();
monitor.id = 2;
monitor.price = 6;
mouse = new Mouse();
mouse.id = 7;
mouse.buttons = 3;
store.addItem(monitor);
store.addItem(mouse);
System.out.println(store.getItem(2).price); // = 6
System.out.println(((Monitor) store.getItem(2)).dpi);
System.out.println(((Mouse) store.getItem(7)).buttons); //Downcasting ... = 3
}
public static class Item {
String id;
String description;
int price;
// common attributes here!
}
public static class Monitor extends Item {
private int dpi;
// monitor particular atributes here!!
}
public static class Mouse extends Item {
private int buttons;
// mouse particular attributes here!!!
}
public static class ItemStore {
private Hashtable<String, Item> table = new HashTable<>();
public boolean addItem(Item item) {
this.table.put(item.getId(), item);
}
public Item getItem(String id) {
return this.table.get(id);
}
}
}
所以最近我一直在尝试在 table 中练习散列和使用链表来存储值。我理解这个概念,但我无法将其付诸实践,而且似乎无法在网上找到我要找的东西。
例如: 假设我想使用散列 table 来存储诸如显示器、鼠标等计算机之类的东西。我想要这样的方法:
boolean addMonitor(String id, String description, double price, int units, String size)
boolean addMouse(String id, String description, double price, int units, int buttons)
我不明白如何使用这些方法将它们存储在散列中table。显然,我以后也想使用其他方法来访问和更改每个方法中的值。任何帮助表示赞赏。谢谢。
即使它的名字是 "table",HashTable 也不像 "database table" 你有列,每个列存储值...你似乎想使用 hashtable 作为数据库 table.
Hashtable 存储对象!所以你的方法应该看起来更好:
public class Example {
public static void main(String[] args) {
ItemStore store;
Monitor monitor;
Mouse mouse;
store = new ItemStore();
monitor = new Monitor();
monitor.id = 2;
monitor.price = 6;
mouse = new Mouse();
mouse.id = 7;
mouse.buttons = 3;
store.addItem(monitor);
store.addItem(mouse);
System.out.println(store.getItem(2).price); // = 6
System.out.println(((Monitor) store.getItem(2)).dpi);
System.out.println(((Mouse) store.getItem(7)).buttons); //Downcasting ... = 3
}
public static class Item {
String id;
String description;
int price;
// common attributes here!
}
public static class Monitor extends Item {
private int dpi;
// monitor particular atributes here!!
}
public static class Mouse extends Item {
private int buttons;
// mouse particular attributes here!!!
}
public static class ItemStore {
private Hashtable<String, Item> table = new HashTable<>();
public boolean addItem(Item item) {
this.table.put(item.getId(), item);
}
public Item getItem(String id) {
return this.table.get(id);
}
}
}