Results 1 to 20 of 20
Like Tree2Likes
  • 1 Post By jim829
  • 1 Post By Norm

Thread: Import .csv content to HashMap

  1. #1
    TomTom1 is offline Member
    Join Date
    Jul 2016
    Posts
    36
    Rep Power
    0

    Default Import .csv content to HashMap

    Hey there,

    I`m trying to read all csv files in a specific folder into a HashMap<String, List<String>>, where the key is representing the filenames. The list contains the csv content.
    The content of the csv files are just two double values per line, for example: 0.01,24.0.

    What I'm currently doing is this: I create a filelist with all file paths in the folder.
    Then I use this list to loop through all the files in that list.
    In the loop I'm reading the current file, and put the current filename with a list of read values into a Hashmap.
    To code is executable but my finished Hashmap only gets one entry.

    Java Code:
       public static void main(String[] args) throws URISyntaxException {
    
    
            ArrayList<Path> fileList = listFiles();
            Map<String, List<String>> lists = new HashMap<>();
    
            for (int i = 0; i < fileList.size(); i++) {
    
                Path currentFilePath = fileList.get(i);
                System.out.println("current filepath is: " + currentFilePath);
                List<String> content = new ArrayList<>();
                FileInputStream in;
                
                try {
                    
                    in = new FileInputStream(currentFilePath.toString());
                    BufferedReader reader = new BufferedReader(new InputStreamReader(in));
                    String tmp;
    
                    while ((tmp = reader.readLine()) != null) {
                    content.add(tmp);
                        
                    }
                    reader.close();
                    lists.put(currentFilePath.toString(), content);
                    System.out.println("Current key: " + currentFilePath.toString());            
                    
                } catch (FileNotFoundException ex) {
                    Logger.getLogger(CsvCreator.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IOException ex) {
                    Logger.getLogger(CsvCreator.class.getName()).log(Level.SEVERE, null, ex);
                }
            } 
                 System.out.println("The finished map looks like this: " + lists);
        }
    I just want to cache the csv files content in the Map, and create new csv files with the same content later.
    Last edited by TomTom1; 09-23-2016 at 02:48 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Import .csv content to HashMap

    my finished Hashmap only gets one entry.
    How many should it get?
    Try debugging the code by adding some print statements that print out the values of the filenames and keys that the program sees and uses.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    TomTom1 is offline Member
    Join Date
    Jul 2016
    Posts
    36
    Rep Power
    0

    Default Re: Import .csv content to HashMap

    My current selected folder has 7 files. When I print "currentFilePath" in the for loop everything looks like it should.
    All 7 file paths are printed. The same is true for the key. However, only one key out of the middle of the files ends up in my HashMap

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Import .csv content to HashMap

    What is printed out by the print statements I suggested that I suggested that you add for debugging?
    Can you copy the full text of what the program prints out and paste it here?
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    TomTom1 is offline Member
    Join Date
    Jul 2016
    Posts
    36
    Rep Power
    0

    Default Re: Import .csv content to HashMap

    Sure, I updated my first post so you can check where I added the prints.

    This is the output:
    Java Code:
    run:
    current filepath is: C:\Users\Tom\Desktop\Test\File1.log
    Current key: C:\Users\Tom\Desktop\Test\File1.log
    current filepath is: C:\Users\Tom\Desktop\Test\File2.log
    Current key: C:\Users\Tom\Desktop\Test\File2.log
    current filepath is: C:\Users\Tom\Desktop\Test\File3.log
    Current key: C:\Users\Tom\Desktop\Test\File3.log
    current filepath is: C:\Users\Tom\Desktop\Test\File4.log
    Current key: C:\Users\Tom\Desktop\Test\File4.log
    current filepath is: C:\Users\Tom\Desktop\Test\File5.log
    Current key: C:\Users\Tom\Desktop\Test\File5.log
    current filepath is: C:\Users\Tom\Desktop\Test\File6.log
    Current key: C:\Users\Tom\Desktop\Test\File6.log
    current filepath is: C:\Users\Tom\Desktop\Test\File7.log
    Current key: C:\Users\Tom\Desktop\Test\File7.log
    The finished map looks like this: {C:\Users\Tom\Desktop\Test\File4.log=[0.004606,13.66459771990776, 0.01466,13.664597   ...

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Import .csv content to HashMap

    You say everything looks okay. But that is 'your' observation. You may not recognize problems in the output. I can think of at least four
    places where you should be putting print statements. The first is right after you get the list from listFiles(). And when you print
    lists and maps, put them in a proper loop.

    Regards,
    Jim
    Last edited by jim829; 09-23-2016 at 03:00 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Import .csv content to HashMap

    What's ...? Please print the map in a proper loop. And remember that the order of the keys for the hashmap are indeterminant.

    Regards,
    Jim
    Norm likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Import .csv content to HashMap

    Is there anything in the log file? Add a call to printStackTrace() to all the catch blocks so any errors are easy to see in the console.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: Import .csv content to HashMap

    If you want to simplify the output then just print the keys in the Map.
    At least that way you'll see if it's added each file.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  10. #10
    TomTom1 is offline Member
    Join Date
    Jul 2016
    Posts
    36
    Rep Power
    0

    Default Re: Import .csv content to HashMap

    @jim829, I know that hashmap keys are indeterminant but they appear just like they do in the folder browser.
    I just added "..." becauce the csv files have a couple thousand lines.

    this is the output for the received arraylist from listFiles():
    Java Code:
    run:
    This is the received filelist:
    C:\Users\Tom\Desktop\Test\File1.log
    C:\Users\Tom\Desktop\Test\File2.log
    C:\Users\Tom\Desktop\Test\File3.log
    C:\Users\Tom\Desktop\Test\File4.log
    C:\Users\Tom\Desktop\Test\File5.log
    C:\Users\Tom\Desktop\Test\File6.log
    C:\Users\Tom\Desktop\Test\File7.log
    @Norm,
    yes the log file content looks like this:
    0.01,24.0
    0.03,24.1
    0.05,24.5
    ...and so on

    I added printStackTrace() to the catch blocks put it's not triggered.

    @Tolls

    I added this after the loop is done:
    Java Code:
            for (Map.Entry<Path, List<String>> entry : lists.entrySet()) {
        Path key = entry.getKey();
        System.out.println("The finished map contains key: " + key);
    So here it looks like it actually worked.

    The finished map contains key: C:\Users\Tom\Desktop\Test\File1.log
    The finished map contains key: C:\Users\Tom\Desktop\Test\File2.log
    The finished map contains key: C:\Users\Tom\Desktop\Test\File3.log
    The finished map contains key: C:\Users\Tom\Desktop\Test\File4.log
    The finished map contains key: C:\Users\Tom\Desktop\Test\File5.log
    The finished map contains key: C:\Users\Tom\Desktop\Test\File6.log
    The finished map contains key: C:\Users\Tom\Desktop\Test\File7.log
    Last edited by TomTom1; 09-23-2016 at 03:38 PM.

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Import .csv content to HashMap

    Does the hashmap always have the same one key?
    What if the for loop is changed to only look at the first file? What is in the map then?
    Java Code:
     for (int i = 0; i < 1 /*fileList.size()*/; i++) { // only the first
    If you don't understand my response, don't ignore it, ask a question.

  12. #12
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Import .csv content to HashMap

    It may be possible that when you printed the map with a single statement, it overprinted the same line, resulting in
    confusing output.

    You can also double check your map by printing out the keys, followed by the size of the corresponding list. This should provide an idea
    of whether you populated each array list.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Import .csv content to HashMap

    the csv files have a couple thousand lines.
    For testing use a folder with small csv files.
    jim829 likes this.
    If you don't understand my response, don't ignore it, ask a question.

  14. #14
    TomTom1 is offline Member
    Join Date
    Jul 2016
    Posts
    36
    Rep Power
    0

    Default Re: Import .csv content to HashMap

    @Norm, yes it's always the same key when I do:

    Java Code:
    System.out.println("The finished map looks like this: " + lists);
    the output always is (which appears just to be the first Map entry):
    Java Code:
    The finished map looks like this: {C:\Users\Tom\Desktop\Test\File4.log=[0.004606,13.66459771990776,
    and if I do it the way Norm suggested:
    Java Code:
       for (Map.Entry<Path, List<String>> entry : lists.entrySet()) {
    Path key = entry.getKey();
    List value = entry.getValue();
    System.out.println("The finished map contains key: " + key + "and the corresponding list: " + value);
    the output is:
    Java Code:
    The finished map contains key: C:\Users\Tom\Desktop\Test\File4.log and the corresponding list: [0.004606,13.66459771990776,
    The finished map contains key: C:\Users\Tom\Desktop\Test\File6.log and the corresponding list: [0.004156,15.190477669239044,
    The finished map contains key: C:\Users\Tom\Desktop\Test\File5.log and the corresponding list: [0.004156,14.885301679372787,
    The finished map contains key: C:\Users\Tom\Desktop\Test\File1.log and the corresponding list: [0.004606,13.969773709774017,
    The finished map contains key: C:\Users\Tom\Desktop\Test\File2.log and the corresponding list: [0.004606,14.274949699640274,
    The finished map contains key: C:\Users\Tom\Desktop\Test\File3.log and the corresponding list: [0.004606,14.58012568950653,
    The finished map contains key: C:\Users\Tom\Desktop\Test\File7.log and the corresponding list: [0.004156,14.885301679372787,
    But what's the reason for that?
    Last edited by TomTom1; 09-24-2016 at 12:15 AM.

  15. #15
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Import .csv content to HashMap

    The returned keys are not guaranteed to be in any particular order for a HashMap. If you add another entry, the new order could be
    completely different.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  16. #16
    TomTom1 is offline Member
    Join Date
    Jul 2016
    Posts
    36
    Rep Power
    0

    Default Re: Import .csv content to HashMap

    Yes, but the order of the keys doesn't matter, I guess if it would, one would use a linked hashmap.

    One more question:
    I now want to use the hashmap to create new csv files.
    The key is the file name, the list my file content.
    The way I have it right now the file content would look like this:
    [0.004606,13.969773709774017, 0.01466,13.969773709774017, 0.02477....
    but it should look like this:
    0.004606,13.969773709774017
    0.01466,13.969773709774017
    0.02477....
    One way I could do it is to substitute every even comma number with a new line. But I don't know if that would be a good solution?
    Or is it better to store the original values in a different type then the current used list?

  17. #17
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Import .csv content to HashMap

    Well, there are a number of options. One is to make a small class that holds the values and store them in the list. You could use Point2D.Double
    for example. And the the map would be Map<String,List<Point2D>> where each Point2D is an x, y value pair. You could also do something like
    Map<String,List<List<String>>> where each list is a list of two valued lists. It all depends how how best to serve your specific needs. For the
    Point2D.Double method you would have to convert them to double before storing them of course. Or you could just do the newline thing
    which you suggested.

    Regards,
    Jim
    Last edited by jim829; 09-23-2016 at 07:49 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  18. #18
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Import .csv content to HashMap

    I missed the resolution of the original problem. Was the code working correctly but the OP didn't look at the debug print out closely enough to see that it was working?
    If you don't understand my response, don't ignore it, ask a question.

  19. #19
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Import .csv content to HashMap

    I believe so. I don't think the OP wasn't printing them out usefully because they ran off the page. The middle one showed first because, as we know,
    the keys are not ordered in any fashion.

    Regards,
    Jim
    Last edited by jim829; 09-24-2016 at 01:32 AM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  20. #20
    TomTom1 is offline Member
    Join Date
    Jul 2016
    Posts
    36
    Rep Power
    0

    Default Re: Import .csv content to HashMap

    I think so too. System.out.println(lists) prints the whole Map in one console line.
    Since the list contains too many values the other keys are not shown, but they are there.
    Thanks everyone!

Similar Threads

  1. file import which is not standard import
    By willemjav in forum New To Java
    Replies: 11
    Last Post: 04-06-2014, 06:58 AM
  2. final HashMap hm=new HashMap();
    By sangramkeshari.jena in forum New To Java
    Replies: 4
    Last Post: 07-21-2011, 09:44 PM
  3. Replies: 1
    Last Post: 04-19-2011, 08:52 AM
  4. Replies: 7
    Last Post: 12-08-2009, 07:17 PM
  5. Replies: 2
    Last Post: 05-30-2009, 10:42 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •