java - Alternative for String[]? -


i have requirement need read tabbed separated data file , put in map particular keys. right now, doing split data, create string[] , put hashmap. sample code on how doing provided below:

while ((line = bufferedreader.readline()) != null) {     string[] data = line.split("\t");     myhashmap = new hashmap<string, string>();     (int = 0; < data.length; i++) {            myhashmap.put("<key>"+i,data[i]);     }     <additional logic> } 

the problem have read 2500 files each having around 1000 lines. result high memory usage , performance issue. far know string costlier regards memory. looking better approach tackle requirement.

thanks help.

do need store information map?

if answer no can store in list.

while ((line = bufferedreader.readline()) != null) {     string[] data = line.split("\t");     mylist = new arraylist<string>(data.length);     (int = 0; < data.length; i++) {            mylist.add(data[i]);     }     // elements, instead of using:     myhashmap.get("<key>" + i);     // use:     mylist.get(i);     //<additional logic> } 

i'm considering close file/buffer.

while ((line = bufferedreader.readline()) != null) {     list<string> mylist = arrays.aslist(line.split("\t"));     // elements, instead of using:     myhashmap.get("<key>" + i);     // use:     mylist.get(i);     //<additional logic> } 

unless you're using different key hash map improves access performance should use index of array.

while ((line = bufferedreader.readline()) != null) {     string[] data = line.split("\t");     // elements, instead of using:     myhashmap.get("<key>" + i);     // use:     data[i];     //<additional logic> } 

Comments