Convert two dimensional array to List in java?

This is a nice way of doing it for any two-dimensional array, assuming you want them in the following order: [[array[0]-elems], [array[1]elems]…] public <T> List<T> twoDArrayToList(T[][] twoDArray) { List<T> list = new ArrayList<T>(); for (T[] array : twoDArray) { list.addAll(Arrays.asList(array)); } return list; }

What implementation detail makes this code fail so easily?

First, it’s not failing reliably. I managed to have some runs where no exception occurred. This, however doesn’t imply that the resulting map is correct. It’s also possible that each thread witnesses its own value being successfully put, while the resulting map misses several mappings. But indeed, failing with a NullPointerException happens quite often. I … Read more

Non-static method cannot be referenced from a static context in java 8 streams

First it should be noted, that the message is issued not by java compiler (javac), but by IntelliJ IDEA. You can see javac messages in “Messages Build” window when you actually launch a build process. What you see in editor window is messages generated by IDEA itself and they could differ. The error message is … Read more

Is there a way to have a Java8 duration of one year that accounts for leap years?

As per the response in Getting Duration using the new dateTime API you should be using Period p = Period.ofYears(1); It’s important to understand the difference between Duration (exact number of nanoseconds < 1 day) and Period (variable > 1 day). Duration won’t account for leap days, daylight savings time or leap seconds, for example, … Read more

Java 8 stream operation on empty list

collect is a terminal operation, so it must be evaluated. When terminating a Stream pipeline with collect(Collectors.toList()), you’ll always get an output List (you’ll never get null). If the Stream is empty (and it doesn’t matter if it’s empty due to the source of the stream being empty, or due to all the elements of … Read more

Java 8 streams conditional processing

Java 8 streams weren’t designed to support this kind of operation. From the jdk: A stream should be operated on (invoking an intermediate or terminal stream operation) only once. This rules out, for example, “forked” streams, where the same source feeds two or more pipelines, or multiple traversals of the same stream. If you can … Read more

Why is Stream.sorted not type-safe in Java 8?

Essentially, you’re asking if there’s a way to tell the compiler, “hey, this one method requires the type parameter match more specific bounds than defined at the class level“. This is not possible in Java. Such a feature may be useful but I’d also expect confusing and/or complicated. There’s also no way to make Stream.sorted() … Read more

Join a list of object’s properties into a String

To retrieve a String consisting of all the ID’s separated by the delimiter “,” you first have to map the Person ID’s into a new stream which you can then apply Collectors.joining on. String result = personList.stream().map(Person::getId) .collect(Collectors.joining(“,”)); if your ID field is not a String but rather an int or some other primitive numeric … Read more