Java – maximum flow using custom comparator

The following is my specially written code for using a custom comparator with Max in Java 8 stream

import java.math.BigDecimal;
import java.util.*;

public class BigDecimalMax  {

    public static BigDecimal getBigDecimalMax(List<forTest> list) {

        return list.stream()
            .filter(t -> t.id % 2 == 0)
            .max(forTestComparator::compare)  //<-- Syntax error ----------
            .orElse(null);
    }

    public static class forTestComparator implements Comparator<forTest> {

        @Override
        public int compare(forTest val1,forTest val2) {
            return val1.value.compareTo(val2.value);
        }
    }

    public static void main(String[] args) {

        List<forTest> lst = new ArrayList<>();
        Random rn = new Random();
        BigDecimalMax bdm = new BigDecimalMax();

        for (int i=1; i<22; ++i) {
            lst.add(bdm.new forTest(i,BigDecimal.valueOf(rn.nextLong())));
        }

        System.out.println(getBigDecimalMax(lst));

    }

    class forTest {
        public int id;
        public BigDecimal value;

        forTest(int id,BigDecimal value) {
            this.id = id;
            this.value = value;
        }

        @Override
        public String toString() {
            return "forTest{" +
                    "id=" + id +
                    ",value=" + value +
                    '}';
        }
    }
}

I encountered a syntax error in the method reference. I don't understand

Error:(15,18) java: incompatible types: invalid method reference
    cannot find symbol
      symbol:   method compare(BigDecimalMax.forTest,BigDecimalMax.forTest)
      location: class BigDecimalMax.forTestComparator

IntelliJ idea complains that non - static methods cannot be referenced from static context

What's wrong with me?

Additional notes (24 April 2014):

>I now understand the causes of grammatical errors Thank you. > Do you really need a custom comparator here?

Since BigDecimal implements comparable but does not seem to implement comparator (it has compareto() but does not have compare()), I think it is necessary to customize comparator That's why I can't just use comparator comparing(ft – > ft.value). Is there a flaw in my logic?

Solution

Sotirios delimanolis' answer shows how to solve the problem, but I have something to add

If you already have a class that implements comparator, you do not need to use a method reference to its compare () method You can pass its instance directly because max () accepts a reference to the comparator:

.max(new forTestComparator())

or

forTestComparator instance = new forTestComparator();
    ...
    .max(instance)

However, composite functions on comparator usually do not need to have a class that implements comparator For example, you can completely get rid of the fortestcomparator class by doing the following:

.max(Comparator.comparing(ft -> ft.value))

Alternatively, if fortest has an obvious getValue () method, you can override the stream max() call, as shown below:

.max(Comparator.comparing(forTest::getValue))

In addition, if you want fortest to implement the comparable interface, you can do this:

public class forTest implements Comparable<forTest> {
    @Override
    public int compareTo(forTest other) {
        return this.value.compareTo(other.value);
    }
    ...
}

And the method of using max() on comparable is:

.max(Comparator.naturalOrder())

Two styles of notes:

>I strongly oppose the use of orelse (null) on the instance of optional This is allowed, although its main purpose may be to improve the new Java 8 API to code that expects null to represent missing values If possible, avoid using orelse (null) because it forces the caller to check for null Instead, replace the absent value with the actual value, or return the option itself to the caller so that the caller can apply any policy it wants. > I recommend sticking to the established Java Naming Convention for uppercase mixed case class names The class names of fortest and fortestcomparator make these codes difficult to use because they don't look like class names

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>