[英]java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to java.lang.Comparable exception when trying sort a List
While trying to sort a list of WlMatch Type as in below code, I am seeing the exception: 在尝试按下面的代码对WlMatch类型列表进行排序时,我看到了异常:
java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to java.lang.Comparable
I tried using both comparable and comparator sort but resulted in same error. 我尝试使用可比较和比较器排序,但导致相同的错误。 Can some one with more expertise guide where I would be going wrong. 一些有更多专业知识的人可以指导我出错的地方。
ReadContext readContext = JsonPath.parse(responseJson);
List<WlMatch> wlMatchs = readContext.read("$.response.watchList.searchResult.records.resultRecord[0].watchlist.matches.wlmatch");
if (wlMatchs != null) {
// wlMatchs.sort(Comparator.comparingInt(WlMatch::id));
wlMatchs.sort(new Comparator<WlMatch>() {
@Override
public int compare(WlMatch w1, WlMatch w2) {
if (w1.getId() == w2.getId()) {
return 0;
}
return w1.getId() - w2.getId();
}
});
In case if you want to see what is in list. 如果你想查看列表中的内容。 wlMatchs value in debug mode is 调试模式中的wlMatchs值是
[
{
addresses=address,
bestName=null,
reasonListed=null,
countryDetails=country,
matchReAlert=null,
phones=null,
resultDate=0,
acceptListID=acceptlist,
bestNameScore=0,
error=null,
bestCountry=null,
trueMatch=null,
doBs=null,
file={
published=3123123213,
build=111,
name=file1,
id=456,
type=txt,
custom=true
},
entityDetails={
akAs=null,
addresses={
entityAddress=[
{
stateProvinceDistrict=null,
country=India,
comments=null,
city=Hyderabad,
postalCode=500001,
street1=null,
id=0,
street2=null,
type=null
}
]
},
dateListed=null,
comments=null,
gender=MALE,
listReferenceNumber=null,
reasonListed=reason,
entityType=IND,
additionalInfo={
entityAdditionalInfo=[
{
comments=null,
id=0,
type=DOB,
value=12-12-1989
}
]
},
name=null,
iDs=null,
phones=null
},
entityName=In,
falsePositive=null,
gatewayOFACScreeningIndicatorMatch=null,
previousResultID=null,
conflicts=null,
iDs=null,
entityScore=0,
id=456,
addedToAcceptList=true,
matchXML=null,
secondaryOFACScreeningIndicatorMatch=null,
entityUniqueID=null,
autoFalsePositive=null,
bestCountryScore=null,
citizenships=null,
checkSum=0,
addressName=true,
ofacInfo=null,
bestAddressIsPartial=null,
bestCountryType=null
},
{
addresses=address,
bestName=null,
reasonListed=null,
countryDetails=country,
matchReAlert=null,
phones=null,
resultDate=0,
acceptListID=acceptlist,
bestNameScore=0,
error=null,
bestCountry=null,
trueMatch=null,
doBs=null,
file={
published=3123123213,
build=111,
name=file1,
id=789,
type=txt,
custom=true
},
entityDetails={
akAs=null,
addresses={
entityAddress=[
{
stateProvinceDistrict=null,
country=India,
comments=null,
city=Hyderabad,
postalCode=500001,
street1=null,
id=0,
street2=null,
type=null
}
]
},
dateListed=null,
comments=null,
gender=MALE,
listReferenceNumber=null,
reasonListed=reason,
entityType=IND,
additionalInfo={
entityAdditionalInfo=[
{
comments=null,
id=0,
type=DOB,
value=12-12-1989
}
]
},
name=null,
iDs=null,
phones=null
},
entityName=In,
falsePositive=null,
gatewayOFACScreeningIndicatorMatch=null,
previousResultID=null,
conflicts=null,
iDs=null,
entityScore=0,
id=789,
addedToAcceptList=true,
matchXML=null,
secondaryOFACScreeningIndicatorMatch=null,
entityUniqueID=null,
autoFalsePositive=null,
bestCountryScore=null,
citizenships=null,
checkSum=0,
addressName=true,
ofacInfo=null,
bestAddressIsPartial=null,
bestCountryType=null
},
{
addresses=address,
bestName=null,
reasonListed=null,
countryDetails=country,
matchReAlert=null,
phones=null,
resultDate=0,
acceptListID=acceptlist,
bestNameScore=0,
error=null,
bestCountry=null,
trueMatch=null,
doBs=null,
file={
published=3123123213,
build=111,
name=file1,
id=123,
type=txt,
custom=true
},
entityDetails={
akAs=null,
addresses={
entityAddress=[
{
stateProvinceDistrict=null,
country=India,
comments=null,
city=Hyderabad,
postalCode=500001,
street1=null,
id=0,
street2=null,
type=null
}
]
},
dateListed=null,
comments=null,
gender=MALE,
listReferenceNumber=null,
reasonListed=reason,
entityType=IND,
additionalInfo={
entityAdditionalInfo=[
{
comments=null,
id=0,
type=DOB,
value=12-12-1989
}
]
},
name=null,
iDs=null,
phones=null
},
entityName=In,
falsePositive=null,
gatewayOFACScreeningIndicatorMatch=null,
previousResultID=null,
conflicts=null,
iDs=null,
entityScore=0,
id=123,
addedToAcceptList=true,
matchXML=null,
secondaryOFACScreeningIndicatorMatch=null,
entityUniqueID=null,
autoFalsePositive=null,
bestCountryScore=null,
citizenships=null,
checkSum=0,
addressName=true,
ofacInfo=null,
bestAddressIsPartial=null,
bestCountryType=null
}
]
You can try to do this. 你可以尝试这样做。
ReadContext readContext = JsonPath.parse(responseJson);
List<HashMap> mapWlMatchs = readContext.read("$.response.watchList.searchResult.records.resultRecord[0].watchlist.matches.wlmatch");
if (mapWlMatchs != null) {
//Transform to List<WlMatch> with Java8 stream (Pseudocode. Depends on key/value objects in LinkedHashMap. Assuming you have <Object, WlMatch> key/value Map.
List<WlMatch> wlMatchs = mapWlMatchs.entrySet().stream().map(w -> {w.getValue()}).collect(Collector.toList());
// wlMatchs.sort(Comparator.comparingInt(WlMatch::id));
wlMatchs.sort(new Comparator<WlMatch>() {
@Override
public int compare(WlMatch w1, WlMatch w2) {
if (w1.getId() == w2.getId()) {
return 0;
}
return w1.getId() - w2.getId();
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.