考虑下面的代码,
interface TestInter {
public void abc();
}
class DemoStatic {
public static void testStatic(String abc) {
System.out.println(abc);
}
public void runTest () {
// Using lambda expression.
String str = "demo string" ;
TestInter demo1 = () -> DemoStatic.testStatic(str);
demo1.abc();
// Using method reference.
TestInter demo2 = DemoStatic::testStatic; // This line is not compiling.
demo2.abc();
}
}
Run Code Online (Sandbox Code Playgroud)
如果这样的答案中描述的方法的参数将被消除,我们可以调用该testStatic方法作为TestInter接口实体的主体.abc()testStaic()
但在这种情况下,我们如何为参数化方法编写方法参考testStatic?
我在我们的项目中遇到了以下代码:
MyInterface var = MyClass::new;
Run Code Online (Sandbox Code Playgroud)
有没有区别
MyInterface var = new MyClass();
Run Code Online (Sandbox Code Playgroud)
懒?
java constructor variable-assignment java-8 method-reference
使用Guava可以通过这种方式确保升序排序:
import com.google.common.collect.Ordering;
import io.predictor.dao.ohlcv.OhlcvHm;
import static java.util.stream.Collectors.toList;
assertThat("Ordered by age", Ordering.natural().isOrdered(
employees.stream().map(Employee::getAge).collect(toList())));
Run Code Online (Sandbox Code Playgroud)
对我来说,Guava(因为它与Java lambdas密切相关)并不能为这种情况提供简单的解决方案.当然,我可以写一些帮助方法并包装它,但也许有人已经在库中完成了它.有一种最简单的方法吗?就像是:
XLibrary.isOrdered(employees, Employee::getAge);
Run Code Online (Sandbox Code Playgroud) 请帮助我了解如何使用以下方法的方法参考替换lambda.
public List<Person> sortByStartDate_ASC(LinkedHashSet<Person> personList) {
List<Person> pList = new ArrayList<Person>(personList);
Collections.sort(pList, (Person person1, Person person2) -> person1
.getStartDate().compareTo(person2.getStartDate()));
return pList;
}
Run Code Online (Sandbox Code Playgroud) 如果您只有一个Class对象,一个人如何获得方法引用到的方法,如toString?稍后我们将有这个特定类的实例,我们将通过方法引用调用此方法.
例如,考虑Java枚举,它的子类Enum.这里T定义为<T extends Enum>.
Class c = MyEnum.class
…
Function< T , String> f = c :: toString ;
Run Code Online (Sandbox Code Playgroud)
我收到一条错误,说"无效的方法参考".
我有一个函数列表,所有这些函数都扩展了Consumer接口.我需要提取出函数的索引.这样做的原因是我正在实现循环工作流,其中用户按顺序调用列表中的函数.因此,当用户使用foo完成时,foo调用一个调度程序函数,该函数在列表中的foo之后调度该函数.为此,foo需要知道列表中自己的索引.我不想硬编码数字,这就是我试图检索foo索引的原因.这就是我所拥有的:
import com.google.common.collect.ImmutableList;
import java.util.List;
import java.util.function.Consumer;
public class Experiment {
private List<Consumer<String>> activities = ImmutableList.of(
this::bar,
this::foo
);
public void bar(String x) {
System.out.println();
}
public void foo(String x) {
System.out.println();
}
public void print() {
System.out.println(activities.indexOf((Consumer<String>)this::foo));
}
public static void main(String []args) {
Experiment e = new Experiment();
e.print();
}
}
Run Code Online (Sandbox Code Playgroud)
这打印-1给我,所以它无法在列表中找到它.有没有办法从这个列表中提取出函数的索引?
是否有任何Java 8 API静态方法Function在非null输入上运行,但nullValue在null输入上返回?
我自己可以很容易地编写这个方法,但如果它存在,我宁愿使用标准方法.
public static <T, R> R transform(final T t, final Function<T, R> rFromT, final R nullValue) {
return
t == null
? nullValue
: rFromT.apply(t)
;
}
// which can be called like:
final Number x = getNumberThatCouldBeNull();
final long y = transform(x, Number::longValue, 0L);
Run Code Online (Sandbox Code Playgroud) 我有一段代码,我一直在努力理解方法参考.
private static String s;
public static void main(String[] args) {
// TODO Auto-generated method stub
LambdaTut lamTut = new LambdaTut();
Function<String, Integer> lenghthFunction = (a) -> a.length();
lamTut.funtionTut(LambdaTut::stringLength);
}
public int stringLength() {
System.out.println(s);
return s.length();
}
public <T, S> void funtionTut(Function<T, S> function) {
Function<T, String> sqFunction = function.andThen(a -> "Length:" + a);
System.out.println(sqFunction.compose((a) -> (T) ("Name:" + a)).apply("Demo"));
}
Run Code Online (Sandbox Code Playgroud)
当我使用时Lambdatut::stringLength,我得到了一个类别转换异常,因为String::length工作正常.我在这里混淆了length()函数如何替换函数接口中的方法S apply(T obj)
如果我使用lamTut::stringLength,我得到一个编译时异常:
LambdaTut类型中的方法funtionTut(Function)不适用于参数(lamTut :: stringLength)
我正在学习Java 8中的Lambda表达式和方法引用,并且看到我们可以通过使用'super'来引用方法的超类版本,如下所示:
超级::名
但是,当我这样做时,它不起作用.以下是示例代码:
interface MyInterface {
int someFunc(int x);
}
class A {
static int Func(int y) {
// return (int) (Math.random() * y);
return y;
}
}
class B extends A {
static int Func(int y) {
// return (int) (Math.random() * y);
return y + 1;
}
}
public class Test {
public static void main(String[] args) {
System.out.print("Enter a number: ");
java.util.Scanner scanner = new java.util.Scanner(System.in);
int result = funcOp(B::Func, scanner.nextInt()); // <-This works.
//int result …Run Code Online (Sandbox Code Playgroud) 这有效
Supplier<Double> random1 = () -> Math.random();
Run Code Online (Sandbox Code Playgroud)
为什么这样不起作用:
Supplier<Double> random2 = () -> Math::random
Run Code Online (Sandbox Code Playgroud) java-8 ×10
method-reference ×10
java ×9
lambda ×6
collections ×1
constructor ×1
enums ×1
guava ×1