java-8

Why Instant does not support operations with ChronoUnit.YEARS?

时光毁灭记忆、已成空白 提交于 2020-01-11 19:55:12
问题 This was unexpected to me: > Clock clock = Clock.systemUTC(); > Instant.now(clock).minus(3, ChronoUnit.DAYS); java.time.Instant res4 = 2016-10-04T00:57:20.840Z > Instant.now(clock).minus(3, ChronoUnit.YEARS); java.time.temporal.UnsupportedTemporalTypeException: Unsupported unit: Years As a workaround I have to do this: > Instant.now(clock).atOffset(ZoneOffset.UTC).minus(3, ChronoUnit.YEARS).toInstant(); java.time.Instant res11 = 2013-10-07T01:02:56.361Z I am curios why Instant does not

Usage of multiple inheritance in Java 8

冷暖自知 提交于 2020-01-11 15:49:07
问题 Am I using a feature of Java 8 or misusing it? Refer the code and explanation below to know as to why it was chosen to be like this. public interface Drawable { public void compileProgram(); public Program getProgram(); default public boolean isTessellated() { return false; } default public boolean isInstanced() { return false; } default public int getInstancesCount() { return 0; } public int getDataSize(); public FloatBuffer putData(final FloatBuffer dataBuffer); public int getDataMode();

Dynamically change the color of a Rectangle in Javafx

南笙酒味 提交于 2020-01-11 13:59:17
问题 I am creating a two javafx.scene.shape.Rectangle objects in a GridPane and doing the following. rectArray = new Rectangle[2]; boardGrid.setStyle("-fx-background-color: #C0C0C0;"); rectArray[0] = new Rectangle(12,12); rectArray[0].setFill(Color.AQUA); boardGrid.add(rectArray[0], 2, 0); rectArray[1] = new Rectangle(12,12); rectArray[1].setFill(Color.BLACK); boardGrid.add(rectArray[1], 2, 1); Button buttonStart = new Button("Change color"); buttonStart.addEventHandler(MouseEvent.MOUSE_CLICKED,

Nested lambda expressions results in a warning in Eclipse Luna

烂漫一生 提交于 2020-01-11 09:15:12
问题 I've installed Eclipse Luna on my MAC OSX with Java8 and when I write simple nested Lambda expression it give the following warning: (Recovered) Internal inconsistency detected during lambda shape analysis Eventually, the code compiles and run perfectly, but still this warning is disturbing.... Thank you. 来源: https://stackoverflow.com/questions/26579881/nested-lambda-expressions-results-in-a-warning-in-eclipse-luna

Nested lambda expressions results in a warning in Eclipse Luna

只愿长相守 提交于 2020-01-11 09:15:08
问题 I've installed Eclipse Luna on my MAC OSX with Java8 and when I write simple nested Lambda expression it give the following warning: (Recovered) Internal inconsistency detected during lambda shape analysis Eventually, the code compiles and run perfectly, but still this warning is disturbing.... Thank you. 来源: https://stackoverflow.com/questions/26579881/nested-lambda-expressions-results-in-a-warning-in-eclipse-luna

Java mapToInt vs Reduce with map

一个人想着一个人 提交于 2020-01-11 09:06:10
问题 I've been reading up on reduce and have just found out that there is a 3 argument version that can essentially perform a map reduce like this: String[] strarr = {"abc", "defg", "vwxyz"}; System.out.println(Arrays.stream(strarr).reduce(0, (l, s) -> l + s.length(), (s1, s2) -> s1 + s2)); However I can't see the advantage of this over a mapToInt with a reduce. System.out.println(Arrays.stream(strarr).mapToInt(s -> s.length()).reduce(0, (s1, s2) -> s1 + s2)); Both produce the correct answer of 12

Java mapToInt vs Reduce with map

淺唱寂寞╮ 提交于 2020-01-11 09:06:00
问题 I've been reading up on reduce and have just found out that there is a 3 argument version that can essentially perform a map reduce like this: String[] strarr = {"abc", "defg", "vwxyz"}; System.out.println(Arrays.stream(strarr).reduce(0, (l, s) -> l + s.length(), (s1, s2) -> s1 + s2)); However I can't see the advantage of this over a mapToInt with a reduce. System.out.println(Arrays.stream(strarr).mapToInt(s -> s.length()).reduce(0, (s1, s2) -> s1 + s2)); Both produce the correct answer of 12

Tool to Delambdafy Java code from Java 8 to Java 7 syntax? [closed]

人走茶凉 提交于 2020-01-11 08:59:08
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 2 years ago . Does anyone know of any tool to convert Java 8 code (at the source level) that uses lambdas and method references into Java 7 code that uses anonymous inner classes? I know about Retrolambda, but that works at the bytecode level, not the source level. For now, I have a version working that works as an IntelliJ

Throw exception if Optional<> value is present

老子叫甜甜 提交于 2020-01-11 08:53:09
问题 Suppose I have a Spring Data Repository method. Optional<Branch> findByName(@Nonnull final String name); My business logic is such if I find any value for this method execution I would throw an exception. I could do this for example : Optional.of(branchRepository.findByName(branch.getName())) .filter(bo -> !bo.isPresent()) .orElseThrow(NameNotAvailableException::new); or another way: Optional.of(branchRepository.findByName(branch.getName())) .filter(Optional::isEmpty) .orElseThrow

Finding the median value from a List of objects using Java 8

别等时光非礼了梦想. 提交于 2020-01-11 08:41:40
问题 I have two classes that are structured like this: public class Company { private List<Person> person; ... public List<Person> getPerson() { return person; } ... } public class Person { private Double age; ... public Double getAge() { return age; } ... } Basically the Company class has a List of Person objects, and each Person object can get an Age value. If I get the List of the Person objects, is there a good way to use Java 8 to find the median Age value among all the Person objects (Stream