• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Splitting Strings and putting them into a TreeMap

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am reading in a list of long Strings



I am trying to use the first String before the comma on every line as a key in a TreeMap(A+,A-,B+,O,B-)

Then I want to use the subsequent names following the key to be the mapped value

For example, A+ = G.W. Bush B.H Obama J.F. Kennedy

I found out a way to do this for the first line, but I do not know how to make it loop and do this for every line.


 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Silas,
Welcome to CodeRanch!

Before we get to your question, I see a potential problem. Keys need to be unique. It seems like you only have five distinct values. Does this mean that there are only 5 rows in your file? If so, you are fine. If not, you will lose the data in earlier rows as it gets replaced by later instances of that key.

As to your actual question, this is the pattern for reading from a file while only having one line in memory at a time.



If you can read the whole file in one pass and are on Java 7, you can all Files.readAllLines() and then just loop through that list.
 
reply
    Bookmark Topic Watch Topic
  • New Topic