如何使用42,000个密钥优化此HashMap

Pet*_*tro 4 java arrays hashmap

我有一个csv文件,其中42,000行采用以下模式

03055,Milford,NH
03057,Mont Vernon,NH
03060,Nashua,NH
Run Code Online (Sandbox Code Playgroud)

我试图将数据存储在一个HashMap使用zipcode作为键,如

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);
}
Run Code Online (Sandbox Code Playgroud)

但是当我去查找时:

 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() +")";
     }
 }
Run Code Online (Sandbox Code Playgroud)

它需要超过20秒....不应该表现相对较快?我怎样才能提高性能?

tl; dr 如何优化本例中的读取?

Ell*_*sch 5

如果您正在寻找性能,那么您不应该迭代entrySet查找带键的zipcode.相反,您可以使用HashMap并通过其键获取值.喜欢,

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() +")";
}
Run Code Online (Sandbox Code Playgroud)