java returns 中的 TreeMap 实现仅最后一个元素

TreeMap implementation in java returns only last element

这是一个 class 我想将其对象放入 TreeMap 中的对象。

public class JobDefinition  {
    private static String jobDescription;
    private static String datasetName;
    private static String jobName;
    private static String responsiblePerson;
    public JobDefinition(String jobDesc, String dataSet, String jobName2, String person) {
        jobDescription=jobDesc;
        datasetName=dataSet;
        jobName=jobName2;
        responsiblePerson=person;
    }
    public  String getJobDescription() {
        return jobDescription;
    }

    public  String getDatasetName() {
        return datasetName;
    }

    public  String getJobName() {
        return jobName;
    }

    public  String getResponsiblePerson() {
        return responsiblePerson;
    }

}

这里我使用 POI 库从电子表格中获取值。 TreeMap 使用一个整数作为 Key 和上面 class 的 Object 作为它的值。

for (int rowCount = rowStartIndex+1; rowCount < rowEndIndex; rowCount++) 
    {
        String jobDesc=spreadsheet.getRow(rowCount).getCell(0).toString();
        String dataSet=spreadsheet.getRow(rowCount).getCell(1).toString();
        String jobName=spreadsheet.getRow(rowCount).getCell(2).toString();
        String person =spreadsheet.getRow(rowCount).getCell(3).toString();
        if(!jobName.equals("N/A") && jobName!=""){
            validJobCount++;
            jobDefinitionInfo.put(validJobCount, new JobDefinition(jobDesc,dataSet,jobName,person));
            }
    }
    for(Map.Entry<Integer,JobDefinition> entry : jobDefinitionInfo.entrySet()) {
          System.out.println(entry.getKey()+"::"+entry.getValue().getJobDescription());
        }

当在地图中设置所有值时。我遍历它。我得到了正确的键,但所有对应的值(它是 JobDefinition class 的对象)是最后放置的值。

输出::

1::Monthly UPDTMEND File
2::Monthly UPDTMEND File
3::Monthly UPDTMEND File
4::Monthly UPDTMEND File
5::Monthly UPDTMEND File
6::Monthly UPDTMEND File
7::Monthly UPDTMEND File
8::Monthly UPDTMEND File
9::Monthly UPDTMEND File
10::Monthly UPDTMEND File

预期输出

1::VRSFEND - TRANSACTION SWEEP
2::XCTLOAD 
3::CHEKDATE  - TO IDENTIFY BACKDATED TRANSACTIONS
4::EDITALIVE  
5::EDITB 
6::PRICE LOAD
7::ACCTSIM - run manually 
8::ACCTLIV - run manually by DVG                        
9::Check Sybase jobs
10::Monthly UPDTMEND File

我觉得实现有问题。请告诉我还应该添加什么才能使它 运行 正确。

您在参数中使用了 static。这意味着每次你 运行 它们都会被覆盖,这完美地解释了为什么你总是得到最后一个元素。

你需要改成这样:

private String jobDescription;
private String datasetName;
private String jobName;
private String responsiblePerson;