java.lang.ClassCastException: java.util.LinkedHashMap не может быть приведен к java.lang.Comparable исключение при попытке сортировки списка - PullRequest
0 голосов
/ 07 апреля 2019

При попытке отсортировать список типа 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
    }
]

1 Ответ

0 голосов
/ 08 апреля 2019

Вы можете попытаться сделать это.

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