method-reference

Utility Class to bootstrap lambda expressions or method references for method chaining?

天大地大妈咪最大 提交于 2020-01-14 02:59:13
问题 With the functional interfaces introduced in Java 8, you can easily chain different expressions into one new expression, illustrated by the code snippet below. public class PredicateChaining { public static void main(String[] args) { // verbose, but standard JDK functionality only Predicate<String> allUpperCase = StringUtils::isAllUpperCase; Predicate<String> longerThan5 = s -> s.length() > 5; if (allUpperCase.and(longerThan5).test("PREDICATE")) { System.out.println("'PREDICATE' is a

Reference to methods with different parameters in Java8

∥☆過路亽.° 提交于 2020-01-10 01:13:28
问题 I'm wondering how does all this stuff with method references and functional interfaces works on lower level. The easiest example is where we have some List List<String> list = new ArrayList<>(); list.add("b"); list.add("a"); list.add("c"): Now we want to sort it with Collections class, so we can call: Collections.sort(list, String::compareToIgnoreCase); But if we define custom comparator it could be something like: Comparator<String> customComp = new MyCustomOrderComparator<>(); Collections

Why 'T.super.toString()' and 'super::toString' use a synthetic accessor method?

让人想犯罪 __ 提交于 2020-01-02 01:04:44
问题 Consider the following set of expressions: class T {{ /*1*/ super.toString(); // direct /*2*/ T.super.toString(); // synthetic Supplier<?> s; /*3*/ s = super::toString; // synthetic /*4*/ s = T.super::toString; // synthetic }} Which gives the following result: class T { T(); 0 aload_0 [this] 1 invokespecial java.lang.Object() [8] 4 aload_0 [this] 5 invokespecial java.lang.Object.toString() : java.lang.String [10] 8 pop // ^-- direct 9 aload_0 [this] 10 invokestatic T.access$0(T) : java.lang

Java 8 reference to a static method vs. instance method

半城伤御伤魂 提交于 2020-01-01 08:47:33
问题 say I have the following code public class A { int x; public boolean is() {return x%2==0;} public static boolean is (A a) {return !a.is();} } and in another class... List<A> a = ... a.stream().filter(b->b.isCool()); a.stream().filter(A::is); //would be equivalent if the static method is(A a) did not exist the question is how do I refer to the instance method version using the A::is type notation? Thanks a lot 回答1: In your example, both the static and the non-static method are applicable for

Syntax for specifying a method reference to a generic method

馋奶兔 提交于 2019-12-30 17:21:19
问题 I read the following code in "Java - The beginner's guide" interface SomeTest <T> { boolean test(T n, T m); } class MyClass { static <T> boolean myGenMeth(T x, T y) { boolean result = false; // ... return result; } } The following statement is valid SomeTest <Integer> mRef = MyClass :: <Integer> myGenMeth; Two points were made regarding the explanation of the above code 1 - When a generic method is specified as a method reference, its type argument comes after the :: and before the method

How does Java 8 know which String::compareTo method reference to use when sorting?

↘锁芯ラ 提交于 2019-12-30 04:31:04
问题 How does Java know which String::compareTo method reference to use when calling Collections.sort(someListOfStrings, String::compareTo); ? compareTo is not static and it needs to know the value of the "left hand side" of the comparison. 回答1: Suppose that you use method reference for Comparator interface: Comparator<String> cmp = String::compareTo; When you call the cmp.compare(left, right) (which is "single abstract method" or "SAM" of Comparator interface), the magic occurs: int result = cmp

Please Explain Java 8 Method Reference to instance Method using class name

☆樱花仙子☆ 提交于 2019-12-28 06:33:12
问题 public interface MyFunc<T> { boolean func(T v1, T v2); } public class HighTemp { private int hTemp; HighTemp(){ } public HighTemp(int ht) { this.hTemp = ht; } boolean sameTemp(HighTemp ht2){ return hTemp == ht2.hTemp; } boolean lessThanTemp(HighTemp ht2){ return hTemp < ht2.hTemp; } } class InstMethWithObjRef { 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

Is it possible (how) to get the name of a method reference at Runtime Java? [duplicate]

孤人 提交于 2019-12-23 19:12:53
问题 This question already has answers here : Printing debug info on errors with java 8 lambda expressions (1 answer) Reflection type inference on Java 8 Lambdas (5 answers) How to get the MethodInfo of a Java 8 method reference? (10 answers) Closed last year . I've been using a lot of method references and lambdas recently, and wanted to know at runtime if i could print to screen the source of the lambda ie its name, simply for debugging reasons. I figured it might be possible using reflection,

Method References like in Java 8 in Scala

ぐ巨炮叔叔 提交于 2019-12-23 10:48:08
问题 In this Java class: import java.util.function.*; public class T { public String func(String a) { System.out.println("There we go: " + a); return a; } public static void main(String... args) { final Supplier<T> c = T::new; final BiFunction<T, String, String> f = T::func; final T t = c.get(); final String v = f.apply(t, "something"); System.out.println(v); } } I can get a method reference to the constructor of T and to the instance method func . Is there a way to do the same in scala, i.e. to

Method reference in java

三世轮回 提交于 2019-12-23 02:44:16
问题 I'm exploring method reference in java, and just curious if following can be converted to a method reference List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8); list.forEach(item -> new SomeClass(item).someMethod(item)); I tried the following, but that didn't work list.forEach(SomeClass::new::someMethod); 回答1: There are four types of method reference, that you can use based on java specification , you can use only this type method reference Reference to a static method Class: