Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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
  }

}
like image 554
Alex Avatar asked Jun 09 '17 07:06

Alex


People also ask

How do I return Comparator?

It returns a positive value if obj1 is greater than obj2. Otherwise, a negative value is returned. By overriding compare( ), you can alter the way that objects are ordered. For example, to sort in a reverse order, you can create a comparator that reverses the outcome of a comparison.

How to override Comparator Java?

There are certain rules and important points to remember while overriding compareTo method: 1) CompareTo method must return negative number if current object is less than other object, positive number if current object is greater than other object and zero if both objects are equal to each other.

What is the return type of Comparator in Java?

Java Comparator Interface Definition The compare() method returns an int which signals which of the two objects was larger. The semantics of the return values are: A negative value means that the first object was smaller than second object. The value 0 means the two objects are equal.

Why Comparator has equals method?

Now when the javadocs talk about comparisons being "consistent with equals", then "equals" in this context always refers to the equals(Object) method of the object to be compared, may that object implement Comparable itself or just be compared to another object with a Comparator .

What is the return value of comparator method in Java?

The returned comparator by this method is serializable and throws NullPointerException when comparing null. Parameters: This method accepts nothing. Return value: This method returns a comparator that imposes the reverse natural ordering on Comparable objects.

How to compare two objects using Comparator interface in Java?

The Comparator interface defines two methods: compare ( ) and equals ( ). The compare ( ) method, shown here, compares two elements for order − obj1 and obj2 are the objects to be compared. This method returns zero if the objects are equal. It returns a positive value if obj1 is greater than obj2. Otherwise, a negative value is returned.

What is comparator reverseorder () in Java?

Comparator reverseOrder () method in Java with examples Last Updated : 29 Apr, 2019 The reverseOrder () method of Comparator Interface in Java returns a comparator that use to compare Comparable objects in reverse of natural order. The returned comparator by this method is serializable and throws NullPointerException when comparing null.

How to impose custom ordering of items in a comparator?

The custom ordering of items is imposed by implementing Comparator’s compare () method in the objects. 1. When to Use Comparator Interface 2. Overriding compare () Method


1 Answers

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.

like image 132
Ronan Dhellemmes Avatar answered Oct 20 '22 22:10

Ronan Dhellemmes