This is a class whose object I want to put in a 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;
}
}
Here I am fetching values from Spreadsheet using POI library. TreeMap uses a integer as Key and Object of above class as its value.
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());
}
When all value is set in Map. And I iterate over it. I get correct key but all corresponding values (which is an object of JobDefinition class) against it is the last value which was placed.
Output::
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
Expected Output
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
I feel there is something wrong with Implementation. Please tell me what more should be added to make it run correctly.
You are using static
in your parameters. This means that everytime you run they get overriden which perfectly explains why you are always getting the last element.
You need to change to this:
private String jobDescription;
private String datasetName;
private String jobName;
private String responsiblePerson;