Linked Questions
20 questions linked to/from Generic type inference not working with method chaining?
13 votes
2 answers
22k views
What is the type of Map.Entry.comparingByValue().reversed()? [duplicate]
I have a list of map entries Map<String, Integer> map = new HashMap<>(); ...(fill the map)... List<Entry<String, Integer>> entries = new ArrayList<>(map.entrySet()); and ...
13 votes
2 answers
27k views
Comparator comparingInt [duplicate]
I need to sort a list of Points. First I need to compare the x value, then if the x values are equal, the y value. So I thought I'd use the thenComparing method: Comparator<Point> cmp = ...
1 vote
1 answer
1k views
Why does returning CompletableFuture.completedFuture(...) from inside a try-catch block (appear to) cause this error? [duplicate]
I have a method that returns a CompletionStage<> at various points, but it appears that doing so from within a try-catch block causes an "Incompatible types" error: Incompatible types ...
2 votes
0 answers
85 views
Understand Java Stream.Iterate() behavior with generic types [duplicate]
I am new to Java generics and trying to understand how Stream.iterate() works with generic types. I have a validator class that has some validation and manipulation logic using reflection. In the ...
38 votes
8 answers
70k views
Java List <T> T[] toArray(T[] a) implementation
I was just looking at the method defined in the List interface: <T> T[] toArray(T[] a) , and I have a question. Why is it generic? Because of that fact, method is not complete type-safe. The ...
40 votes
6 answers
10k views
Why is this type inference not working with this Lambda expression scenario?
I have a weird scenario where type inference isn't working as I'd expect when using a lambda expression. Here's an approximation of my real scenario: static class Value<T> { } @...
24 votes
5 answers
15k views
Java lambda returning a lambda
I am trying to do what seems to be a relatively basic thing in the new JDK 8 land of functional programming, but I can't get it to work. I have this working code: import java.util.*; import java....
16 votes
5 answers
6k views
Java 8 Optional cannot be applied to interface
Using Optional, I want to return a certain implementation (First or Second) of an interface according to the mapping result. This is the interface that First and Second implement: public interface ...
6 votes
4 answers
13k views
Converting List<MyObject> to Map<String, List<String>> in Java 8 when we have duplicate elements and custom filter criteria
I have an instances of Student class. class Student { String name; String addr; String type; public Student(String name, String addr, String type) { super(); this....
14 votes
2 answers
5k views
List<Object[]> to Map<K, V> in java 8
Often there is the need to transform results for a query like: select category, count(*) from table group by category to a map in which keys are categories and values are count of records belonging ...
7 votes
1 answer
5k views
Convert Consumer into Runnable inside Stream.map()
I am trying to convert a Consumer to a Runnable. The following code does not generate any compiler errors in Eclipse IDE. Consumer<Object> consumer; Runnable runnable; Object value; ... ...
8 votes
3 answers
5k views
Unable to understand this Java Stream+Generics example
Could someone help me to understand why this code behaves as described in the comments // 1) compiles List<Integer> l = Stream.of(1, 2, 3).collect(ArrayList::new, ArrayList::add, ArrayList<...
14 votes
3 answers
4k views
Java Stream Generics Type Mismatch
While manipulating Java 8 streams I've encountered an error where the compiler seems to 'forget' the type my generic parameters. The following snippet creates a stream of class names and attempts to ...
12 votes
1 answer
2k views
Generic type parameters inference in method chaining
After reading this question, I've started to think about generic methods in Java 8. Specifically, what happens with generic type parameters when methods are chained. For this question, I will use ...
3 votes
2 answers
605 views
Cannot return Stream of subclass objects after filtering
I have a method that returns a Stream of type A. I also have a subtype B of A. The method creates a Stream that it populates with instances of B, which are also of type A due to inheritance. This ...