[英]How to display Generic HashMap entries
I am trying to understand how Generics work and I have this piece of code to display the entries of newDetails a class with these kind of parameters Map. 我试图了解泛型的工作方式,并且我有这段代码来显示newDetails条目的类,其中包含此类参数Map。 This is my code.
这是我的代码。
public class Map<Key,Value> {
private Map<Key, Value> entry;
public Map(){
entry = new HashMap<Key,Value>();
}
public void putEntry(Key k, Value id){
entry.put(k, id);
}
public void printMe(){
System.out.println();
for (Map.Entry<Key, Value> x: entry.entrySet()){
System.out.print(x+" ");
}
}
}
Chocolates Class 巧克力课
public class Chocolates<T> {
private List<T> listOfChocolate = new ArrayList<T>();
public Chocolates(T choc){
listOfChocolate.add(choc);
}
public void addChocolate(T getChoc){
listOfChocolate.add(getChoc);
}
public List<T> getLists(){
return listOfChocolate;
}
}
Main method 主要方法
public static void main(String[] arg){
Map<Chocolates<String>, Integer> newDetails = new Map<Chocolates<String>, Integer>();
Chocolates<String> choco = new Chocolates<String>();
newDetails.putEntry((new Chocolates<String>("Cadbury")), 1);
newDetails.putEntry((new Chocolates<String>("Hersheys")), 2);
newDetails.printMe();
}
Whenever I try to ran the program it just display this one 每当我尝试运行该程序时,它只会显示该程序
generics.Chocolates@9931f5=1 generics.Chocolates@19ee1ac=2
How do I display the actual entries like Cadbury 1 then Hersheys 2? 如何显示像吉百利1和Hersheys 2这样的实际条目?
Also is there any way I can improve the main method or some parts of the program because I always create a new object whenever I add a new entry. 还有什么方法可以改善主要方法或程序的某些部分,因为每当添加新条目时,我总是创建一个新对象。 Originally what I wanted to do is create a Generics version of this code:
最初,我想做的是创建此代码的泛型版本:
Map<List<String>, Integer> newDetails = new HashMap<List<String>, Integer>();
As someone told me that its alot better to implement that line of code in a Generics Class. 正如有人告诉我的那样,最好在Generics类中实现该行代码。 Any suggestions?
有什么建议么? Thanks
谢谢
I edited Maps Class printMe Method as Nambari have requested 我按照Nambari的要求编辑了Maps类的printMe方法
public void printMe(){
System.out.println();
for (Map.Entry<Key, Value> x: entry.entrySet()){
System.out.print(x.getKey()+" "+x.getValue());
}
}
The output is: 输出为:
generics.Chocolates@9931f5 1generics.Chocolates@19ee1ac 2 generics.Chocolates@9931f5 1generics.Chocolates@19ee1ac 2
But if I add this line as suggested by Joao 但是如果我按照Joao的建议添加这一行
public String toString(){
return listOfChocolate.toString();
}
output is : 输出是:
[Hersheys] 2[Cadbury] 1
Thanks everyone for your help! 谢谢大家的帮助!
Override Object#toString
in the Chocolate
class: 在
Chocolate
类中重写Object#toString
:
public class Chocolates<T> {
// ...
@Override
public String toString() {
return listOfChocolate.toString();
}
}
Update printMe(..)
method System.out
更新
printMe(..)
方法System.out
System.out.print(x.getKey() + " " + x.getValue());
EDIT: 编辑:
public void printMe(){
System.out.println();
for (java.util.Map.Entry<Key, Value> x: entry.entrySet()){
System.out.print(((Chocolates)x.getKey()).getLists().get(0) +" "+x.getValue());
}
}
NOTE: This code may throw NPE
if getLists()
is null 注意:如果
getLists()
为null,则此代码可能会引发NPE
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.