I wrote the following piece of code and want to hear your opinion, in this snippet I have a vector called aggregateFeaturesForML
which has elements of Class with 3 fields: sourceip, key, value. what I want is to collect all the key-value pairs that have the same sourceip and form a nice histogram (for each IP the keys are unique), for that purpose I collect the key-value pairs in a map called histogram and then use its toString function to print a {key=value, key=value}
formation. I store the histograms inside another map called aggregator
.
Any comments on the readability, writing manner, other stuff will be appritiated.
Definition:
private HashMap<String, HashMap<String, String> > aggregator;
protected Vector<SingleResult> aggregateFeaturesForML = new Vector<SingleResult>(); //Single result has 3 fields: IP, key, value
Code:
String previousIp = aggregateFeaturesForML.get(0).getSourceip();
if(!aggregator.containsKey(previousIp))
{
aggregator.put(previousIp, new HashMap<String, String>());
}
HashMap<String, String> histogram = new HashMap<String, String>();
for(int iterator=0;iterator<aggregateFeaturesForML.size();iterator++)
{
SingleResult sr = aggregateFeaturesForML.get(iterator);
String ip = sr.getSourceip();
if(ip != previousIp)
{
HashMap<String, String> mapForIP = aggregator.get(previousIp);
mapForIP.put(key, histogram.toString());
aggregator.put(previousIp, mapForIP);
if(!aggregator.containsKey(ip))
{
aggregator.put(ip, new HashMap<String, String>());
}
histogram.clear();
previousIp = ip;
}
histogram.put(sr.getKey(), sr.getValue());
}
HashMap<String, String> mapForIP = aggregator.get(previousIp);
mapForIP.put(key, histogram.toString());
aggregator.put(previousIp, mapForIP);