我的讲师为HashEntry
课程和HashMap
课程提供了一些示例代码。我必须在HashMap
中为标准探测实现get和put方法,然后是线性和二次方法。我已经完成了所有这些工作,我可以使用任何探测方法。下一步是读入.csv
文件并解析它,以便第一列是键,其余列是行。这一切都很有效,直到我.csv
的关键值高于有效int
值。我的问题是如何更改提供的代码以使用long
作为密钥而不是int
。
public class HashEntry {
private int key;
private String value;
HashEntry(int key, String value) {
this.key = key;
this.value = value;
}
public int getKey() {
return key;
}
public String getValue() {
return value;
}
public void setValue(String val) {
this.value = val;
}
}
HashMap中
public class HashMap {
private final static int TABLE_SIZE = 1900000;
HashEntry[] table;
HashMap() {
table = new HashEntry[TABLE_SIZE];
for (int i = 0; i < TABLE_SIZE; i++)
table[i] = null;
}
public String get(int key) {
int hash = (key % TABLE_SIZE);
while (table[hash] != null && table[hash].getKey() != key)
hash = (7 * hash + 1) % TABLE_SIZE;
if (table[hash] == null)
return "There is no value at this key";
else
return table[hash].getValue();
}
public void put(int key, String value) {
int hash = (key % TABLE_SIZE);
while (table[hash] != null && table[hash].getKey() != key)
hash = (7 * hash + 1) % TABLE_SIZE;
table[hash] = new HashEntry(key, value);
}
public void linearprobeput(int key, String value){
int i = 0;
int hash = (key % TABLE_SIZE);
//System.out.println("The hash is " + hash+ "\n");
while (table[hash] != null && table[hash].getKey() != key)
{
hash = (hash + i) % TABLE_SIZE;
i++;
//printing was to verify collisions were being managed correctly
//System.out.println("Their was a collision now trying with hash'"
//+ hash +" and an i value of "+ i+ "'\n");
}
table[hash] = new HashEntry(key, value);
}
public String linearprobeget(int key){
int hash = (key % TABLE_SIZE);
int i = 0;
while (table[hash] != null && table[hash].getKey() != key)
{
hash = (hash + i) % TABLE_SIZE;
i++;
}
if (table[hash] == null)
return "There is no value at this key";
else
return table[hash].getValue();
}
public void quadraticprobeput(int key, String value){
int i = 0;
int hash = (key % TABLE_SIZE);
//System.out.println("The hash is " + hash+ "\n");
while (table[hash] != null && table[hash].getKey() != key)
{
hash = (hash + (i*i)) % TABLE_SIZE;
i++;
//printing was to verify collisions were being managed correctly
//System.out.println("Their was a collision now trying with hash'"
//+ hash +" and an i value of "+ i+ "'\n");
}
table[hash] = new HashEntry(key, value);
}
public String quadraticprobeget(int key){
int hash = (key % TABLE_SIZE);
int i = 0;
while (table[hash] != null && table[hash].getKey() != key)
{
hash = (hash + (i*i)) % TABLE_SIZE;
i++;
}
if (table[hash] == null)
return "There is no value at this key";
else
return table[hash].getValue();
}
}
主要
HashMap upchm = new HashMap();
BufferedReader in = new BufferedReader
(new FileReader("UPCtest.csv"));
//Integer a = null;
String str=null;
ArrayList<String> lines = new ArrayList<String>();
//lines.split(",")[1]+
while((str = in.readLine()) != null){
//lines.add(str);
String[] upc= str.split(",");
Integer val = Integer.valueOf(upc[0]);
upchm.quadraticprobeput(val, upc[1]+upc[2]);
System.out.println("UPC = " + val + " Description "+upc[1]+upc[2]);
}
//change arraylist to array
//String[] upc = lines.toArray(new String[lines.size()]);
in.close();
System.out.println("\n");
System.out.println("The description for the product with upc 123 is " + upchm.quadraticprobeget(123));
System.out.println("The description for the product with upc 10344 is " + upchm.quadraticprobeget(10344));
.csv
中某些行的示例79 ,, INDIANA LOTTO 93 ,, treo 700w 123 ,, Wrsi Riversound cafe cd
161,Dillons / Kroger员工优惠券(1.25美元信贷)
2158242769,288 / 1.12Z,GREEN SUGAR COOKIES4276 2158561631 ,, HOT COCOA W / BKMK 2158769549,njhjhn,gjfhjbgkj 2160500567,2.25盎司(64)克,美元 Bar Rich Raspberry 2172307284,混合季节性花束
2177000074 ,, 4路13 AMP Extension Lead(威尔金森英国)2184000098,21 盎司,克里斯托弗的什锦水果果冻2187682888 ,,,球道
15400021142,9 Z,WF BOWL CLEANER(JAR 15400021159,8 Z,WF ANTIBAC LIQ SOAP 15400021166,3 RL,WF DECORATOR VALUE 15400021173,64 Z,WF CLEAR AMMONIA 15400021210,128 Z,WF FAB SOFT 15400021319,28 Z,WF DEGREAS ULT DISH 15400021326,28 Z,WF LEMON ULTRA DISH 15400021340,28 Z,WF ANTIBACT DISH LIQ 15400021364,65 Z,WF AUTO DISH DETERGE
答案 0 :(得分:0)
您必须使用long key
而不是int key
,并在计算表格中的存储桶时转换类型:
HashEntry类
public class HashEntry {
private long key;
private String value;
}
HashMap类
public class HashMap {
private static final int TABLE_SIZE = 1900000;
private final HashEntry[] table;
public HashMap() {
// all elements are null by default
table = new HashEntry[TABLE_SIZE];
}
public String get(long key) {
int bucket = getBucket(key);
while (table[bucket] != null && table[bucket].getKey() != key) {
bucket = (7 * bucket + 1) % TABLE_SIZE;
}
return getValueByBucket(bucket, key);
}
public void put(long key, String value) {
int bucket = getBucket(key);
while (table[bucket] != null && table[bucket].getKey() != key) {
bucket = (7 * bucket + 1) % TABLE_SIZE;
}
table[bucket] = new HashEntry(key, value);
}
public void linearprobeput(long key, String value) {
int i = 0;
int bucket = getBucket(key);
//System.out.println("The hash is " + hash+ "\n");
while (table[bucket] != null && table[bucket].getKey() != key) {
bucket = (bucket + i) % TABLE_SIZE;
i++;
//printing was to verify collisions were being managed correctly
//System.out.println("Their was a collision now trying with hash'"
//+ hash +" and an i value of "+ i+ "'\n");
}
table[bucket] = new HashEntry(key, value);
}
public String linearprobeget(long key) {
int i = 0;
int bucket = getBucket(key);
while (table[bucket] != null && table[bucket].getKey() != key) {
bucket = (bucket + i) % TABLE_SIZE;
i++;
}
return getValueByBucket(bucket, key);
}
public void quadraticprobeput(long key, String value) {
int i = 0;
int bucket = getBucket(key);
//System.out.println("The hash is " + hash+ "\n");
while (table[bucket] != null && table[bucket].getKey() != key) {
bucket = (bucket + (i * i)) % TABLE_SIZE;
i++;
//printing was to verify collisions were being managed correctly
//System.out.println("Their was a collision now trying with hash'"
//+ hash +" and an i value of "+ i+ "'\n");
}
table[bucket] = new HashEntry(key, value);
}
public String quadraticprobeget(long key) {
int i = 0;
int bucket = getBucket(key);
while (table[bucket] != null && table[bucket].getKey() != key) {
bucket = (bucket + (i * i)) % TABLE_SIZE;
i++;
}
return getValueByBucket(bucket, key);
}
private static int getBucket(long key) {
// this is safe, because key % TABLE_SIZE < TABLE_SIZE which is int
return (int)(key % TABLE_SIZE);
}
private String getValueByBucket(int bucket, long key) {
return table[bucket] == null ? "There is no value at key '" + key + '\'' : table[bucket].getValue();
}
}
我认为,java.lang.HashMap
也使用它,最好将HashEntry
移到HashMap
类:
public class HashMap {
private static final int TABLE_SIZE = 1900000;
private final Entry[] table;
public HashMap() {
// all elements are null by default
table = new Entry[TABLE_SIZE];
}
//...
private static final class Entry {
private long key;
private String value;
}
}
其次,最好使用There is no value at key
或返回null
抛出异常。如果你有map.put(666, "There is no value at key")
怎么办?当你get
这个密钥时,你怎么知道这个密钥是否存在?!