I have a csv file with 42,000 lines in the following template
03055,Milford,NH
03057,Mont Vernon,NH
03060,Nashua,NH
I tried to save the data in HashMapusing zipcode as a key, e.g.
while ((line = stream_in.readLine())!=null) {
LocationBean temp_location_bean = new LocationBean();
String line_trimmed = line.trim();
String[] line_chunked = line_trimmed.split(",",4);
temp_location_bean.setZip_code(line_chunked[0]);
temp_location_bean.setCity(line_chunked[1]);
temp_location_bean.setState(line_chunked[2]);
this.locations_as_beans_list.put(zip_code, temp_location_bean);
}
But when I go to search:
for(Map.Entry<String, LocationBean> location_object : this.locations_as_beans_list.entrySet())
{
LocationBean temp_location_bean = location_object.getValue();
if (params[0].matches(temp_location_bean.getZip_code())) {
master_location = temp_location_bean.getCity() + ","
+ temp_location_bean.getState()
+ ", (" + temp_location_bean.getZip_code() +")";
}
}
It takes more than 20 seconds ... Should performance not be relatively fast? How can I improve performance here?
TL; DR
how can I optimize reading in this example?
Petro source
share