0

Functional interface defines method MyFunc that takes 2 argument, But when this method is implemented in HighTemp class, it only has 1 argument for some reason, and it is called using f.func(vals[i], v), how this function calls this method boolean sameTemp(HighTemp ht2), what am I missing?

interface MyFunc<T> {
    boolean func(T v1, T v2);
}

class HighTemp {
    private int hTemp;

    HighTemp(int ht) { hTemp = ht; }

    boolean sameTemp(HighTemp ht2) {
        return hTemp == ht2.hTemp;
    }

    boolean lessThanTemp(HighTemp ht2) {
        return hTemp < ht2.hTemp;
    }
}

public class InstanceMethObjectRefDemo {
    static<T> int Counter(T[] vals, MyFunc<T> f, T v) {
        int count = 0;

        for (int i = 0; i < vals.length; i++)
            if (f.func(vals[i], v)) count++;

        return count;
    }

    public static void main(String[] args) {
        int count;

        HighTemp[] weekDayHighs = {
                                    new HighTemp(89), new HighTemp(82),
                                    new HighTemp(90), new HighTemp(89),
                                    new HighTemp(89), new HighTemp(91),
                                    new HighTemp(84), new HighTemp(83)
                                    };

        count = Counter(weekDayHighs, HighTemp::sameTemp, new HighTemp(89));
        System.out.println(count + " days had a high of 89");

        HighTemp[] weekDayHighs2 = {
                                    new HighTemp(32), new HighTemp(12),
                                    new HighTemp(24), new HighTemp(19),
                                    new HighTemp(18), new HighTemp(12),
                                    new HighTemp(-1), new HighTemp(13)
                                    };

        count = Counter(weekDayHighs2, HighTemp::sameTemp, new HighTemp(12));
        System.out.println(count + " days had a high of 12");

        count = Counter(weekDayHighs, HighTemp::lessThanTemp, new HighTemp(89));
        System.out.println(count + " days had a high less than 89");

        count = Counter(weekDayHighs2, HighTemp::lessThanTemp, new HighTemp(19));
        System.out.println(count + " days had a high of less then 19");
    }
}
Invisible
  • 112
  • 1
  • 2
  • 13
  • 2
    Sorry, I'm not entirely clear what you're asking. Can you please rephrase? – Joe C Mar 19 '17 at 20:42
  • yes but 'boolean sameTemp(HighTemp ht2)' takes 1 argument, how is it correct to call this method with 2 arguments – Invisible Mar 19 '17 at 20:45
  • Maybe asking why the method reference works in the constructor? – pvg Mar 19 '17 at 20:45
  • Can you edit your code to capitalize `counter` because that's probably making it harder for people to read than you might think. And be specific about which lines/expressions you think are problematic – pvg Mar 19 '17 at 20:46
  • I am asking how 'InstanceMethObjectRefDemo.counter' calls 'boolean sameTemp' method, with 2 argument, when 'sameTemp' takes only 1 argument – Invisible Mar 19 '17 at 20:47
  • 1
    Possible duplicate of [What's the difference between instance method reference types in Java 8?](http://stackoverflow.com/questions/22516331/whats-the-difference-between-instance-method-reference-types-in-java-8) – john16384 Mar 19 '17 at 20:47
  • Read the dupe. An instance of `HighTemp` is the implied argument you're missing. – pvg Mar 19 '17 at 20:51

1 Answers1

2

HighTemp::sameTemp is equivalent to:

(HighTemp t1, HighTemp t2) -> t1.sameTemp(t2);
john16384
  • 7,800
  • 2
  • 30
  • 44