Can I use any other approach to read the semicolon separated string from .txt file into Has map instead of *sourceArra*y
public static void main(String[] args) throws IOException {
try {
ArrayList<Synset> booleansynsets = null;
ArrayList<Synset> booleanduplicatesynsets = null;
Map<String, String> basebooleanentitieslist = new HashMap<String, String>();
BufferedReader bufferedReader = new BufferedReader(new FileReader("C:\\Users\\anand\\Desktop\\updatedDuplicateBooleanEntitiesList-sorted.txt"));
String line = "";
while ((line = bufferedReader.readLine()) != null) {
String[] sourceArray = line.split(";");
basebooleanentitieslist.put(sourceArray[0],sourceArray[1]);
System.out.println(line);
}
// the updated one
bufferedReader.toString();
StringTokenizer st1 = new StringTokenizer(bufferedReader.toString(),";");
while ((line = bufferedReader.readLine()) != null && st1.hasMoreTokens()) {
// String[] sourceArray = line.split(";");
basebooleanentitieslist.put(st1.nextToken(";"), st1.nextToken());
System.out.println(line);
}
You don’t say what you are trying to optimize for: performance? memory usage? readability?
If you are concerned about performance, the next question is whether your concern is actually justified. Have you run your application? Is it too slow? Have you profiled it and determined that splitting the lines is taking a significant amount of time?
What specifically is wrong with using an array? (Yes, I know that allocating an array costs something, but have you any evidence that this is significant?)
If you are trying to optimize for readability, then I’d say that using String.split is probably more readable for this example. (Many Java programmers have never come across / used the
StringTokenizerclass.)If you are trying to optimize for performance / memory usage, then
StringTokenizeris worth trying, but I wouldn’t guarantee it is faster. Another alternative is to usePatternandMatcherdirectly as follows:(By the way, the code about will handle the case where the line doesn’t split gracefully; i.e. without throwing an exception. If you want to deal with it explicitly, add an
elseclause.)