2017-11-01 7 views
-1

クラスのオブジェクトのハッシュマップを作成したいと思います。私は文字列としてハッシュマップ持つキーを作成し、ハッシュマップに値を挿入しようとしていますクラスのオブジェクトのハッシュマップを作成する

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とハッシュマップの新機能です。私は何が間違っているのですか、なぜそのような奇妙な価値を得ていますか?

+0

'map.get(repoNameを)やってみてください。私のclass'の名前です –

+2

'いくつかの非常に奇妙な値 'と結果を確認 - あなたがしたい場合は、'あなたは 'のtoStringを(上書きする必要があります)奇妙な値を取得しないでください。 – Eran

+0

@エラン、ちょっと魅力的だったよ。どうもありがとうございます –

答えて

0

まずは、クラスののtoString()メソッドをオーバーライドする必要があります。オブジェクトインスタンスをオーバーライドしてオブジェクトインスタンスから呼び出した場合は、オブジェクトクラスの実装として応答します。ここにその基本的な実装を見つけることができますplease find toString() Method

下記のtoString()メソッドの共有コードブロックをコピーしてクラスに貼り付けてください。

はまた、私はマップするためにデータを入れている間、あなたがキーとしてrepoNameを定義しますが、データの取得中にあなたがキーとして展開サービスapi.gitを使用することに気づきました。

必ず同じキー値を使用してください。

@Override 
     public String toString() { 
      return "BitbucketRecordDataModel{" + 
        "softwareId='" + softwareId + '\'' + 
        ", scmUrl='" + scmUrl + '\'' + 
        ", aggregationDate='" + aggregationDate + '\'' + 
        '}'; 
     } 
関連する問題