This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Five Lines of Code and have Christian Clausen on-line!
See this thread for details.
Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Writing HashMap to a Text File

 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi ranchers,

I have an HashMap<String,List<String>> to be written onto a text file. Using poi api i was able to write successfully it to an excel. Now I have to write it to a text file.

The key of the HashMap is a String that represents the column header value

The value of the HashMap is a List<String> that represents the column values.

consider this a sample HashMap -> ("NAME",[JOHN, PAUL, JAI])
("AGE",[20,23,25])
("COUNTRY",[INDIA,UK,GERMANY])

I want the output in my text file as below -

NAME AGE COUNTRY
JOHN 20 INDIA
PAUL 23 UK
JAI 25 GERMANY

All the values list of in the HashMap will be of the same size (3 as in the example). Could you suggest the logic of how can I write since a text file goes line by line rather than column wise as in a excel api
 
Ranch Hand
Posts: 174
Java ME Opera Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Iterate your map:
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Zandis,

Thanks a lot. It perfectly addresses my issue!
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I need to sort the HashMap based upon the Name column and the rest of the column list values should also be sorted based upon the name column.

My previous requirement which I completed -


consider this a sample HashMap -> ("NAME",[JOHN, PAUL, JAI])
("AGE",[20,23,25])
("COUNTRY",[INDIA,UK,GERMANY])

I want the output in my text file as below -

NAME AGE COUNTRY
JOHN 20 INDIA
PAUL 23 UK
JAI 25 GERMANY



Now I have to sort the HashMap values based on the Name column. So I want the result to be as below.

NAME AGE COUNTRY
JAI 25 GERMANY
JOHN 20 INDIA
PAUL 23 UK

Is this possible?

 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would revise your datastructure. Create a Person which holds a the name, age and location. Then you can create a Comparator<Person> which you can use to sort.
 
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic