Search This Blog

Thursday, March 14, 2013

Order HashTable Or Vector ; HashMap TreeMap



/*Now remember you can use this in mvc frame work even hibernate lets say you have lookups listener to load lookups on deploy to servlet context instead of using javascript ordering on each client machine you can do this on deployment phase this way you will save alot of overhead on page request ... */

1-----------------the table search method (you can get result in any way you want ; google it like this one here
mysql get db results)

public static void loadGMajor(ServletContext servletContext) {  
        try {  
             /*the below method to hit database and get back with 
             hash map of  
             key value pairs e.g  
             it will look something like this ...
             for illustration only insid SearchGmajorLookup
            while (theResultSet.next()) {
             LkGmajor lkGmajor = new LkGmajor();

             lkGmajor.setMajor(theResultSet.getString("MAJOR"));
             lkGmajor.setMajorD(theResultSet.getString("MAJOR_D"));
             lkGmajor.setMajorEd(theResultSet.getString("MAJOR_ED")); 
                 
             //result hashmap returned 
             results.put(lkGmajor.getMajor(), lkGmajor);
            }*/
 

            HashMap gMajorHT = lookupsDAO.SearchGmajorLookup(params);  
              

     HashMap<String, LkGmajor> map = (HashMap<String, LkGmajor>)gMajorHT;  
            ValueComparator bvc = new ValueComparator(gMajorHT);  
            TreeMap<String, LkGmajor> sorted_map = new TreeMap(bvc);  

            //System.out.println("unsorted map");  
            /*for (String key : map.keySet()) {
            System.out.println("key/value: " + key + "-" + map.get(key));
            }*/
  

            sorted_map.putAll(map);  

            /*System.out.println("results");
            for (String key : sorted_map.keySet()) {
            System.out.println("key/value: " + key + "-" + sorted_map.get(key));
            }*/
  

//saving the Ordered TreeMap In servlet Context now you can use JSP tag for each to loop it and //display
            servletContext.setAttribute("gMajorHT", (TreeMap<String, LkGmajor>) sorted_map); 
              

        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    } 



2-----------the Bean implements Comparable Interface


import java.io.Serializable; 

/**
 *
 * @author shareef hiasat
 */
 
public class LkGmajor implements Serializable, Comparable<LkGmajor> { 

    private String major; 
    private String majorD; 
    private String majorEd; 

    public LkGmajor() { 
        major = ""
        majorD = ""
        majorEd = ""
    } 

    public LkGmajor(String major) { 
        this.major = major; 
    } 

    public LkGmajor(String major, String majorD) { 
        this.major = major; 
        this.majorD = majorD; 
    } 

    public String getMajor() { 
        return major; 
    } 

    public void setMajor(String major) { 
        this.major = major; 
    } 

    public String getMajorD() { 
        return majorD; 
    } 

    public void setMajorD(String majorD) { 
        this.majorD = majorD; 
    } 

    public String getMajorEd() { 
        return majorEd; 
    } 

    public void setMajorEd(String majorEd) { 
        this.majorEd = majorEd; 
    } 

    @Override 
    public int hashCode() { 
        int hash = 0; 
        hash += (major != null ? major.hashCode() : 0); 
        return hash; 
    } 

    @Override 
    public boolean equals(Object object) { 
        // TODO: Warning - this method won\'t work in the case the id fields are not set 
        if (!(object instanceof LkGmajor)) { 
            return false
        } 
        LkGmajor other = (LkGmajor) object; 
        if ((this.major == null && other.major != null) || (this.major != null && !this.major.equals(other.major))) {
            return false
        } 
        return true
    } 

/*this metho is the corner stone here it will compare 2 object of the same type depending on a private member such majorD here in our case*/


public int compareTo(LkGmajor o) { 


        if (o.getMajorD() != null && this.getMajorD() != null) { 
            if (o.getMajorD().equals(this.getMajorD())) { 
                return -1; 
            } else { 
                return this.getMajorD().compareTo(o.getMajorD()); 
            } 
        } else { 
            return 0; 
        } 
    } 
}






3----------The Most Important Part the Class Comparator


import java.util.Comparator; 
import java.util.Map; 


/**
 *
 * @author shareef hiasat
 */
 
public  class ValueComparator implements Comparator{ 

    Map base; 
  public ValueComparator(Map base) { 
      this.base = base; 
  } 

  public int compare(Object a, Object b) { 

      return compareValue(a, b); 
  
  } 

  private <E extends Comparable>int compareValue(Object a, Object b) 
  { 
      return ((E)base.get(a)).compareTo((E)base.get(b)); 
  } 



Its Simple Right But I searched the net and after many reading i built this not taken from any one so please dont foget to refer the link and do not copy my hard work and saying its yours :)


Thanks Java open source

//example of using you now in jsp
<td height="30" >
   <select id="ddlMajor" name="ddlMajor" style="width:160px">
      <option value="-1">Select</option>
      <c:forEach var="major" items="${gMajorHT}">
         <option value="${major.key }">${major.value.majorD}</option>
      </c:forEach>
   </select>
</td>

No comments:

Post a Comment