Java8引用实例方法。VAL[i]是如何自动映射到“中”的;InstanceMethWithObjectRefDemo.counter“;

Java8引用实例方法。VAL[i]是如何自动映射到“中”的;InstanceMethWithObjectRefDemo.counter“;,java,java-8,Java,Java 8,我正在读一本java书,从中我得到了这个代码。我知道方法引用是如何产生的,但这一个让我头疼。我不知道if(f.func(vals[I],v))中的vals[I]如何充当映射函数的this // Use an instance method reference with different objects. // A functional interface that takes two reference arguments // and returns a boolean result.

我正在读一本java书,从中我得到了这个代码。我知道方法引用是如何产生的,但这一个让我头疼。我不知道
if(f.func(vals[I],v))
中的
vals[I]
如何充当映射函数的
this

// Use an instance method reference with different objects. 
// A functional interface that takes two reference arguments 
// and returns a boolean result.
interface MyFunc<T> { 
    boolean func(T v1, T v2); 
}
// A class that stores the temperature high for a day. 
class HighTemp { 
    private int hTemp; 
    HighTemp(int ht) {
    hTemp = ht;
} 

// Return true if the invoking HighTemp object has the same 
// temperature as ht2.
boolean sameTemp(HighTemp ht2) { 
    return hTemp == ht2.hTemp; 
}
// Return true if the invoking HighTemp object has a temperature 
// that is less than ht2. 
boolean lessThanTemp(HighTemp ht2) { 
    return hTemp < ht2.hTemp; 
} 
} 
class InstanceMethWithObjectRefDemo { 
// A method that returns the number of occurences 
// of an object for which some criteria, as specified by 
// the MyFunc parameter, is true. 
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;
    // Create an array of HighTemp objects. 
    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) }; 

    // Use counter() with arrays of the class HighTemp. 
    // Notice that a reference to the instance method 
    // sameTemp() is passed as the second argument. 
    count = counter(weekDayHighs, HighTemp::sameTemp,new HighTemp(89)); 
    System.out.println(count + " days had a high of 89"); 

    // Now, create and use another array of HighTemp objects. 
    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"); 
    // Now, use lessThanTemp() to find days when temperature was less 
    // that a specified value.
    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 than 19"); 
    } 
}
//对不同的对象使用实例方法引用。
//接受两个引用参数的函数接口
//并返回一个布尔结果。
接口MyFunc{
布尔函数(T v1,T v2);
}
//一个可以储存一天高温的班级。
类高温{
私有int hTemp;
高温(内部高温){
hTemp=ht;
} 
//如果调用的HighTemp对象具有相同的
//温度为ht2。
布尔sameTemp(高温ht2){
返回hTemp==ht2.hTemp;
}
//如果调用的HighTemp对象具有温度,则返回true
//这比ht2小。
布尔lessThanTemp(高温ht2){
返回hTemp
在此上下文中,
高温::sameTemp
相当于
(t1,t2)->t1.sameTemp(t2)

在中,它被称为对特定类型的任意对象的实例方法的引用

以下是对特定类型的任意对象的实例方法的引用示例:

String[]stringArray={“Barbara”、“James”、“Mary”、“John”, “帕特里夏”、“罗伯特”、“迈克尔”、“琳达”}; sort(stringArray,String::CompareTignoreCase)

方法引用String::compareToIgnoreCase的等效lambda表达式将具有形式参数列表(字符串a、字符串b),其中a和b是用于更好地描述此示例的任意名称。方法引用将调用方法a.compareToIgnoreCase(b)


查看呼叫:
新高温(89)
。更正。在此上下文中,HighTemp::sameTemp相当于(t1,t2)->t1.sameTemp(t2)