java - Time Complexity of keySet() for encapsulated Map<> values -


i need hashmap signature

hashmap<integer, double> map; 

but encapsulated 2 values like:

class customkv {     integer key;     double value; } 

hence, map<> becomes

set<customkv> custommap; 

if use map, when wanted iterate on keys, map.keyset() sufficient. however, if use second, have iterate on objects of customkv , add keys, add them in set, return it.

my question is: java have optimization this? or each time call keyset(), going iterate on objects?

edit:

here, write difference of 2 iterations:

for(integer : map.keyset())  for(customkv kv : custommap()) 

it seems identical. ask is, if keep calling

for(customkv kv : custommap)     int key = kv.getkey(); 

would slow me down, or java recognize , treat custommap map?

if you're asking java's implementation, hashmap cache set after first call keyset(). first call doesn't require iteration, because returns view, or wrapper, of underlying entry set.

so, yes, java optimize implementation, technically apply same optimizations custom set.


Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -