0

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 UUIDs 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()));
                        }

                    }
                }

         }
 });
Zain
  • 37,492
  • 7
  • 60
  • 84
KCYrgn
  • 251
  • 1
  • 12
  • 1
    You should provide more of your code as the for-loop in the above case would only print out only once Key/Value and not twice. There is no update to see. – jmizv Nov 01 '21 at 12:32
  • Thank you, I've updated the post. The `data.getJobs().get(i).getUuid()` will return a list of `uuid's` which when I try adding them to the `treemap` it will add them inside 1 key set, I am not sure if it is updating or not but all I wanted to know is if there is a way to add each value in a different key? – KCYrgn Nov 01 '21 at 12:45
  • 2
    Er, why are you trying to duplicate keys? TreeMap doesn't support duplicate keys; call put twice and you'll overwrite the previous value. Either use a different key or a different data structure altogether. – Roddy of the Frozen Peas Nov 01 '21 at 13:55
  • Always search Stack Overflow before posting. While the Question is not clear, it seems you want multiple values per key. This has been [covered many times](https://duckduckgo.com/?q=java+map+with+mulitple+values+site%3Astackoverflow.com&t=osx&ia=web) such as [*HashMap with multiple values under the same key*](https://stackoverflow.com/q/4956844/642706) and [*How to have a key with multiple values in a map?*](https://stackoverflow.com/q/13720225/642706). – Basil Bourque Nov 06 '21 at 22:15
  • Multiple values per key is known as a *multimap*. You can get explicit classes for this in libraries such as Google Guava or Eclipse Collections. And with modern Java syntax, you can do this without additional libraries. – Basil Bourque Nov 06 '21 at 22:15

0 Answers0