Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8280461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T09:41:14+00:00 2026-06-08T09:41:14+00:00

Say I have a hashmap with String type as key and ArrayList type as

  • 0

Say I have a hashmap with String type as key and ArrayList type as value, example {"value1"=["one","three","five"], "value2"=["two","four","six"]} where “value1” and “value2” are keys. I want to write the above hashmap data in following format. (so that I can read the csv file in excel)

value1,value2
one,two
three,four
five,six

My idea was to write the first key and its values as follows

value1
one
three
five

Then I was thinking of using the seek method in RandomAccessFile class to back to line 1 and again write the second key and its values. However I am not able to accomplish this task since seek function takes in the length of strings in the entire file and writes the new string after it. While I wanted the pointer to jump to the first line and append the string. Is there a better way to do this?. A quick example would be much appreciated.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T09:41:15+00:00Added an answer on June 8, 2026 at 9:41 am

    Why can’t you just use 4 Strings, one for each row? Something like this:

    StringBuilder keyRow = new StringBuilder();
    StringBuilder value1 = new StringBuilder();
    StringBuilder value2 = new StringBuilder();
    StringBuilder value3 = new StringBuilder();
    
    Iterator keys = hashmap.keySet().iterator();
    boolean notFirst = true;
    while(keys.hasNext()) {
        String key = (String)keys.next();
        ArrayList list = (ArrayList)hashmap.get(key);
        if(!notFirst) {
            keyRow.append(",");
            value1.append(",");
            value2.append(",");
            value3.append(",");
        }
        keyRow.append(key);
        value1.append((String)list.get(0));
        value2.append((String)list.get(1));
        value3.append((String)list.get(2));
        notFirst = false;
    }
    

    Then at the end, just take the 4 Strings

    String csv = keyRow.toString()+"\n"+value1.toString()+"\n"+value2.toString()+"\n"+value3.toString();
    

    Note that this example isn’t really proper CSV. Strings with commas aren’t wrapped in quotes.


    Or you iterate through the HashMap a thousand times if you have thousands of these rows. To save a bit of time from looking up a key, you can put them all in an ArrayList:

    StringBuilder csv = new StringBuilder();
    int row = 0;
    ArrayList<ArrayList> list = new ArrayList<ArrayList>();
    
    // Write the keys row:
    Iterator keys = hashmap.keySet().iterator();
    boolean notFirst = true;
    while(keys.hasNext()) {
        String key = (String)keys.next();
        ArrayList tmp = (ArrayList)hashmap.get(key);
        if(!notFirst) {
            csv.append(",");
        }
        csv.append(key);
        // store list
        list.add(tmp);
        notFirst = false;
    }
    csv.append("\n");
    
    
    // Write the rest of the rows
    while(row<numberOfTotalRow) {
        notFirst = true;
        for(int x=0;x<list.size();x++) {
            if(!notFirst) {
                csv.append(",");
            }
            csv.append((String)list.get(x).get(row));
            notFirst = false;
        }   
        row++; 
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to have a HashMap with only one key-value object. I have
Say I have a hashmap, $hash = array('fox' => 'some value', 'fort' => 'some
Say I have the following JSON string returned from server: { response:{ imageInstances:{ one:{
let say I have this code Map<String, String> list = new HashMap<String, String>(); list.put(number1,
I have a relatively complicated generic type (say Map<Long,Map<Integer,String>> ) which I use internally
Say I have a HashMap of HashMaps defined like this: hashMap = new HashMap<String,
Let's say I have a HashMap: val userMap = new HashMap[String, String] userMap +=
Lets say have this immutable record type: public class Record { public Record(int x,
Say I have three tables, a table of users, a table of around 500
I have two lists ( not java lists, you can say two columns) For

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.