Is there a way to return comparator which wont do anything?

Alex :

I have two classes extending one parent class and there is a method which sorts data by some parameter. So for one of these two classes, I need to apply some sorting but for two others don't do anything. Is there such possibility?

public class MedicalCompositeRatesData{

  @Override
  public List<RateTableData> buildData(RateTableInputBean inputBean)
  {
    SGQuotingData sgQuotingData = inputBean.getQuotingData();

    List<ProductData> products = extractorFactory
      .getProductExtractor(inputBean.getPlanType(), inputBean.getRatingType())
      .extract(sgQuotingData);

    List<Row> rates = products.stream()
        .map(it -> buildRow(it, sgQuotingData))
        .sorted(getProductComparator())
        .filter(Objects::nonNull)
        .collect(Collectors.toList());

    return buildRateTables(rates);
  }

  protected Comparator<Product> getProductComparator(){
    //should leave default sorting
  }

}

public class AlternateCompositeRatesBuilder extends MedicalCompositeRatesData
{

  protected Comparator<Product> getProductComparator(){
    //will sort by rate
  }

}
Ronan Dhellemmes :

Stream.sort makes a stable sort if the stream is ordered. In other words, if two elements are equals then they'll keep their initial ordering.

Thus, as your stream is made from a List, you can simply make all elements equal :

protected Comparator<Product> getProductComparator() {
    return (a1, a2) -> 0;
}

That's not very cool-looking though but it should do the job.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=461037&siteId=1