java-8

How to send parameters to a reference method in a stream (java 8)?

二次信任 提交于 2020-02-22 08:09:22
问题 I have a list of activities(Activity) and I want to determine a data structure of the form Map(String, DateTime) (not Duration or Period; DateTime it's a must) that maps. For each activity the total duration computed over the monitoring period. The class Activity has: activityLabel(String) , startTime(DateTime) , endTime(DateTime) . I use joda time. This is what I have done: Map<String, DateTime> durations = activities.stream().collect(Collectors.toMap( it -> it.activityLabel, it ->new

System.out.println() of Stream#reduce() unexpectedly prints “Optional[]” around result

蓝咒 提交于 2020-02-22 07:34:06
问题 I started to learn Lambda expressions of Java 8, and wrote below program to get sum of all numbers in the list: import java.util.Arrays; import java.util.List; public class MainClass { public static void main(String[] args) { List<Integer> number = Arrays.asList(1, 2, 3, 4, 5); System.out.println(number.stream().reduce((c,e) -> { return c + e; })); } } I was expecting the output to be: 15 but I got: Optional[15] Java version: 1.8.0_45 Please explain what does Optional[] means in the output?

Java swing applications not rendering properly with VNC

落花浮王杯 提交于 2020-02-21 12:52:04
问题 Swing applications show a blank window when launched on a VNC desktop with Java 8. I don't have this problem with JRE 7 and non-swing applications (eg. Eclipse) runs properly with the same settings. Are there some sort of default settings like 3D acceleration or something that have been enabled by default on JRE 8 ? How does i disable it ? 回答1: I had blank Java windows in VNC too, but with Java 7 and Java 8, so it wasn't much of a surprise that disabling xrender didn't help me. That probably

Advantages of Stream and Spring Data

匆匆过客 提交于 2020-02-21 10:35:05
问题 Some people override the CrudRepository's method findAll to return an Stream (java 8), but I saw they finally transform the Stream to a List in order to send it through a rest controller. Why are they using a Stream ? What is the advantage to use a Stream here? If they want to filter the records I think would be better filter on DataBase. 回答1: This is already supported in Spring Data JPA, look here; so there's not real advantage to override those to return Stream . If you really want a Stream

Advantages of Stream and Spring Data

淺唱寂寞╮ 提交于 2020-02-21 10:34:09
问题 Some people override the CrudRepository's method findAll to return an Stream (java 8), but I saw they finally transform the Stream to a List in order to send it through a rest controller. Why are they using a Stream ? What is the advantage to use a Stream here? If they want to filter the records I think would be better filter on DataBase. 回答1: This is already supported in Spring Data JPA, look here; so there's not real advantage to override those to return Stream . If you really want a Stream

How to stop LocalDate from changing when being saved to a mySQL database

百般思念 提交于 2020-02-21 09:49:53
问题 When saving a LocalDate field (e.g. '2017-09-27') to a mySQL Date column using JPA CriteriaBuilder API, the result is different (e.g. '2017-09-26'). I have validated that my database's timezone is set to UTC using SELECT TIMEDIFF(NOW(), UTC_TIMESTAMP) as the result is '00:00:00'. I am testing this locally, and I have a timezone of GMT + 2, so my suspicion is that when the conversion occurs from LocalDate to Date , 2 hours are being deducted and producing a date 1 day before the requested date

How to stop LocalDate from changing when being saved to a mySQL database

可紊 提交于 2020-02-21 09:48:52
问题 When saving a LocalDate field (e.g. '2017-09-27') to a mySQL Date column using JPA CriteriaBuilder API, the result is different (e.g. '2017-09-26'). I have validated that my database's timezone is set to UTC using SELECT TIMEDIFF(NOW(), UTC_TIMESTAMP) as the result is '00:00:00'. I am testing this locally, and I have a timezone of GMT + 2, so my suspicion is that when the conversion occurs from LocalDate to Date , 2 hours are being deducted and producing a date 1 day before the requested date

How to stop LocalDate from changing when being saved to a mySQL database

眉间皱痕 提交于 2020-02-21 09:48:26
问题 When saving a LocalDate field (e.g. '2017-09-27') to a mySQL Date column using JPA CriteriaBuilder API, the result is different (e.g. '2017-09-26'). I have validated that my database's timezone is set to UTC using SELECT TIMEDIFF(NOW(), UTC_TIMESTAMP) as the result is '00:00:00'. I am testing this locally, and I have a timezone of GMT + 2, so my suspicion is that when the conversion occurs from LocalDate to Date , 2 hours are being deducted and producing a date 1 day before the requested date

Bug in jdk8 date-conversion?

被刻印的时光 ゝ 提交于 2020-02-19 12:35:27
问题 I was writing some testcode for java-8 conversion between java.util.Date and java.time.LocalDateTime , and discovered an anomaly seems to occur in the hour after the transition from normaltime-to-summertime, when the year is 2038 or higher. I just wanted to know if this is a bug in jdk8, or if I am doing something wrong? Note: I am on Windows-7, 64-bit jdk, so should not be affected by the 2038-unix bug, which would have a much worse effect. Here my demo-code: package conversiontest; import

CompletableFuture supplyAsync

|▌冷眼眸甩不掉的悲伤 提交于 2020-02-17 07:04:39
问题 I've just started exploring some concurrency features of Java 8. One thing confused me a bit is these two static methods: CompletableFuture<Void> runAsync(Runnable runnable) CompletableFuture<U> supplyAsync(Supplier<U> supplier) Do anyone know why they choose to use interface Supplier? Isn't it more natural to use Callable, which is the analogy of Runnable that returns a value? Is that because Supplier doesn't throw an Exception that couldn't be handled? 回答1: Short answer No, it's not more