How can I optimize this HashMap with 42,000 keys

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?

+4
source share
5 answers

If you're looking for performance, you shouldn't have to iterate over entrySetto search for encrypted zip code. Instead, you can use HashMapand get the value by its key. How,

LocationBean temp_location_bean = this.locations_as_beans_list.get(params[0]);
if (temp_location_bean != null) {
    master_location = temp_location_bean.getCity() + "," 
            + temp_location_bean.getState() 
            + ", (" + temp_location_bean.getZip_code() +")";
}
+5
source

. , .

, , , .

1. - BufferedReader 6/7 seconds to parse 878 MB file. ?

. RandomAccessChannel API in java, 0,16/0,19 .

. Asynchronous File Reads .

2.

. API- Runtime , , , .

. .

, ,

+4

, , , . . HashMaps . , - key get HashMap ( , , , , get ).

, , Javolution. , EntrySet HashMap , . , ( ).

+4

, HashMap . , .

:

.

PLUS: MapReduce 40k , .

+1


-

A. - Sharding - , ( MapReduce

B. - ? .
- , - ?

C. EntrySet, getKey()? ( )

0

All Articles