Solved

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

Posted on 2004-09-20
3
1,101 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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
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…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

708 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now