skip to main | skip to sidebar

Java Programs and Examples with Output

Pages

▼
 
  • RSS
  • Twitter
Friday, October 5, 2012

Sorting using Reflections

Posted by Raju Gupta at 10:49 PM – 0 comments
 
As we call this method , passing the required data, for each object in the arraylist using Java reflections the corresponding set of fields are extracted. From there we find a sensible key for the data in each field and place both of them in a hashmap. Using the TreeMap, we will now sort the hash map object, hence achieving the objective. Later the Objects are moved from Hash map to an array List and is the returned by the Method.

please note that here the fields that are delcared public can only be extracted using our way of implementation of reflections.
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;
import java.util.ArrayList;
import java.util.Collections;


public class Sort {

    private ArrayList valueList = new ArrayList();
    private Map sortObject;

    public ArrayList sortedList(ArrayList listParam, String coloumnName, String dataType) throws IllegalArgumentException, IllegalAccessException, ParseException {
        
        double saveUs= 0.0;
        sortObject = new HashMap();
        System.out.println("list param size is " + listParam.size());
        for (int i = 0; i < listParam.size(); i++) {
            Field f[] = ((Class) ((Object) listParam.get(i)).getClass()).getFields();/* This will return the Set of Fields for each object in arraylist*/


            for (int j = 0; j < f.length; j++) {

                String type = f[j].getGenericType().toString();  /*retunrns the original data type of field */
                String colName = f[j].getName();/*return the name of the Field*/
                String nullValue= "";
                if (coloumnName.equalsIgnoreCase(colName)) {     /*checking for the name of column depending on which data needs to be sorted*/

                    if (!dataType.toLowerCase().contains("date")) {
                        if (type.toLowerCase().contains("string")) {
                                                  
                            if (f[j].get((Object) listParam.get(i)) == null) {
                                sortObject.put(nullValue.concat(new Integer(i).toString()), listParam.get(i));/*checking for the null values*/
                            } else {
                                sortObject.put(f[j].get((Object) listParam.get(i)).toString().toLowerCase().concat(new Integer(i).toString()), listParam.get(i));
                            }
                        }
                        }

                        if (type.toLowerCase().contains("int")) {
                            if (f[j].get((Object) listParam.get(i)) == null) {
                                sortObject.put(nullValue.concat(new Integer(i).toString()), listParam.get(i));/*checking for the null values*/
                            } else {

                                sortObject.put( (saveUs+(Integer) (f[j].get((Object) listParam.get(i))))+i*0.0001 , listParam.get(i));
                            }
                        }

                        if (type.toLowerCase().contains("date")) {

                            if (f[j].get((Object) listParam.get(i)) == null) {
                                sortObject.put(nullValue.concat(new Integer(i).toString()), listParam.get(i));/*checking for the null values*/
                            } else {
                                sortObject.put((Long) (((Date) (f[j].get((Object) listParam.get(i)))).getTime()) + i, listParam.get(i));
                            }
                        }

                    } else {
                        
                        if (!type.toLowerCase().contains("date")) {
                            Date dt = new SimpleDateFormat("dd/MM/yyyy").parse((String) f[j].get((Object) listParam.get(i)));
                            sortObject.put((saveUs+(Long) ((dt).getTime())) + i*0.01, listParam.get(i));
                        } else {
                            sortObject.put((saveUs+(Long) (((Date) (f[j].get((Object) listParam.get(i)))).getTime()) )+ i*0.0001, listParam.get(i));

                        }
                    }

                }
            }
        }


        Map sortedMap = new TreeMap(sortObject);  /*Sorting the hash map object.*/

        Iterator it = sortedMap.entrySet().iterator();

        while (it.hasNext()) {
            Map.Entry pairs = (Map.Entry) it.next();
            valueList.add(pairs.getValue()); /* retrieving the sorted data and placing into another arraylist*/
        }

        
     
        return valueList;
    }
}   

Leave a Reply

Newer Post Older Post
Subscribe to: Post Comments ( Atom )
  • Popular
  • Recent
  • Archives
Powered by Blogger.
 
 
 
© 2011 Java Programs and Examples with Output | Designs by Web2feel & Fab Themes

Bloggerized by DheTemplate.com - Main Blogger