I need to add multiple values inside a TreeMap
from a database table but I am currently facing a duplication issue where the map is updating or replacing the values that I am adding every time the call is executed.
Note:
The below is going to be used to retrieve the UUID
values from the database which I need to add in a TreeMap
.
JobRepository jobRepository = new JobRepository(getApplication());
List<Job> jobs = jobRepository.getAllJobs();
I am currently trying to add the list of UUID
retrieved from the database in a TreeMap
:
TreeMap<Integer,String> validatingJobs=new TreeMap<Integer,String>();
if (data.getJobs() != null) {
for (int i=0; i<data.getJobs().size(); i++) {
jobRepository.insert(data.getJobs().get(i));
validatingJobs.put(1, String.valueOf(data.getJobs().get(i).getUuid()));
for (Map.Entry<Integer, String> entry : validatingJobs.entrySet()) {
Log.d("MAP KEY ", String.valueOf(entry.getKey()));
Log.d("MAP VALUE ", String.valueOf(entry.getValue()));
}
}
}
Note: data.getJobs().get(i).getUuid()
will return a list of UUID
s from the jobs
table.
The above will return the following:
D/MAP KEY: 1
D/MAP VALUE: 153352c8-298a-41e2-83a7-82abdb5651a9
D/MAP KEY: 1
D/MAP VALUE: 46ec925c-cc63-4234-b9bc-b01d42cfbf60
It seems like it is updating or replacing key 1. What I want to achieve though is the following:
D/MAP KEY: 1
D/MAP VALUE: 153352c8-298a-41e2-83a7-82abdb5651a9
D/MAP KEY: 2
D/MAP VALUE: 46ec925c-cc63-4234-b9bc-b01d42cfbf60
Update: I tried iterating but it didn't quite work, below is the full code of what I am currently doing.
ArrayList<String> listOfJobsValues = new ArrayList<String>();
TreeMap<Integer,String> validatingJobs=new TreeMap<Integer,String>();
Iterator<Map.Entry<Integer, String>> it = validatingJobs.entrySet().iterator();
callArray.enqueue(new Callback<SyncResponse>() {
@Override
public void onResponse(@NotNull Call<SyncResponse> call, @NotNull Response<SyncResponse> response) {
Log.d("Call request", bodyToString(call.request()));
Log.d("Response raw header", response.headers().toString());
Log.d("Response raw", String.valueOf(response.raw().body()));
Log.d("Response code", String.valueOf(response.code()));
SyncResponse data = response.body();
if (data == null) {
Log.d("NOTICE", "Empty response body");
return;
}
if (data.getJobs() != null) {
for (int i=0; i<data.getJobs().size(); i++) {
jobRepository.insert(data.getJobs().get(i));
while (it.hasNext())
{
listOfJobsValues = new ArrayList<String>();
for(int j = 0; j < data.getJobs().size(); j++)
{
listOfJobsValues.add(it.next().getValue());
}
validatingJobs.put(it.next().getKey(), String.valueOf(listOfJobsValues));
// validatingJobs.put(1, String.valueOf(data.getJobs().get(i).getUuid()));
for (Map.Entry<Integer, String> entry : validatingJobs.entrySet()) {
Log.d("MAP KEY ", String.valueOf(entry.getKey()));
Log.d("MAP VALUE ", String.valueOf(entry.getValue()));
}
}
}
}
});