I am using HashMap with integer keys,
HashMap<Integer, Double> feature_vector = new HashMap<Integer, Double>();
and while reading data, add a new member when the value is not 0,
if (value != 0)
feature_vector.add(index, value);
For anything that deals with this feature_vector, how can I just iterate over the "non-empty" positions of this feature_vector? I don't want to increment the key from 0 to the size of the feature_vector and check the value of every single key, which slows the program down.
Meaning there will be cases when the feature vector looks like: 1->7, 3->0, 4->5, 5->9, ... (the second key has no value, so there would not be a second key and its value)
Thanks in advance for your time and help!
HashMap.entrySet()method (which is iterable) to explore the contents (both keys and values together), or useHashMap.keySet()for just the keys (indices).TreeMapinstead of a 'HashMap', the keys / entries will be sorted by Integer value.