1

I am looking for better algorithm to print a LinkedHashMap in table format by its key. I explain scenario here,

//hashmap goes like this
LinkedHashMap<String , ArrayList<String>> hash=new LinkedHashMap<String,ArrayList<String>>();

// put some values in some arraylist
ArrayList<String> value1=Arrays.asList("s1", "s2", "s3");
ArrayList<String> value2=Arrays.asList("s4", "s5", "s6");
ArrayList<String> value1=Arrays.asList("s7", "s8", "s9");

// put values in hashmap
hash.put("key1",value1);
hash.put("key2",value2);
hash.put("key3",value3);

Now, i wish output should be like this in html table.

key1 key2 key3

s1    s4   s7

s2    s5   s6

s3    s6   s9

what is good way to do this? thanks .

0

3 Answers 3

1

Following code should do

public static void main(String[] args) {
        // hashmap goes like this
        LinkedHashMap<String, List<String>> hash = new LinkedHashMap<String, List<String>>();

        // put some values in some arraylist
        List<String> value1 = (List<String>) Arrays.asList("s1", "s2", "s3");
        List<String> value2 = (List<String>) Arrays.asList("s4", "s5", "s6");
        List<String> value3 = (List<String>) Arrays.asList("s7", "s8", "s9");

        // put values in hashmap
        hash.put("key1", value1);
        hash.put("key2", value2);
        hash.put("key3", value3);

        int count = 0;
        boolean exit = false;
        while (!exit) {
            for (String key : hash.keySet()) {
                String value = hash.get(key).get(count);
                System.out.printf("\t" + value);
            }
            System.out.printf("\n");
            count++;
            if (count == hash.get("key1").size()) {
                exit = true;
            }
        }

    }

And following is the output as desired:

s1  s4  s7
s2  s5  s8
s3  s6  s9
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much . Just did some typecasting and it worked . thanks
1

if you are using JSP page, try this

LinkedHashMap> hash=new LinkedHashMap>();

// put some values in some arraylist
ArrayList<String> value1=Arrays.asList("s1", "s2", "s3");
ArrayList<String> value2=Arrays.asList("s4", "s5", "s6"); 
ArrayList<String> value1=Arrays.asList("s7", "s8", "s9");

// put values in hashmap
hash.put("key1",value1);
hash.put("key2",value2);
hash.put("key3",value3);

<% 
  for(Entry<String, ArrayList<String>> en: hash.entrySet()) {
      %>
   <td> 
     <% en.getKey(); %>
   </td>
 <%  }
 %>
 </td>
  </tr>

you can also do this using JSTL tags.

1 Comment

this only prints the LinkedHashMap keys, not the values in the ArrayList.
1

Try,

 StringBuffer sb=new StringBuffer();
 sb.append("<tr>");
 sb.append("<td>" + hash.get("key1").get(0) + "</td>");
 sb.append("<td>" + hash.get("key2").get(0) + "</td>");
 sb.append("<td>" + hash.get("key3").get(0) + "</td>");
 sb.append("</tr>");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.