Reputation: 2390
I working on a project where I have to connect the specific database based on the bank_name.I am using java,struts2,hibernate. for example:
{'sbi':
{'host':'111.111.15.45','port':3306,'username':'xxxx','password':'xxxx','database':'sbidb'}
Here sbi is a bank_name which is a key,then as a value there is another map. so I want to retrieve all values such as host,port,db based on bank_name(ex: 'sbi'). How to achieve this?
Upvotes: 1
Views: 3637
Reputation: 3794
You can use following data structure to achieve this
Map<String, Map<String,String> bankdetails = new HashMap<String, Map<String,String> ();
to put details of sbi,
first populate sbi specific details
HashMap<String, String> details = new HashMap<String,String>();
details.put("host","XXXX);
details.put("port", "101111");
......
then use to put details of sbi as below
bankdetails.put(sbi ,details);
Upvotes: 0
Reputation: 1184
You should have a Map<String,Map<String,String>>
. To get the keys for the interior map use the keySet()
function of the map. Ex:
Map<String,Map<String,String>> X;
Set<String> sbikeys = X.get('sbi').keySet();
Upvotes: 0
Reputation: 34367
This is simple.
You Map is something like this:
Map<String, Map<String, String>> map = new HashMap<String, Map<String, String>>();
.....
Now you can retrieve the key value pair from inner map as below:
Map<String,String> sbiMap = map.get("sbi");
Set<String> keys = sbiMap.keySet();
for(String key: keys){
System.out.println("key="+key+" value = " + sbiMap.get(key));
}
This will print all inner keys and corresponding values in the map for sbi
.
Upvotes: 1