Linked Questions

13 votes
2 answers
22k views

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 ...
John McClane's user avatar
  • 3,578
13 votes
2 answers
27k views

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 = ...
fwend's user avatar
  • 1,853
1 vote
1 answer
1k views

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 ...
Andrew Cheong's user avatar
2 votes
0 answers
85 views

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 ...
thiru's user avatar
  • 21
38 votes
8 answers
70k views

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 ...
midas's user avatar
  • 1,858
40 votes
6 answers
10k views

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> { } @...
Josh Stone's user avatar
  • 4,478
24 votes
5 answers
15k views

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....
MK.'s user avatar
  • 34.8k
16 votes
5 answers
6k views

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 ...
Nikolas's user avatar
  • 45k
6 votes
4 answers
13k views

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....
prime's user avatar
  • 15.8k
14 votes
2 answers
5k views

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 ...
Dragan Bozanovic's user avatar
7 votes
1 answer
5k views

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; ... ...
Nathan's user avatar
  • 9,111
8 votes
3 answers
5k views

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<...
Luigi Cortese's user avatar
14 votes
3 answers
4k views

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 ...
Chris Kerekes's user avatar
12 votes
1 answer
2k views

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 ...
fps's user avatar
  • 34.5k
3 votes
2 answers
605 views

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 ...
Sahand's user avatar
  • 8,450

15 30 50 per page