patternjavaMinor
Storing variables to a map by splitting a text file
Viewed 0 times
mapfiletextsplittingvariablesstoring
Problem
I'm currently making a social media app that contains profile details such as interests in a text file. The following code works by using markers in the text file to upload these details to map data structures. I'm wondering if there is a more efficient way of doing this.
I've created a split function that looks for marker to split it on
textList = ReadFile(FileName);
System.out.println("File Length" + textList.size());
for (int i = 0; i friends = new ArrayList<>();
List interests = new ArrayList<>();
if (split.length >= 2){
String[] interestArray = splitTxt(split[2], ";");
for(int j = 0; j < interestArray.length; j++){
interests.add(interestArray[j]);
}
Accounts.put(split[0],split[1]);
intrestDic.put(split[0],interests);
for(int j= 3; j < split.length; j++){
friends.add(split[j]);
}
FriendsDic.put(split[0],friends);
System.out.println(FriendsDic.get(split[0]));
}
}I've created a split function that looks for marker to split it on
public static String[] splitTxt(String text, String type){
String[] parsedText = text.split(type);
return parsedText;
}Solution
Range-based
You can simplify this with a range-based
No need to manage
I also eliminated the call to
Bug
This will cause an array index out of bounds when the
When
Switching to an early exit changes from
There is no point to declaring and initializing
You don't need to manually copy one by one.
Using a copy constructor with
Summary
and later
This isn't necessarily more efficient, although it could be (depending on how Java optimizes it). The primary advantage of this version is that it leaves maintenance to the Java engine.
Hibernate
Hibernate (and Java Persistence Annotations in general) is for exactly this task. It maps classes to storage and vice versa. Now, that might be a heavyweight solution, but it is one that exactly addresses the problem.
You can write persistence code manually, without annotations. But I would generally still use a database if you intend the code to scale at all. You don't want to read all the records every time you want one record. Rather than reinventing that wheel, just use the existing solution: a database.
Hibernate makes it comparatively easy to switch from one database to another.
for loopfor (int i = 0; i < textList.size(); i++){
String[] split = splitTxt(textList.get(i), "\\s+");You can simplify this with a range-based
for loop. for (String text : textList) {
String[] split = text.split("\\s+");No need to manage
i manually. I also eliminated the call to
splitTxt, which makes the code smaller and easier to read. Bug
List friends = new ArrayList<>();
List interests = new ArrayList<>();
if (split.length >= 2){
String[] interestArray = splitTxt(split[2], ";");This will cause an array index out of bounds when the
split.length is 2. if (split.length friends = new ArrayList<>();
List interests = new ArrayList<>();When
split is of length 2, only 0 and 1 are valid indexes. So split[2] would be out of bounds. Making it a 3 instead of a 2 would fix that. Switching to an early exit changes from
>= to < and allows us to reduce the level of indent. It works here because the entire code for an iteration after that point is inside the if. There is no point to declaring and initializing
friends and interests if we're not going to use them. So do those tasks after the check, not prior. asListString[] interestArray = splitTxt(split[2], ";");
for(int j = 0; j < interestArray.length; j++){
interests.add(interestArray[j]);
}
Accounts.put(split[0],split[1]);
intrestDic.put(split[0],interests);You don't need to manually copy one by one.
Accounts.put(split[0],split[1]);
intrestDic.put(split[0], new ArrayList(Arrays.asList(split[2].split(";"))));Using a copy constructor with
Arrays.asList will allow the whole thing to be copied at once. Summary
private static final int HEADER_SIZE = 3;and later
for (String text : ReadFile(FileName)) {
String[] split = text.split("\\s+");
if (split.length (Arrays.asList(split[2].split(";"))));
List friends = Arrays.asList(split).subList(HEADER_SIZE, split.length));
FriendsDic.put(split[0], new ArrayList(friends);
}This isn't necessarily more efficient, although it could be (depending on how Java optimizes it). The primary advantage of this version is that it leaves maintenance to the Java engine.
Hibernate
Hibernate (and Java Persistence Annotations in general) is for exactly this task. It maps classes to storage and vice versa. Now, that might be a heavyweight solution, but it is one that exactly addresses the problem.
You can write persistence code manually, without annotations. But I would generally still use a database if you intend the code to scale at all. You don't want to read all the records every time you want one record. Rather than reinventing that wheel, just use the existing solution: a database.
Hibernate makes it comparatively easy to switch from one database to another.
Code Snippets
for (int i = 0; i < textList.size(); i++){
String[] split = splitTxt(textList.get(i), "\\s+");for (String text : textList) {
String[] split = text.split("\\s+");List<String> friends = new ArrayList<>();
List<String> interests = new ArrayList<>();
if (split.length >= 2){
String[] interestArray = splitTxt(split[2], ";");if (split.length < 3) {
continue;
}
String[] interestArray = splitTxt(split[2], ";");
List<String> friends = new ArrayList<>();
List<String> interests = new ArrayList<>();String[] interestArray = splitTxt(split[2], ";");
for(int j = 0; j < interestArray.length; j++){
interests.add(interestArray[j]);
}
Accounts.put(split[0],split[1]);
intrestDic.put(split[0],interests);Context
StackExchange Code Review Q#161895, answer score: 4
Revisions (0)
No revisions yet.