0

I have a list of tag_ids configured with the test_id_val in a table.I retrieve the Data using the following query

Select test_id_val , tag_id from p_details

For a straightforward case i would rather use a hash map to store key value pair and retrieve the data,But in my case the test_id_val can accept null values

For ex consider the following structure :

 1. APP TR202 
 2. APP TR204 
 3. APP TR205 
 4.NULL TR206 
 5.NULL TR207 
 6.NULL SM504 
 7.NULL SM505

So i will be having a list of tag_ids values to map to a test_id key which can either be null or contain values.My requirement is to map the test_id key to the list of tag_ids

Is there a specific way to do this?

Thanks for the help and time..

3 Answers 3

1

You might consider using some implementation of com.google.common.collect.Multimap.

You can put multiple values associated with one key and then retrieve them as a collection with the map.get(key) method. In your case:

Multimap<String, String> mmap = HashMultimap.create();

mmap.put("APP", "TR202");
mmap.put("APP", "TR204");
mmap.put("APP", "TR205");
mmap.put(null, "TR206");
mmap.put(null, "TR207");
mmap.put(null, "SM504");
mmap.put(null, "SM505");

for (String key : mmap.keySet()) {
  for (String value : mmap.get(key)) {
    System.out.println(key + ": " + value);
  }
}

Prints:

null: SM505
null: SM504
null: TR207
null: TR206
APP: TR202
APP: TR205
APP: TR204
Sign up to request clarification or add additional context in comments.

Comments

0

You can use ArrayList to store the test_id_val. Then in HashMap, store tag_id as a key and index of ArrayList to test_id_val as a value of HashMap. ArrayList allows a null value so you can easily store the null value of specific tag_id. Like this..

KEY     VALUE               ArrayList
TR202     1                 1-->  APP 
TR204     2                 2-->  APP 
TR205     3                 3-->  APP 
TR206     4                 4-->  NULL 
TR207     5                 5-->  NULL 

Comments

0

You might want to reconsider if this really is the best database schema design you have, but if you choose to stick with it, create a wrapper class for test_id_val in which you specify if you really mean null via a boolean:

private class TestIdVal {
    String test_id_val;
    boolean isNull;
}

You can then instantiate new TestIdVal() objects to use as keys for the map. I don't recommend using a list of tag_id as the key, as that doesn't seem to mirror your schema correctly. It seems that you're trying to do test_id_val => List of tag_id.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.