Solved

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

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
hibernate example issues from command prompt 10 85
Java: The Public Class Main 4 65
Setup GlassFish 4 55
Strange router problem - can't access hotmail.com 14 108
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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:

734 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