用java打印hashmap?
因此,我有一个哈希映射类,它工作正常,但我希望它像这样打印出内容:用java打印hashmap?,java,hash,hashmap,Java,Hash,Hashmap,因此,我有一个哈希映射类,它工作正常,但我希望它像这样打印出内容: 1 -> 101 -> 201 (this is a bucket for handling collision) 2 3 - > 103 - > 203 4 5 换句话说,我只想知道如何让我的程序打印出哈希表的内容,使它看起来像那样。如有任何意见或建议,将不胜感激。我不熟悉散列映射,所以这很让人困惑 我不知道该怎么做 这是我的哈希映射类,如果有帮助: public class HashMap<K
1 -> 101 -> 201 (this is a bucket for handling collision) 2
3 - > 103 - > 203
4
5
换句话说,我只想知道如何让我的程序打印出哈希表的内容,使它看起来像那样。如有任何意见或建议,将不胜感激。我不熟悉散列映射,所以这很让人困惑
我不知道该怎么做
这是我的哈希映射类,如果有帮助:
public class HashMap<K, V> {
private int DEFAULT_CAPACITY = 10;
private MapEntry<K, V>[] Hash;
private int size;
public HashMap() {
Hash = new MapEntry[DEFAULT_CAPACITY];
}
public int getHashCode(K key) {
int bucketIndex = key.hashCode() % Hash.length;
return bucketIndex;
}
public V get(K key) {
if (key == null) {
throw new IllegalArgumentException("Null Key!");
}
MapEntry<K, V> entry = Hash[getHashCode(key)];
while (entry != null && !key.equals(entry.getKey()))
entry = entry.getNext();
if (entry != null)
return entry.getValue();
else
return null;
}
/**
*
* @param key
* @param value
* The put method works by associating the specified value with
* the given key in the map.
* If the key is already in the map,
* the old value is replaced with the new one.
*/
public void put(K key, V value) {
int keyBucket = hash(key);
MapEntry<K, V> temp = Hash[keyBucket];
while (temp != null) {
if ((temp.key == null && key == null)
|| (temp.key != null && temp.key.equals(key))) {
temp.value = value;
return;
}
temp = temp.next;
}
Hash[keyBucket] = new MapEntry<K, V>(key, value);
size++;
}
/**
*
* @param key
* @param value
* The delete method works similarly to the put method.
* It locates the desired value in the hash, e,
* and then it removes e from the bucket, like removing a node
* from a linked list.
* Then it sets the value of e to its next node.
* And then it decreases the size of the map.
*/
public void delete(K key, V value) {
if (key == null) {
throw new IllegalArgumentException("Null Key!");
}
int keyBucket = hash(key);
MapEntry<K, V> e = Hash[keyBucket];
while (e != null) {
if ((e.key == null && key == null)
|| (e.key != null && e.key.equals(key))) {
e.value = value;
return;
}
e = e.next;
}
Hash[keyBucket] = new MapEntry<K, V>(key, value);
size--;
}
public void print(){
//THIS IS WHERE I NEED HELP
}
private int hash(K key) {
if (key == null) {
return 0;
} else {
return Math.abs(key.hashCode() % this.Hash.length);
}
} }
公共类HashMap{
专用int默认_容量=10;
私有映射条目[]散列;
私有整数大小;
公共HashMap(){
哈希=新映射项[默认容量];
}
public int getHashCode(K键){
int bucketinex=key.hashCode()%Hash.length;
返回Buckettendex;
}
公共V获取(K密钥){
如果(key==null){
抛出新的IllegalArgumentException(“空键!”);
}
MapEntry=Hash[getHashCode(key)];
while(entry!=null&&!key.equals(entry.getKey()))
entry=entry.getNext();
if(条目!=null)
返回条目.getValue();
其他的
返回null;
}
/**
*
*@param-key
*@param值
*put方法通过将指定的值与
*地图中给定的键。
*如果钥匙已经在地图上,
*旧值将替换为新值。
*/
公开作废认沽权(K键,V值){
int keyback=hash(key);
MapEntry temp=散列[keyBucket];
while(temp!=null){
if((temp.key==null&&key==null)
||(temp.key!=null&&temp.key.equals(key))){
温度值=温度值;
返回;
}
温度=下一个温度;
}
Hash[keyBucket]=新的映射条目(键,值);
大小++;
}
/**
*
*@param-key
*@param值
*delete方法的工作原理与put方法类似。
*它在散列中定位所需的值,e,
*然后它从bucket中删除e,就像删除节点一样
*从链接列表中。
*然后将e的值设置为下一个节点。
*然后它会减小地图的大小。
*/
公共无效删除(K键,V值){
如果(key==null){
抛出新的IllegalArgumentException(“空键!”);
}
int keyback=hash(key);
MapEntry e=散列[keyBucket];
while(e!=null){
if((e.key==null&&key==null)
||(e.key!=null&&e.key.equals(key))){
e、 价值=价值;
返回;
}
e=e.next;
}
Hash[keyBucket]=新的映射条目(键,值);
大小--;
}
公开作废印刷品(){
//这就是我需要帮助的地方
}
私有整数散列(K密钥){
if(key==null){
返回0;
}否则{
返回Math.abs(key.hashCode()%this.Hash.length);
}
} }
首先,您应该覆盖类的字符串returning.toString()方法,而不是使用名为print()的新void方法
关于你的问题,我不完全理解你想要印刷什么。您是只想为哈希映射中的每个条目打印“key”->“value”,还是想实现不同的结果?您需要遍历作为存储桶维护的数组,并将条目的内容附加到每个位置。 这应该起作用:
public void print() {
final StringBuilder sb = new StringBuilder();
for (int i = 0; i < Hash.length; i++) {
sb.append(i).append(" : ");
MapEntry<K, V> temp = Hash[i];
while (temp != null) {
sb.append(temp.key).append(" -> ").append(temp.value).append(" , ");
temp = temp.next;
}
sb.append("\n");
}
System.out.println(sb.toString());
}
您的put()
和delete()
方法中有一个bug。这将修复put()方法:
public void put(K键,V值){
int keyback=hash(key);
MapEntry temp=散列[keyBucket];
while(temp!=null){
if((temp.key==null&&key==null)
||(temp.key!=null&&temp.key.equals(key))){
温度值=温度值;
返回;
}
温度=下一个温度;
}
最终映射条目newEntry=新映射条目(键,值);
newEntry.next=散列[keyBucket];//与当前条目的链
Hash[keyback]=newEntry;
大小++;
}
打印“key”->“value”正是我想要做的,但是我如何为每个key做这件事>如果你只想让输出是“key”->“value”,问题归结为“如何迭代hashmap”,或者,你可以在循环时将System.out.print()和System.out.println()结合起来。这有节省内存的优点。@Code Guru是的,它节省内存。不幸的是,这是以牺牲较慢的系统(IO)调用为代价的。这是规范的时间与空间权衡的又一个例子。
public void print() {
final StringBuilder sb = new StringBuilder();
for (int i = 0; i < Hash.length; i++) {
sb.append(i).append(" : ");
MapEntry<K, V> temp = Hash[i];
while (temp != null) {
sb.append(temp.key).append(" -> ").append(temp.value).append(" , ");
temp = temp.next;
}
sb.append("\n");
}
System.out.println(sb.toString());
}
0 : 180 -> 0.889234530714529 ,
1 :
2 : 992 -> 0.11655748287282786 ,
3 :
4 : 734 -> 0.8213900931007967 , 824 -> 0.8399483889863836 , 554 -> 0.7833733949735435 , 304 -> 0.9461472125123178 ,
5 : 865 -> 0.604963832544362 , 555 -> 0.1889914052365086 ,
6 : 536 -> 0.5835183387314298 ,
7 : 597 -> 0.3846960557011073 ,
8 :
9 :
public void put(K key, V value) {
int keyBucket = hash(key);
MapEntry<K, V> temp = Hash[keyBucket];
while (temp != null) {
if ((temp.key == null && key == null)
|| (temp.key != null && temp.key.equals(key))) {
temp.value = value;
return;
}
temp = temp.next;
}
final MapEntry<K, V> newEntry = new MapEntry<K, V>(key, value);
newEntry.next = Hash[keyBucket]; // chain with current entry
Hash[keyBucket] = newEntry;
size++;
}