method-reference Questions

3

Solved

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) { boo...
Saddlery asked 6/7, 2015 at 11:50

1

Solved

Consider the following snippet: public static void main(String[] args) { Function<String, String> function = String::toUpperCase; //OK // Comparator<String> comparator = String::toUppe...
Godson asked 31/5, 2024 at 7:31

2

Solved

Say I want to assign the java method Log.d(String, String) to a variable x of method type (String, String) -> Int and I do it like this: val x: (String, String) -> Int = android.util.Log::d ...
Temperance asked 8/12, 2014 at 9:59

18

Solved

I am looking for a way to pass a method by reference. I understand that Java does not pass methods as parameters, however, I would like to get an alternative. I've been told interfaces are the al...
Cringe asked 2/2, 2010 at 19:20

7

Solved

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 rec...
Guerrero asked 10/3, 2016 at 11:10

3

Solved

The purpose is to create a new Predicate usable in a stream filter : myCollectionOfElement .stream() .filter( MyStaticHelperClass.compose(MyStaticHelperClass.getSubElement1OfTheElement(),MyStatic...
Heading asked 14/4, 2017 at 16:2

3

Solved

I'm learning about Method References from Java 8 and I have difficulties understanding why does this work? class Holder { private String holded; public Holder(String holded) { this.holded = ho...
Hallux asked 16/5, 2018 at 18:28

3

When I try to compile this code import java.util.Optional; public class GenericTest { public static void main(String[] args) { Optional.empty().map(o -> getStringClass(o)).orElse(String.cla...
Drone asked 25/5, 2019 at 11:18

4

Solved

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.annotatio...
Friedlander asked 18/11, 2015 at 16:48

1

Solved

Consider the following article from the JLS (§15.13.1) A method reference expression ending with Identifier is exact if it satisfies all of the following: If the method reference expression has t...
Sufflate asked 21/2, 2022 at 16:38

3

Solved

I am unable to grasp the concept of Method references in case of instance methods in Java For example in the example below, the compiler is giving error in the list line. I have seen the examples o...
Cantilever asked 3/4, 2021 at 11:20

4

Solved

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....
Glooming asked 7/8, 2014 at 0:51

7

Solved

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/m...
Unideaed asked 8/5, 2014 at 5:23

5

Why doesn't this code compile ? Unable to grasp nuances java method reference completely :( public class TestClass { static void println() {} public static void main(String[] args) { Runnabl...
Spenserian asked 1/12, 2020 at 10:24

1

Solved

I'm sort of new to TypeScript, I know Java quite well though. There, whenever a functional interface needs to be satisfied, it's most commonly done using lambda expressions (->) or method refere...
Cia asked 5/10, 2020 at 5:30

2

Solved

First things first, let me add the actual "example code": Map<CarBrand, List<Car>> allCarsAndBrands = new HashMap(); final String bmwBrandName = "BMW"; final String...
Glynisglynn asked 10/9, 2020 at 21:2

3

Solved

Doing the exercise 'Literature' on https://java-programming.mooc.fi/part-10/2-interface-comparable I discovered a very strange behavior when trying to sort key-value pairs in a HashMap, without cop...
Shilohshim asked 4/6, 2020 at 16:58

6

Solved

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 ...
Fikes asked 8/9, 2018 at 13:41

1

Solved

I'm trying to dynamically create a method reference of type BiConsumer through LambdaMetafactory. I was trying to apply two approaches found on https://www.cuba-platform.com/blog/think-twice-before...
Tudor asked 4/5, 2020 at 12:47

2

Suppose I do this in jshell: jshell> void printIsEven(int i) { ...> System.out.println(i % 2 == 0); ...> } | created method printIsEven(int) jshell> List<Integer> l = Arrays.a...
Annunciator asked 29/5, 2017 at 4:36

2

Solved

The api for Stream.max requires an argument of type Comparator<? super T>, and for Comparator, the only abstract method is int compare(T o1, T o2) but Double::compareTo, the compareTo api...
Centesimal asked 15/2, 2020 at 15:12

2

Solved

When using streams in Java, is it possible to use a method reference for arithmetic addition (rather than a lambda)? At the moment (to sum the contents of a stream) I am using stream.reduce(0, (x,...
Helgeson asked 20/1, 2020 at 22:36

1

Solved

Can someone suggest, why am I not able to apply method reference here? Working Code. System.out.println( Arrays.stream(str.split(" ")) .collect(Collectors.groupingBy(Function.identity(),Collect...
Bouffe asked 11/1, 2020 at 18:28

4

Solved

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 sameTe...
Fount asked 29/8, 2015 at 7:46

1

Solved

I have two factory-methods which produce "consumers" use different approaches 👉🏻 lambda and method references: @SuppressWarnings("Convert2MethodRef") public Consumer<String...
Rifling asked 18/11, 2019 at 17:42

© 2022 - 2025 — McMap. All rights reserved.