Storing variables to a map by splitting a text file












2














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.



textList = ReadFile(FileName);
System.out.println("File Length" + textList.size());
for (int i = 0; i < textList.size(); i++){
String split = splitTxt(textList.get(i), "\s+");
List<String> friends = new ArrayList<>();
List<String> 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;
}









share|improve this question
















bumped to the homepage by Community 10 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.















  • Welcome to Code Review. To make this a good question, you should include a small excerpt from the text file.
    – 200_success
    Apr 29 '17 at 5:40
















2














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.



textList = ReadFile(FileName);
System.out.println("File Length" + textList.size());
for (int i = 0; i < textList.size(); i++){
String split = splitTxt(textList.get(i), "\s+");
List<String> friends = new ArrayList<>();
List<String> 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;
}









share|improve this question
















bumped to the homepage by Community 10 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.















  • Welcome to Code Review. To make this a good question, you should include a small excerpt from the text file.
    – 200_success
    Apr 29 '17 at 5:40














2












2








2







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.



textList = ReadFile(FileName);
System.out.println("File Length" + textList.size());
for (int i = 0; i < textList.size(); i++){
String split = splitTxt(textList.get(i), "\s+");
List<String> friends = new ArrayList<>();
List<String> 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;
}









share|improve this question















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.



textList = ReadFile(FileName);
System.out.println("File Length" + textList.size());
for (int i = 0; i < textList.size(); i++){
String split = splitTxt(textList.get(i), "\s+");
List<String> friends = new ArrayList<>();
List<String> 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;
}






java






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Apr 28 '17 at 18:01









t3chb0t

34.1k746114




34.1k746114










asked Apr 26 '17 at 23:01









p315

111




111





bumped to the homepage by Community 10 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.







bumped to the homepage by Community 10 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.














  • Welcome to Code Review. To make this a good question, you should include a small excerpt from the text file.
    – 200_success
    Apr 29 '17 at 5:40


















  • Welcome to Code Review. To make this a good question, you should include a small excerpt from the text file.
    – 200_success
    Apr 29 '17 at 5:40
















Welcome to Code Review. To make this a good question, you should include a small excerpt from the text file.
– 200_success
Apr 29 '17 at 5:40




Welcome to Code Review. To make this a good question, you should include a small excerpt from the text file.
– 200_success
Apr 29 '17 at 5:40










1 Answer
1






active

oldest

votes


















0














Range-based for loop




for (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<String> friends = new ArrayList<>();
List<String> 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 < 3) {
continue;
}

String interestArray = splitTxt(split[2], ";");
List<String> friends = new ArrayList<>();
List<String> 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.



asList




        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);



You don't need to manually copy one by one.



        Accounts.put(split[0],split[1]);
intrestDic.put(split[0], new ArrayList<String>(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 < HEADER_SIZE) {
continue;
}

Accounts.put(split[0], split[1]);
intrestDic.put(split[0], new ArrayList<String>(Arrays.asList(split[2].split(";"))));
List<String> friends = Arrays.asList(split).subList(HEADER_SIZE, split.length));
FriendsDic.put(split[0], new ArrayList<String>(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.






share|improve this answer





















    Your Answer





    StackExchange.ifUsing("editor", function () {
    return StackExchange.using("mathjaxEditing", function () {
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
    });
    });
    }, "mathjax-editing");

    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "196"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f161895%2fstoring-variables-to-a-map-by-splitting-a-text-file%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Range-based for loop




    for (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<String> friends = new ArrayList<>();
    List<String> 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 < 3) {
    continue;
    }

    String interestArray = splitTxt(split[2], ";");
    List<String> friends = new ArrayList<>();
    List<String> 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.



    asList




            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);



    You don't need to manually copy one by one.



            Accounts.put(split[0],split[1]);
    intrestDic.put(split[0], new ArrayList<String>(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 < HEADER_SIZE) {
    continue;
    }

    Accounts.put(split[0], split[1]);
    intrestDic.put(split[0], new ArrayList<String>(Arrays.asList(split[2].split(";"))));
    List<String> friends = Arrays.asList(split).subList(HEADER_SIZE, split.length));
    FriendsDic.put(split[0], new ArrayList<String>(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.






    share|improve this answer


























      0














      Range-based for loop




      for (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<String> friends = new ArrayList<>();
      List<String> 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 < 3) {
      continue;
      }

      String interestArray = splitTxt(split[2], ";");
      List<String> friends = new ArrayList<>();
      List<String> 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.



      asList




              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);



      You don't need to manually copy one by one.



              Accounts.put(split[0],split[1]);
      intrestDic.put(split[0], new ArrayList<String>(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 < HEADER_SIZE) {
      continue;
      }

      Accounts.put(split[0], split[1]);
      intrestDic.put(split[0], new ArrayList<String>(Arrays.asList(split[2].split(";"))));
      List<String> friends = Arrays.asList(split).subList(HEADER_SIZE, split.length));
      FriendsDic.put(split[0], new ArrayList<String>(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.






      share|improve this answer
























        0












        0








        0






        Range-based for loop




        for (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<String> friends = new ArrayList<>();
        List<String> 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 < 3) {
        continue;
        }

        String interestArray = splitTxt(split[2], ";");
        List<String> friends = new ArrayList<>();
        List<String> 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.



        asList




                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);



        You don't need to manually copy one by one.



                Accounts.put(split[0],split[1]);
        intrestDic.put(split[0], new ArrayList<String>(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 < HEADER_SIZE) {
        continue;
        }

        Accounts.put(split[0], split[1]);
        intrestDic.put(split[0], new ArrayList<String>(Arrays.asList(split[2].split(";"))));
        List<String> friends = Arrays.asList(split).subList(HEADER_SIZE, split.length));
        FriendsDic.put(split[0], new ArrayList<String>(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.






        share|improve this answer












        Range-based for loop




        for (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<String> friends = new ArrayList<>();
        List<String> 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 < 3) {
        continue;
        }

        String interestArray = splitTxt(split[2], ";");
        List<String> friends = new ArrayList<>();
        List<String> 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.



        asList




                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);



        You don't need to manually copy one by one.



                Accounts.put(split[0],split[1]);
        intrestDic.put(split[0], new ArrayList<String>(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 < HEADER_SIZE) {
        continue;
        }

        Accounts.put(split[0], split[1]);
        intrestDic.put(split[0], new ArrayList<String>(Arrays.asList(split[2].split(";"))));
        List<String> friends = Arrays.asList(split).subList(HEADER_SIZE, split.length));
        FriendsDic.put(split[0], new ArrayList<String>(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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Apr 29 '17 at 1:43









        mdfst13

        17.4k52156




        17.4k52156






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Code Review Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            Use MathJax to format equations. MathJax reference.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f161895%2fstoring-variables-to-a-map-by-splitting-a-text-file%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            404 Error Contact Form 7 ajax form submitting

            How to know if a Active Directory user can login interactively

            TypeError: fit_transform() missing 1 required positional argument: 'X'