简体   繁体   English

在Java中,如何创建每个都带有一个参数的函数指针列表?

[英]In Java, how do I create a List of function pointers which take an argument each?

I've already looked here , that works for an argument less method list containing Runnables. 我已经看过这里了 ,它适用于包含Runnables的无参数方法列表。 I need to have Consumer in my case. 我需要拥有Consumer This is what I have so far: 这是我到目前为止的内容:

import com.google.common.collect.ImmutableList;
import java.util.List;
import java.util.function.Consumer;

public class experiment {

 private static List<Consumer<String>> activities;

 public experiment (){
 activities = ImmutableList.of(
     (userId) -> this::bar,        // throws error
     (userId) -> this::foo);
 }

 public void bar(String x) {
   System.out.println(x);
 }

 public void foo(String x) {
   System.out.println(x);
 }

 public static void main(String []args) {

  for (int i=0; i<2; i++) {
    activities.get(i).accept("activity #" + i);
  }
 }
}

The error I see is void is not a functional interface . 我看到的错误是void is not a functional interface

I don't understand why I'm getting that, both bar and foo implement the accept method of the Consumer interface. 我不明白为什么会这样, barfoo实现了Consumer接口的accept方法。 Is there something I'm overlooking here? 我在这里俯瞰什么?

I believe you want to write ImmutableList.of(this::bar, this::foo) instead of ImmutableList.of((userId) -> this::bar, (userId) -> this::foo) . 我相信你想写ImmutableList.of(this::bar, this::foo)而不是ImmutableList.of((userId) -> this::bar, (userId) -> this::foo)

If you do want to use lambdas instead of method references, you should instead write ImmutableList.of((userId) -> this.bar(userId), (userId) -> this.foo(userId)) . 如果确实要使用lambda而不是方法引用,则应该编写ImmutableList.of((userId) -> this.bar(userId), (userId) -> this.foo(userId))

Your lambda's are wrong. 你的lambda错了。 You either supply a method reference, or you supply a lambda expression. 您可以提供方法参考, 可以提供lambda表达式。 You're trying an incorrect combo. 您正在尝试使用错误的组合。

Also, your two methods foo() and bar() should be static , and your instance variable should not be. 另外,您的两个方法foo()bar()应该是static ,而您的实例变量应该不是。 To show how instance methods could be called, I've added baz() and qux() . 为了展示如何调用实例方法,我添加了baz()qux()

Here's showing full example: 这里显示了完整的示例:

public class Experiment {

    private List<Consumer<String>> activities;

    public Experiment() {
        this.activities = ImmutableList.of(
            (userId) -> Experiment.bar(userId),
            Experiment::foo,
            (userId) -> this.baz(userId),
            this::qux
        );
    }

    public static void bar(String x) {
        System.out.println(x);
    }

    public static void foo(String x) {
        System.out.println(x);
    }

    public void baz(String x) {
        System.out.println(this.hashCode() + ": " + x);
    }

    public void qux(String x) {
        System.out.println(this.hashCode() + ": " + x);
    }

    public static void main(String[] args) {
        Experiment ex = new Experiment();
        for (int i = 0; i < ex.activities.size(); i++) {
            ex.activities.get(i).accept("activity #" + i);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java Hadoop:我如何创建作为输入文件的输出器并给出一个输出,即每个文件中的行数? - Java Hadoop: How can I create mappers that take as input files and give an output which is the number of lines in each file? 指针如何在链表中工作(Java) - How do pointers work in a linked list (Java) 如何将函数作为Java中JTextField的输入? - How do I take a function as an input from a JTextField in Java? 我如何在 Java 中创建对象列表 - How do i create a list of Objects in Java 如何用指向Java指针的指针填充结构? - How do I populate a structure with pointers to pointers from Java? 如何在 Java 中创建 padString 函数? - How do I create a padString function in Java? 如何将Java函数传递给String []参数? - How do I pass a Java function a String[] argument? JAVA:为什么我需要在此函数中创建一个新的列表对象? - JAVA: Why do I need to create a new list object in this function? 我如何在java中将char作为命令行参数 - how can i take in a char as an command line argument in java 如何在Kotlin中创建对类进行操作的扩展功能? - How do I create an extension function in Kotlin, which operates on a class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM