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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T15:47:11+00:00 2026-06-13T15:47:11+00:00

This is a very generic question. I have a hashmap and I want to

  • 0

This is a very generic question. I have a hashmap and I want to print it in a tabular format dynamically without knowing the size of the contents beforehand. The table should be well spaced just like we see in a Db query result. Are there any libraries/utilities which directly helps in this type of conversion? Or does JAVA have some intrinsic functions which I could make use of?

The code which I have written is a very naive one, and does not cater to dynamic length of the strings. I need the rows to be aligned also.

    StringWriter returnString = new StringWriter();
    Map<String,HashMap<String, String>> hashMap = new HashMap<String, HashMap<String, String>>();
    for (Entry e : hashMap.entrySet()) {
        HashMap<String, Number> hm = (HashMap<String, Number>) e.getValue();
        String key = (String) e.getKey();
        returnString.append("|\t").append(key).append("\t|");
        for (Entry en : hm.entrySet()){
            returnString.append("|\t").append((String) en.getValue()).append("\t|");
        }
        returnString.append("\r\n");
    }        
    return returnString.toString();

The output should be like this irrespective of the strings length

s1      |     s3     |     s4
askdkc  |  asdkask   |   jksjndan
  • 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-13T15:47:12+00:00Added an answer on June 13, 2026 at 3:47 pm

    It looks like you already have the iteration figured out and are just working on the formatting. You could put it into a TableModel, and let the JTable handle the tabular formatting.

    You could select fixed column widths or iterate once over the entries to find the maximum length of each column, then again to print them with appropriate padding.

    Another option would be to extend HashMap so that it records the longest key and value as entries are added:

    package com.example;
    
    public class MyHashMap<K, V> extends java.util.HashMap<K, V> {
    
        private int maxKeyLength = 0;
        private int maxValueLength = 0;
    
        @Override
        public V put(K key, V value) {
            maxKeyLength = Math.max(maxKeyLength, key.toString().length());
            maxValueLength = Math.max(maxValueLength, value.toString().length());
            return value;
        };
    
        public int getMaxKeyLength() {
            return maxKeyLength;
        }
    
        public int getMaxValueLength() {
            return maxValueLength;
        }
    }
    

    Note this ignores the obvious case where you also remove items–depending on your usage pattern, you’ll have to do a little or a lot more work if you also want to shrink the columns when removing entries with the longest keys/values.

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

Sidebar

Related Questions

This may sound like a very generic question but here it goes. I have
okay, i have a generic question .. I want to have my models.py very
This is a very generic 'best practice' question, but here's an example. Let's say
I realize this is a very generic question, but I guess I'm not really
This is probably a very generic question but I will shoot anyway since I
I have a very generic question. When Developing with ASP.NET MVC. It provides a
This is going to be a very generic question and I'm sorry. But lets
I know this is a very generic and subjective question, so feel free to
I have this very simple jQuery function: $(.milestone-in-tree).live({ mouseenter: function() { setTimeout( $.ajax({ type:
I have this very simple br2nl function that I use to take a string

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.