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

时间:2016-05-14 21:31:21

标签: java treemap

这是一个类,我想将它放在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库从Spreadsheet中获取值。 TreeMap使用整数作为上面类的Key和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());
        }

在Map中设置所有值时。我迭代它。我得到了正确的密钥,但是所有相应的值(它是JobDefinition类的一个对象)都是最后一个放置的值。

输出:

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

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

1 个答案:

答案 0 :(得分:0)

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

您需要更改为:

private String jobDescription;
private String datasetName;
private String jobName;
private String responsiblePerson;
相关问题