我很难用比较器来编写自定义对象列表,并使用以下字符串字段: 1.分支类 - >可以是州,县或邮政编码,用于识别它是哪个数据字段;不可能是空的 2.状态 - >州名,不能为空 3.地理 - >如果分支class.equals' county',它将保存县名,可以为空 4. zip - >邮政编码。州和县可以是空的 5. ParentNodeId - >应该适用于此树的父名称。例如,一个州没有父(空字符串),而县的状态为parentNodeId,而zipCode的县是parentNodeId。因此,对于具有branch =" state",zipCode" 36003"的地理对象,parentNodeId应为Autauga,状态为" AL"。对于具有分支="县"的地理对象;和地理=" Autauga",然后parentNodeId是" AL"。
当前对象列表的格式为:州 - 州 - 州 - 县 - 县 - 邮政编码 - 邮政编码
虽然我希望列出
-state
- 县
- 邮政编码
- 邮政编码
- 县
- 邮政编码
-State
- 县
等等。
我的审判仍然缺少我不知道的案例。这是我的代码
public static final Comparator<Geography> BY_STATE_COUNTY_ZIP_COMPARATOR = new Comparator<Geography>() {
public int compare(final Geography obj1, final Geography obj2) {
if (obj1.getZip().equals("89420") || obj2.getZip().equals("89420") || obj1.getGeography().equals("Mono")
|| obj2.getGeography().equals("Mono")) {
System.out.println("hdfhd");
}
if (obj1.getBranchClass().equalsIgnoreCase(obj2.getBranchClass())) {
return this.similarBranchComparison(obj1, obj2);
}
else {
// Different branches
final int x = this.differentBranchesComparison(obj1, obj2);
return x;
}
}
private int differentBranchesComparison(final Geography obj1,
final Geography obj2) {
if ((obj1.getZip().equals("89420") && obj1.getParentNodeId().equals("Mono"))
|| ((obj2.getZip().equals("89420") && obj2.getParentNodeId().equals("Mono")))) {
System.out.println("hdfhd");
}
// Same states - Obj1 is state
if (obj1.getBranchClass().equalsIgnoreCase(ASDGeographyBasedDashboardVO.STATE_LEVEL)
&& obj1.getState().equalsIgnoreCase(obj2.getState())) {
// obj2 should be greater
return -1;
}
// Same states - Obj2 is state
else if (obj2.getBranchClass().equalsIgnoreCase(ASDGeographyBasedDashboardVO.STATE_LEVEL)
&& obj1.getState().equalsIgnoreCase(obj2.getState())) {
// obj1 should be greater
return 1;
}
// Different states - obj1 OR Obj2 is state
else if (((obj1.getBranchClass().equalsIgnoreCase(ASDGeographyBasedDashboardVO.STATE_LEVEL)) || (obj2
.getBranchClass().equalsIgnoreCase(ASDGeographyBasedDashboardVO.STATE_LEVEL)))
&& !(obj1.getState().equalsIgnoreCase(obj2.getState()))) {
// Delegate to state comparison
return new CompareToBuilder().append(obj1.getState(), obj2.getState()).toComparison();
}
// Same states - Same counties (County - Zip)
else if (obj1.getBranchClass().equalsIgnoreCase(ASDGeographyBasedDashboardVO.COUNTY_LEVEL)
&& ((obj1.getState().equalsIgnoreCase(obj2.getState())) && (obj1.getGeography()
.equalsIgnoreCase(obj2.getParentNodeId())))) {
// obj2 (zip) should be greater
return -1;
}
// Same states - Same counties (Zip - County)
else if (obj2.getBranchClass().equalsIgnoreCase(ASDGeographyBasedDashboardVO.COUNTY_LEVEL)
&& ((obj1.getState().equalsIgnoreCase(obj2.getState())) && (obj1.getParentNodeId()
.equalsIgnoreCase(obj2.getGeography())))) {
// obj1 should be greater
return 1;
}
// Same states different counties (County - zip)
else if ((obj1.getBranchClass().equalsIgnoreCase(ASDGeographyBasedDashboardVO.COUNTY_LEVEL))
&& (obj1.getState().equalsIgnoreCase(obj2.getState()) && !(obj1.getGeography()
.equalsIgnoreCase(obj2.getParentNodeId())))) {
return new CompareToBuilder().append(obj1.getGeography(), obj2.getParentNodeId()).toComparison();
}
// Same states different counties (Zip - County)
else if ((obj2.getBranchClass().equalsIgnoreCase(ASDGeographyBasedDashboardVO.COUNTY_LEVEL))
&& (obj1.getState().equalsIgnoreCase(obj2.getState()) && !(obj1.getParentNodeId()
.equalsIgnoreCase(obj2.getGeography())))) {
return new CompareToBuilder().append(obj1.getParentNodeId(), obj2.getGeography()).toComparison();
}
// Different States
else if (((obj1.getBranchClass().equalsIgnoreCase(ASDGeographyBasedDashboardVO.COUNTY_LEVEL)) || (obj2
.getBranchClass().equalsIgnoreCase(ASDGeographyBasedDashboardVO.COUNTY_LEVEL)))
&& !(obj1.getState().equalsIgnoreCase(obj2.getState()))) {
return new CompareToBuilder().append(obj1.getState(), obj2.getState()).toComparison();
}
return 0;
}
private int similarBranchComparison(final Geography obj1,
final Geography obj2) {
// State-State, County - County, Zip-Zip
// State-State
if (obj1.getBranchClass().equalsIgnoreCase(ASDGeographyBasedDashboardVO.STATE_LEVEL)) {
return new CompareToBuilder().append(obj1.getState(), obj2.getState()).toComparison();
}
// County - County
else if (obj1.getBranchClass().equalsIgnoreCase(ASDGeographyBasedDashboardVO.COUNTY_LEVEL)) {
if (obj1.getState().equalsIgnoreCase(obj2.getState())) {
// Compare Counties within the same state
return new CompareToBuilder().append(obj1.getGeography(), obj2.getGeography()).toComparison();
}
else {
// Compare Counties within different states
return new CompareToBuilder().append(obj1.getState(), obj2.getState()).toComparison();
}
}
else {
// Zip - Zip
if (obj1.getState().equalsIgnoreCase(obj2.getState())) {
if (obj1.getParentNodeId().equalsIgnoreCase(obj2.getParentNodeId())) {
return new CompareToBuilder().append(obj1.getZip(), obj2.getZip()).toComparison();
}
else {
return new CompareToBuilder().append(obj1.getParentNodeId(), obj2.getParentNodeId())
.toComparison();
}
}
else {
// Compare Zip codes within different states
return new CompareToBuilder().append(obj1.getState(), obj2.getState()).toComparison();
}
}
}
};
答案 0 :(得分:1)
这应该是一个简单的比较器,你首先比较state,然后是county,然后是zip。因此,假设您可以编写getState(),getCounty()和getZip()方法,并且可以编写州,县和zip的比较。如果你不能从所有不同类型获得状态,那么你无法比较它们。
这是一些伪代码。
Comparator<Geography> comparator = new Comparator<>() {
public int compare(final Geography obj1, final Geography obj2) {
state1 = getState(obj1);
state2 = getState(obj2);
int retCode = state1.compare(state2);
if (retCode != 0)
return retCode;
county1 = getCounty(obj1);
county2 = getCounty(obj2);
retCode = county1.compare(county2);
if(retCode != 0)
return retCode;
zip1 = getZip(obj1);
zip2 = getZip(obj2);
retCode = zip1.compare(zip2);
return retCode;
}
}