I am using Map as follows
Map<String, String> propMap = new LinkedHashMap<String, String>();
and I saw that there is two methods that I can use keySet() (to get the list of keys)
and values to get the list of values but my question is how to relate between them
for example for key1 the value is 2.
I thought to use get value like follows
Map<String, String> propMap2 = propterm.getPropMap();
Set<String> keySet = propMap2.keySet();
But how I relate it to his respective value ?
You can use
propMap.entrySet()method which returns aMap.Entryofkey, value, if you want to use every pair ofkeyandvalue: –Or, if you want to know how to do this with
propMap.keySet(), you can iterate over theSet<Key>you obtain, and for each key, usepropMap.get(key), to get the value of a particular key: –From an answer from this post: –
With the later approach, if you are regularly accessing the
key-valuepair, then for each key, themap.get()method is called, which – in the case of aHashMap– requires that thehashCode()andequals()methods of thekeyobject be evaluated in order to find the associated value*. In the first case(entrySet), that extra work is eliminated.