Solved

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

Posted on 2004-09-20
3
1,108 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

726 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