java.lang.ClassCastException:尝试排序List时,java.util.LinkedHashMap无法转换为java.lang.Comparable异常

问题描述 投票:0回答:1

在尝试按下面的代码对WlMatch类型列表进行排序时,我看到了异常:

java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to java.lang.Comparable

我尝试使用可比较和比较器排序,但导致相同的错误。一些有更多专业知识的人可以指导我出错的地方。

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();
          }
        });

如果你想查看列表中的内容。调试模式中的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
    }
]
java comparator classcastexception comparable linkedhashmap
1个回答
0
投票

你可以尝试这样做。

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();
          }
        });
© www.soinside.com 2019 - 2024. All rights reserved.