I'm working on a little project that reads party (aka client) data from a spreadsheet into two hashmaps. One keeps track of each party with the value being the object Party, the other is embedded in the Party Object that keeps track of each party's data. Thing is, the way I'm doing it is with two for-loops, which as we all know is a O(N^2) algorithm. The way it is now is about 500 rows (or 500 parties) with about 65 columns (or 65 labels/values) so at that number of elements it's not really a big deal. However, I've been told it might have to handle over 25 million rows, in which case O(N^2) is a problem (not technically O(N^2) with the columns I guess, but the number of columns could be expanded it's not necessarily set at 65).
Long story short, I need tips on how to reduce the run-time but I can't really think of any other way to access every cell in the sheet.
Here's the relevant code:
package storage;
import java.io.File;
import java.util.HashMap;
import jxl.Sheet;
import jxl.Workbook;
import pojo.Party;
public class PartyStructure {
private static HashMap<String, Party> map;
private static PartyStructure partyStructure;
private String inputFile = "C:/Users/joayers/Documents/API Project Information/Sample Data.xls";
File excelData = new File(inputFile);
private PartyStructure() throws Exception
{
map = new HashMap<String, Party>();
readData();
}
public static HashMap<String,Party> getPartyCollection() throws Exception
{
if(partyStructure==null)
{
partyStructure = new PartyStructure();
}
return map;
}
private void readData() throws Exception
{
Workbook w=Workbook.getWorkbook(excelData);
Sheet sheet = w.getSheet(0);
String party_name;
String labelName;
String dataField;
for(int i=1;i<sheet.getRows();i++)
{
party_name = sheet.getCell(2, i).getContents().toString();
//map is a Hashmap<String, Party>
map.put(party_name, new Party());
for(int j=0;j<sheet.getColumns();j++)
{
labelName = sheet.getCell(j, 0).getContents().toString();
dataField = sheet.getCell(j, i).getContents().toString();
Party party = map.get(party_name);
//getPartyInfo is a getter for a HashMap<String, String> that holds values associated with the keys (the labels in excel)
party.getPartyInfo().put(labelName, dataField);
}
}
}
}
Also, is there any difference between a hashmap and a hashtable? They seem like the same thing