Latest Questions tagged Method Reference

Browse all latest questions tagged Method Reference

30
Votes
Published 19 Sep, 2022
Java - Java 8 method references and overridden methods

I've been using lambdas and method references in Java 8 for a while and there is this one thing I do not understand. Here is the example code: Set<Integer> first = Collections.singleton(1);...

114
Votes
Published 05 Sep, 2022
Java - Reference to methods with different parameters in Java8

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<&...

13
Votes
Published 19 Sep, 2022
Java - How to print multiple parameters using Method reference in java8

I'm trying to print out basic hashmap with twoin java. Map<Integer, String> mp = new HashMap<Integer, String>(); mp.put(10, "apple"); mp.put(20, "orange"); mp.put(30, "banana"); But I c...

55
Votes
Published 13 Sep, 2022
Java - How to suitably compose Predicate and Function in a Java function?

The purpose is to create a new Predicate usable in a stream filter : myCollectionOfElement .stream() .filter( MyStaticHelperClass.compose(MyStaticHelperClass.getSubElement1OfTheElement(),MyStatic...

99
Votes
Published 12 Sep, 2022
Java - Is method reference caching a good idea in Java 8?

Consider I have code like the following: class Foo { Y func(X x) {...} void doSomethingWithAFunc(Function<X,Y> f){...} void hotFunction(){ doSomethingWithAFunc(this::func);...

106
Votes
Published 10 Sep, 2022
Java - How do Java 8 array constructor references work?

Say we have a variable of type IntFunction that returns an integer array: IntFunction<int[]> i; With Java 8 generics, it is possible to initialize this variable with a constructor reference l...

51
Votes
Published 07 Sep, 2022
Java - What's the difference between instance method reference types in Java 8?

So Java 8 introduces method references and the docs describe the four types. My question is what's the difference between the two instance types? Reference to an instance method of a particula...

100
Votes
Published 06 Sep, 2022
Java - Java 8 constructor method references

I am reading Java 8 book, and it comes with a sample I reproduce: @FunctionalInterface public interface Action { public void perform(); } An Implementor: public final class ActionImpl implements...

112
Votes
Published 04 Sep, 2022
Java - What does "an Arbitrary Object of a Particular Type" mean in java 8?

In Java 8 there is "Method Reference" feature. One of its kind is "Reference to an instance method of an arbitrary object of a particular type" http://docs.oracle.com/javase/tutorial/java/javaOO/meth...

108
Votes
Published 27 Aug, 2022
Java - Java 8: Difference between method reference Bound Receiver and UnBound Receiver

I am trying to use Java 8 method references in my code. There are four types of method references available. Static method reference. Instance Method (Bound receiver). Instance Method (UnBound rece...

37
Votes
Published 25 Aug, 2022
Java - The type org.eclipse.jdt.annotation.NonNull cannot be resolved. It is indirectly referenced from required .class files

When I use the Java 8 method reference double colon operator (::) with new operator (e.g. MyType::new), I get this error in Eclipse of Spring Tool suite (STS): The type org.eclipse.jdt.annotation....

62
Votes
Published 24 Aug, 2022
Java - Stream if not null and get the first object

I am trying to apply stream on a list which can be null as it is calling a repository method and I want to get the first element from it if it is not null and compare one of its parameter with request...

19
Votes
Published 24 Aug, 2022
Java - BiConsumer and method reference of one parameter

Why is it legal to pass a method reference of one parameter as an argument of expected type BiConsumer whose abstract method requires two arguments? Example: class Experiment { private String n...

33
Votes
Published 09 Sep, 2022
Java - Static context cannot access non-static in Collectors

I have group of students. First I want to group them by the marks. Then I want to further group those sets into same name students together. Map<Integer,Map<String,List<String>>> gr...

18
Votes
Published 19 Aug, 2022
Java - Comparator.reversed() does not compile using lambda

I have a list with some User objects and i'm trying to sort the list, but only works using method reference, with lambda expression the compiler gives an error: List<User> userList = Arrays.asL...