Solved

Maintain Order using TreeMap wrapped in HashMap.  Need urgent help!!

Posted on 2004-09-20
3
1,104 Views
Last Modified: 2010-08-05
Hi All,
I have the following question:
I store data in HashMap, but I need that the data displayed in certain order on my JSP.  I've created a method that sorts my unsorted HashMap, puts the data into TreeMap and then TreeMap is copied to the HashMap.  When I call this method from my JSP it seems that the order is not always retained.  Could you help me to understand why it happens?  Please see the method below..
Thanks!!!


public HashMap sortAllForDisplay(HashMap unsortedData){
            HashMap sortedData = new HashMap();
            ArrayList workAssignOrders = new ArrayList();
            HashMap workAssignLookUp = new HashMap();
      
            JobAccess jbAccess = new JobAccess(this.getDBConnection());
            List l = jbAccess.loadAll();
            if (l!=null && l.size()>0)
            {
                  for (int i=0;i<l.size();i++)
                  {
                        JobData jbData = (JobData)l.get(i);
                        String jobName = jbData.getJobName();
                        int jobId = jbData.getJobId();
                        String jobOrderNumber = jbData.getJobVal2(); //Work Assignment Order
      
                        if (jobOrderNumber==null)
                        {
                              jobOrderNumber = "99999";
                        }
                        
                        Integer jobOrderNumberInt = new Integer(Integer.parseInt(jobOrderNumber));

                        if (!workAssignOrders.contains(jobOrderNumberInt)){
                              workAssignOrders.add(jobOrderNumberInt);
                        }
                        workAssignLookUp.put(new Integer(jobId), jobOrderNumberInt);
                  }
                  
                  
                  if(workAssignOrders!=null)
                  {
                        Collections.sort(workAssignOrders); // Sort Orders in ascending order
                        Collections.reverse(workAssignOrders);
                        
                  
                                    if (workAssignOrders.size()>0)
                                    {
                                          TreeMap tempMap = new  TreeMap();
                                          int realKey = 0;
                                          for (int x=0;x<workAssignOrders.size();x++)
                                          {
                                                                        
                                                            Integer order = (Integer)workAssignOrders.get(x);
                                                            Collection col = unsortedData.values();                                                                                    
                                                            Iterator it = col.iterator();
                                                                  
                                                                  //int keyCounterInner = 1;
                                                                              while (it.hasNext())
                                                                              {
                                                                                    
                                                                                    ArrayList rowData = (ArrayList)it.next();
                                                                                    Integer dataLaborId = (Integer)rowData.get(0);
                                                                                    Integer compareJobOrder = (Integer)workAssignLookUp.get(dataLaborId);
                                                                                    

                                                                                    if (order.compareTo(compareJobOrder)==0)
                                                                                    
                                                                                    {
                                                                        
                                                                                          tempMap.put(new Integer(realKey), rowData);
                                                                                          
                                                                                          realKey ++;
                                                                                          
                                                                                          //
                                                                                    }
                                                                                    
                                                                                    
                                                                              }
                                                      
                                                            
                                                }
                                                
                                                      sortedData.putAll(tempMap);
                                          
                                                
                                          
                                    }
                                    
                                    else
                                    {
                                          sortedData = unsortedData;
                                    }
                  }                  
                  
            }
            
            else
            {
                  sortedData = unsortedData;
            }
            return sortedData;
      }
0
Comment
Question by:germess
  • 2
3 Comments
 
LVL 6

Expert Comment

by:expertmb
ID: 12109331
HashMap
From API doc
This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time.
0
 

Author Comment

by:germess
ID: 12109394
thanks for your response,
Understood! I've read it in the API.. but how can we ensure the order? Should we pass the TreeMap to the JSP? Or sort the hashMap on the JSP? Please let me know your thoughts..
i appreciate your help!
germess
0
 
LVL 6

Accepted Solution

by:
expertmb earned 500 total points
ID: 12109409
>>Should we pass the TreeMap to the JSP?
hmmm thts the best option. its a map with sorted order. i think no need to convert from treemap to hashmap again.
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
micro services vs rest web services 16 105
rhino JavaScript import, load 25 84
Should localization be done inside spring controller 5 26
Java: The Public Class Main 4 16
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question