自定义类做为hashmap键值
public class StructWord {
public String unlistedword;
public String notstopword;
StructWord(String word1,String word2){
unlistedword=word1;
notstopword=word2;
}
public int hascode(){
return unlistedword.hashCode()+notstopword.hashCode();
}
}
然后我想遍历并输出这个hashmap
HashMap<StructWord,Integer> temp=new HashMap<StructWord,Integer>();
Iterator ite = temp.entrySet().iterator();
temp.put(new StructWord("e","gh"), 1);
temp.put(new StructWord("c","cd"), 1);
temp.put(new StructWord("b","cd"), 1);
temp.put(new StructWord("d","of"), 1);
while(ite.hasNext()){
Map.Entry entry = (Map.Entry) ite.next();
Object key=entry.getKey();
System.out.println(key.tostring());
}
为什么报错了呢?返回的key的类型应该是StructWord,但是写StructWord就报错,而且最后也无法返回key.unlistedword,一写就报错。那我想要遍历输出StructWord结构中的unlistedword,要怎么写呢?
我自己已经解决,给后来人:
Iterator ite = temp.entrySet().iterator(); 在temp.put之后
key需要强制类型转换为StructWord 展开
最后的打印语句key.tostring()修改为key.toString()
前面的
Iterator ite = temp.entrySet().iterator();
temp.put(new StructWord("e","gh"), 1);
temp.put(new StructWord("c","cd"), 1);
temp.put(new StructWord("b","cd"), 1);
temp.put(new StructWord("d","of"), 1);
语句当中应该把
Iterator ite = temp.entrySet().iterator();
这句放到temp.put后面,调整为。
temp.put(new StructWord("e","gh"), 1);
temp.put(new StructWord("c","cd"), 1);
temp.put(new StructWord("b","cd"), 1);
temp.put(new StructWord("d","of"), 1);
Iterator ite = temp.entrySet().iterator();
修改后的代码如下:
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class StructWord {
public String unlistedword;
public String notstopword;
StructWord(String word1, String word2) {
unlistedword = word1;
notstopword = word2;
}
public int hascode() {
return unlistedword.hashCode() + notstopword.hashCode();
}
public static void main(String[] args) {
HashMap<StructWord,Integer> temp=new HashMap<StructWord,Integer>();
temp.put(new StructWord("e","gh"), 1);
temp.put(new StructWord("c","cd"), 1);
temp.put(new StructWord("b","cd"), 1);
temp.put(new StructWord("d","of"), 1);
Iterator<?> ite = temp.entrySet().iterator();
while(ite.hasNext()){
Map.Entry entry = (Map.Entry) ite.next();
Object key=entry.getKey();
System.out.println(key.toString());
}
}
}
但是这样的话,只能输出
StructWord@fe0ce1
StructWord@ed3bff
StructWord@1e534cb
StructWord@ac8360
是object key的地址吧?怎么才能把key中2个String:unlistedword和notstopword输出来?
你需要重写toString方法,否则将只能打印地址。
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class StructWord {
public String unlistedword;
public String notstopword;
StructWord(String word1, String word2) {
unlistedword = word1;
notstopword = word2;
}
public int hascode() {
return unlistedword.hashCode() + notstopword.hashCode();
}
public String toString() {
return "unlistedword: " + unlistedword + " notstopword:" + notstopword;
}
public static void main(String[] args) {
HashMap<StructWord,Integer> temp=new HashMap<StructWord,Integer>();
temp.put(new StructWord("e","gh"), 1);
temp.put(new StructWord("c","cd"), 1);
temp.put(new StructWord("b","cd"), 1);
temp.put(new StructWord("d","of"), 1);
Iterator<?> ite = temp.entrySet().iterator();
while(ite.hasNext()){
Map.Entry entry = (Map.Entry) ite.next();
Object key=entry.getKey();
System.out.println(key.toString());
}
}
}
2023-08-15 广告