0

While trying to sort a list of WlMatch Type as in below code, I am seeing the exception:

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

[  
    {  
        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
    }
]
Pavel Smirnov
  • 4,611
  • 3
  • 18
  • 28
  • 3
    Your List doesn't actually contain WlMatch instances. It contains LinkedHashMap instances. That's what the error means. Read the documentation of your JSON parser to make it create the objects you want, or transform the maps to WiMatches by yourself before sorting them. – JB Nizet Apr 07 '19 at 21:14

1 Answers1

0

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();
          }
        });
JosemyAB
  • 387
  • 3
  • 9
  • Note that using rawtypes in newer Java code is not recommended, as it suppresses helpful compiler errors, HashMap requires 2 parameters. – Ferrybig Apr 08 '19 at 06:33