java创建类对象的哈希映射
我想创建一个类对象的哈希映射
课程安排如下-
public class BitbucketRecordDataModel {
private String softwareId;
private String scmUrl;
private String aggregationDate;
public BitbucketRecordDataModel(String softwareId, String scmUrl, String aggregationDate) {
this.softwareId = softwareId;
this.scmUrl = scmUrl;
this.aggregationDate = aggregationDate;
}
public String getSoftwareId() {
return softwareId;
}
public void setSoftwareId(String softwareId) {
this.softwareId = softwareId;
}
public String getScmUrl() {
return scmUrl;
}
public void setScmUrl(String scmUrl) {
this.scmUrl = scmUrl;
}
public String getAggregationDate() {
return aggregationDate;
}
public void setAggregationDate(String aggregationDate) {
this.aggregationDate = aggregationDate;
}
}
我正在创建一个以键为字符串的哈希映射,并尝试将值插入哈希映射。但是当我试图检索哈希映射的细节时,我得到了null和一些非常奇怪的值,它们是我的类的名称
HashMap<String, BitbucketRecordDataModel> map = new HashMap<String, BitbucketRecordDataModel>();
cacheCondition = "hi";
username = "hi1";
protocol = "hi2";
BitbucketRecordDataModel bitbucketRecordDataModel = new BitbucketRecordDataModel(cacheCondition, username, protocol);
map.put(repoName, bitbucketRecordDataModel);
System.out.println(map.get("deployment-service-api.git"));
我对Java和哈希映射非常陌生。我做错了什么?为什么我会得到如此奇怪的价值观
# 1 楼答案
首先,您应该重写类的方法。如果不重写它并从任何对象实例调用,它将作为对象类的实现进行响应。您可以在这里找到它的基本实现please find toString() Method
请复制下面toString()方法的共享代码块,并将其粘贴到类中
我还注意到,在将数据放入映射时,您将repoName定义为键,但您使用的是部署服务api。检索数据时将git作为键
请确保使用相同的键值强>