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 8790851
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:45:35+00:00 2026-06-13T22:45:35+00:00

For example: key1: 1,2,3,4 key2: 5,6 will convert to key1|key2 1|5 2|6 3 4

  • 0

For example:

key1: 1,2,3,4
key2: 5,6

will convert to

key1|key2
1|5
2|6
3
4

And I know if we do this iteratively, there a lot of ways which are not efficient. I am wondering if there is any built-in methods or anything which can transform them directly.

HashMap<String, String> h1 = new HashMap<String, String>;
h1.put("key1", "1,2,3,4"); 
h1.put("key2", "5,6");

need to print out as pipe delimited format:

key1|key2
1|5
2|6
3
4
  • 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-13T22:45:35+00:00Added an answer on June 13, 2026 at 10:45 pm

    You can use a class like this one:

    import java.util.*;
    
    class LegacyGlueifier
    {
        private LegacyGlueifier()
        {
        }
    
        public static String generateLegacyDataset(Map<String, String> data)
        {
            final ArrayList<ArrayList<String>> lists = new ArrayList<ArrayList<String>>();
            final int width = data.size();
    
            int i = 0;
            for (Map.Entry<String, String> entry : data.entrySet())
            {
                String[] values = entry.getValue().split(",");
                changeDims(lists, width, values.length + 1);
    
                for (int j = 0; j < values.length; ++j) setValue(lists, j + 1, i, values[j]);
                setValue(lists, 0, i, entry.getKey());
                ++i;
            }
    
            return stringify(lists);
        }
    
        private static void changeDims(ArrayList<ArrayList<String>> lists, int width, int newHeight)
        {
            while (lists.size() < newHeight) lists.add(arrayListOfSize(width));
        }
    
        private static ArrayList<String> arrayListOfSize(int w)
        {
            ArrayList<String> list = new ArrayList<String>(w);
            while (list.size() < w) list.add(null);
            return list;
        }
    
        private static void setValue(ArrayList<ArrayList<String>> lists, int row, int col, String val)
        {
            ArrayList<String> temp = lists.get(row);
            temp.set(col, val);
            // System.out.println("SET: " + row + " " + col + ": " + val);
        }
    
        private static String swapNullWithEmpty(String s)
        {
            if (s == null) return "";
            return s;
        }
    
        private static String stringify(ArrayList<ArrayList<String>> lists)
        {
            StringBuilder sb = new StringBuilder();
            for (ArrayList<String> sublist : lists)
            {
                if (sublist.size() != 0) sb.append(swapNullWithEmpty(sublist.get(0)));
                for (int i = 1; i < sublist.size(); ++i)
                    sb.append("|").append(swapNullWithEmpty(sublist.get(i)));
                sb.append("\n");
            }
    
            return sb.toString();
        }
    }
    

    Invocation is String dataset = LegacyGlueifier.generateLegacyDataset(myMap)

    I ran it through a basic test case to see if it worked but I’d still test it more thoroughly since you’re going to be using it.

    Its time complexity is somewhere between linear to the total number of comma delimited fields in the original dataset and linear to the total number of fields in the output dataset (including blank ones).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Example of my Code: dictionary={ 'key1' : 'value1' , 'key2' : 'value2' , 'key1'
So I have a file of keys, for example: key1 key2 key3 and I
This is not the clearest of question titles, sorry about that, but I will
Let say we have set of URLs like this yoursite.com/39827-key1-key2-key3 yoursite.com/132-key1-key2-key3 yoursite.com/9223455-key1-key2-key3 and so
I have an array like this: $a = array( 'key1' => 'value1', 'key2' =>
If I have an array that contains ['key1', 'key2', 'key3'] is there any way
Directly from the python requests examples is this snippet: payload = {'key1': 'value1', 'key2':
I have 2 tables which look like: Table A key1 key2 key3 1 a
Example: print max(chain_length(i) for i in xrange(1,10001)) This returns the maximum/biggest chain_length (an arbitrary
How can i extract key value pairs from this xml example using linq: <foo>

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.