Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

920 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

13 Experts available now in Live!

Get 1:1 Help Now