Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Need Help understanding how to Sort within a POJO's

843785Oct 3 2008 — edited Oct 4 2008
To anyone who is willing to give the new Java guy a boost;

I cannot for the life of me grasp this topic of using the compare method from the comparable class to print out the report Id's in order. Below are the classes that I have created to try to accomplish printing the report Id's in order. As illustrated below the ReportPOJO holds several fields, one of which is a set of ReportParamPOJO's. The ReportParamPOJO's hold the report Id's that I want to have sorted. For example, I would like to have the prinout display the values in the following order;


Report Param - AReport01 Label To Date
Report Param - BReport01 Label From Date


Report Param - CReport01 Label Store No.


---------------------------------------------------------------------------------------------------------------
*
*
*import java.util.*;

 *

/** 


* Write a description of class TestTree here. 


* 


* *@author* (DShof) 


* *@version* (09/31/08) 


*/ 

*
public *class *SortReportPOJO

 *

{ 


ReportPOJO reportPOJO = *new* ReportPOJO(); 


ReportParamPOJO pojo = *new* ReportParamPOJO(); 


 


 


 


*public* *static* *void* main(String[] args) { 


*new* SortReportPOJO().go(); 


} 


 


 


*public* *void* go() { 


 


pojo.setId("AReport01"); 


pojo.setLabel("Store No."); 


pojo.setType("SiteId"); 


reportPOJO.addParam(pojo); 


 


pojo = *new* ReportParamPOJO(); 


pojo.setId("BReport01"); 


pojo.setLabel("From Date"); 


pojo.setType("FromDate"); 


reportPOJO.addParam(pojo); 


 


pojo = *new* ReportParamPOJO(); 


pojo.setId("CReport01"); 


pojo.setLabel("To Date"); 


pojo.setType("ToDate"); 


reportPOJO.addParam(pojo); 


 


reportPOJO.setId("Report01"); 


reportPOJO.setName("Report Test 01"); 


 


// Sort the list of paramaters for a given report 


sortList(); 


 


 


} 


*void* sortList() { 


// Sort the list of paramaters for a given report 


 


Iterator iter = reportPOJO.getParams().iterator(); 


 


 


*while*(iter.hasNext()) 


{ 


ReportParamPOJO param = (ReportParamPOJO)iter.next(); 


System.+out+.println("Report Param" + " - " + param.getId() + " Label " + param.getLabel() ); 


} 


 


} 


 


} 


 


 


 

*
abstract *class *ParamCompare *implements *Comparator {

 *

*public* *int* compare(Object one, Object two) { 


ReportParamPOJO o1 = (ReportParamPOJO) one; 


ReportParamPOJO o2 = (ReportParamPOJO) two; 


 


*return* o1.getId().compareTo(o2.getId()); 


 


} 


 


} 
----------------------------------------------------------------------------------------------------------
*
import java.io.Serializable;

 **
import java.util.HashSet;

 **
import java.util.Set;

 *

 

*
public *class *ReportPOJO *implements *Serializable

 *

{ 


/** 


* 


*/ 


*private* *static* *final* *long* +serialVersionUID+ = 1L; 


 


*private* String id; 


*private* String name; 


*private* Set<ReportParamPOJO> params; 


 


 


*public* ReportPOJO() 


{ 


params = *new* HashSet<ReportParamPOJO>(); 


} 


 


*public* String getId() { 


*return* id; 


} 


 


*public* *void* setId(String id) { 


*this*.id = id; 


} 


 


*public* String getName() { 


*return* name; 


} 


 


*public* *void* setName(String name) { 


*this*.name = name; 


} 


 


*public* Set<ReportParamPOJO> getParams() { 


*return* params; 


} 


 


*public* *void* addParam(ReportParamPOJO param) { 


params.add(param); 


} 

}
---------------------------------------------------------------------------------------------------------------------
*
public *class *ReportParamPOJO {

 *

 


/** 


* Serialization ID. Any changes in the class signature and this should be incremented. 


*/ 


*private* *static* *final* *long* +serialVersionUID+ = 1L; 


 


*public* *static* *final* String +PARAM_TYPE_STRING+ = "String"; 


*public* *static* *final* String +PARAM_TYPE_DATE+ = "Date"; 


 


*private* String id; 


*private* String label; 


*private* String type; 


 


*public* String getId() 


{ 


*return* id; 


} 


*public* *void* setId(String id) 


{ 


*this*.id = id; 


} 


*public* String getLabel() 


{ 


*return* label; 


} 


*public* *void* setLabel(String label) 


{ 


*this*.label = label; 


} 


*public* String getType() 


{ 


*return* type; 


} 


*public* *void* setType(String type) 


{ 


*this*.type = type; 


} 


 


 


}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 1 2008
Added on Oct 3 2008
19 comments
407 views