Java8 Stream()引发的“non-static method cannot be referenced from a static context”

问题描述: 使用Java8 Stream对数组进行排序

list.stream().sorted(Comparator.comparing(SeriesData::getName));
复制代码

其中sorted()函数需要传入一个Comparator,而我们使用了Comparator中的静态方法comparing()构建了一个Comparator。在构建Comparator的时候comparing()需要传入一个函数的引用作为参数,也就是SeriesData::getName

SeriesData类如下:

public class SeriesData {
    private Object name;
    
    public void setName(Object name) {
        this.name = name;
    }
    
    public Object getName() {
        return name;
    }
}
复制代码

这时出现两个错误:

Error:(343, 86) java: no suitable method found for comparing(SeriesData::getName)
    method java.util.Comparator.<T,U>comparing(java.util.function.Function<? super T,? extends U>,java.util.Comparator<? super U>) is not applicable
      (cannot infer type-variable(s) T,U
        (actual and formal argument lists differ in length))
    method java.util.Comparator.<T,U>comparing(java.util.function.Function<? super T,? extends U>) is not applicable
      (inferred type does not conform to upper bound(s)
        inferred: java.lang.Object
        upper bound(s): java.lang.Comparable<? super java.lang.Object>,java.lang.Object)
复制代码
Error:(343, 97) java: invalid method reference
  non-static method getName() cannot be referenced from a static context
复制代码

Q1

第一个错误就是说,向comparing()方法中传入的参数类型是错的。为什么是错的呢? 首先先来看看comparing()这个函数的实现:

public static <T, U extends Comparable<? super U>> Comparator<T> comparing(
        Function<? super T, ? extends U> keyExtractor) {
    Objects.requireNonNull(keyExtractor);
    return (Comparator<T> & Serializable)
        (c1, c2) -> keyExtractor.apply(c1).compareTo(keyExtractor.apply(c2));
}
复制代码

第一眼:WTF!,这是什么鬼,根本看不懂啊,那就赶紧好好补补课!

  1. 首先这是一个静态方法,其次,这是泛型方法。

  2. static后面的<T, U extends Comparable<? super U>>,这是泛型方法的泛型参数声明部分,意思是:方法中有一种没有任何限制的参数类型T,另一种参数是实现了Comparable<? super U>接口的类U

  3. comparing()方法接受一个参数:Function<? super T, ? extends U> keyExtractor,这是一个函数引用,所引用的函数需要传入一个参数T,并返回一个U或他的子类。

这是要注意U是如何限定的:实现了Comparable<? super U>接口的类

而在上面的程序中的引用方法SeriesData::getName,返回的是Object,很遗憾它没有实现Comparable接口,所以也就出现了第一个错误。

Q2

第二个错误看似很好理解:“静态上下文中无法引用非静态方法无法”,getName是一个非静态的方法,而comparing()则是静态的,这导致问题无可厚非。But,很怪异的是,当我把getName的返回值改为String后就不会出现上述的问题了,找了好久没找到原因。

另外,也尝试了其他例子,只要是在静态方式中使用非静态方法都会出现“non-static method cannot be referenced”这个错误。

public static Integer fun2(String a) {
	System.out.println(a);
	return Integer.valueOf(a);
}

public static void fun1(Function<String, Integer> ac) {
	Stream.of("1", "2", "4").map(x -> ac.apply("1"));
}

public static void main(String[] args) {
	fun1(StorageAreaAction::fun2);
}
复制代码

如果把fun2()中的static去掉则会报错。

猜测: 难道是因为setter方法的原因?

如果有哪位大神知道的话还请不吝赐教。

Continuing...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这个错误通常发生在尝试从静态上下文中引用非静态方法时。根据引用\[1\]和引用\[2\]的描述,这个错误可能是由于在静态方法中使用了非静态方法导致的。在使用Lambda表达式构建查询参数时,确保你在正确的上下文中使用了正确的方法。引用\[3\]提到,泛型没有正确指定也可能导致这个错误。确保你在LambdaQueryWrapper中正确指定了泛型类型。引用\[4\]提供了一个解决方案,即确保在LambdaQueryWrapper中指定了正确的泛型类型。引用\[5\]中的错误示例也提供了一个可能导致这个错误的情况,即在静态上下文中引用了非静态方法。请检查你的代码,确保你没有在静态上下文中引用非静态方法。 #### 引用[.reference_title] - *1* *4* [Labmda表达式 Non-static method cannot be referenced from a static context 解决办法](https://blog.csdn.net/luck_sheng/article/details/129311230)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [MyBatisPlus Non-static method cannot be referenced from a static context](https://blog.csdn.net/Vampire69/article/details/129371624)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *5* [Java8 Stream.concat() non-static method cannot be referenced from a static context](https://blog.csdn.net/phil_jing/article/details/126152496)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值