java-8

Java 8 forEach over multiple IntStreams

别来无恙 提交于 2019-12-23 10:51:21
问题 I have the following code: IntStream.range(0, width).forEach(x1 -> { IntStream.range(0, height).forEach(y1 -> { IntStream.rangeClosed(x1-1, x1+1).forEach(x2 -> { IntStream.rangeClosed(y1-1, y1+1).forEach(y2 -> { if ((x1 != x2 || y1 != y2) && getNode(x2, y2) != null){ getNode(x1, y1).registerObserverAtNeighbor(getNode(x2, y2)); } }); }); }); }); Is there a way to write the above using fewer nested statements? It's basically "for each node from (0,0) to (width,height) register observer at nodes

How to map only specified fields using mapStruct?

醉酒当歌 提交于 2019-12-23 09:56:12
问题 MapStruct is mapping all the properties of source and destination by default if they have same name. Although we can use ignore property in @Mapping for omitting any field mapping. But that's not I want. I want control over the mapping strategy. I've to specify something like @Mapper(STRATEGY=MAPPING_STRATEGY.SPECIFIED) public interface EmployeeToEmployeeDTOMapper { @Mappings({ @Mapping(target="id", source="id"), @Mapping(target="name", source="name") }) public EmployeeDTO

Java lambda sublist

旧街凉风 提交于 2019-12-23 09:42:36
问题 What is the shortest way to express "get new List B from List A where condition" via a Java 8 lambda? Say I have List<Integer> a = Arrays.asList(1, 2, 3, 4, 5) and I want a new List, B, where the value is > 3. I've read through the new Collections Streams API, but I'm not convinced I have found the best way to do this, and don't want to taint the question with what is probably my less than perfect solution. 回答1: a.stream().filter(x -> x > 3).collect(Collectors.toList()); 来源: https:/

Is it worked Nashorn JS object to java.util.Map?

天涯浪子 提交于 2019-12-23 09:34:41
问题 I have java method void someMethod(String str, Map map) { ... } From JS call this method var map = new Object() map.key1 = "val1" ...someMethod(str, map) Exception: java.lang.NoSuchMethodException: None of the fixed arity signatures [(java.lang.String, java.util.Map)] of method org.prjctor.shell.Bash.eval match the argument types [java.lang.String, jdk.nashorn.internal.scripts.JO] But in Nashorn docs "Mapping of Data Types Between Java and JavaScript" said "Every JavaScript object is also a

Is it possible to debug Lambdas in Java 8

只谈情不闲聊 提交于 2019-12-23 09:29:33
问题 I just started playing with Java 8 Lambdas and I noticed that I can't debug them in the NetBeans IDE. If I try to attach a breakpoint to the following code I get a variable breakpoint which is definately not what I wanted: private EventListener myListener (Event event) -> { command1; command2; // Set Breakpoint here command3; }; NetBeans attaches the debugger at the "myListener" variable but I can't step into the EventListener itself so I can't see what is happening inside it. Is there

What does '->' do in Java?

让人想犯罪 __ 提交于 2019-12-23 09:29:31
问题 I was looking at some java tutorials and wasn't sure what '->' did and couldn't find anything on google about it. Here's some code that I saw that used it: myShapesCollection.stream() .filter(e -> e.getColor() == Color.RED) .forEach(e -> System.out.println(e.getName())); 回答1: That is the syntax used for lambda expressions, available in Java 8. For example, filter expects a Predicate and e -> e.getColor() == Color.RED is functionally equivalent to: new Predicate<Shape>() { public boolean test

Why should I not use identity based operations on Optional in Java8?

喜夏-厌秋 提交于 2019-12-23 09:19:50
问题 The javadoc for java.util.Optional states that: This is a value-based class; use of identity-sensitive operations (including reference equality (==), identity hash code, or synchronization) on instances of Optional may have unpredictable results and should be avoided. However, this junit snippet is green. Why? It seems to contradict the javadoc. Optional<String> holder = Optional.ofNullable(null); assertEquals("==", true, holder == Optional.<String>empty()); assertEquals("equals", true,

Why calling get() before exceptional completion waits for exceptionally to execute?

你。 提交于 2019-12-23 09:15:11
问题 While answering this question, I noticed a strange behaviour of CompletableFuture : if you have a CompletableFuture cf and chain a call with cf.exceptionally() , calling cf.get() appears to behave strangely: if you call it before exceptional completion, it waits for the execution of the exceptionally() block before returning otherwise, it fails immediately by throwing the expected ExecutionException Am I missing something or is this a bug? I am using Oracle JDK 1.8.0_131 on Ubuntu 17.04. The

Java 8, why not a ZonedTime class?

老子叫甜甜 提交于 2019-12-23 07:46:08
问题 I found that Java 8 doesn't have an equivalent to ZonedDateTime but to work only with Time (a ZonedTime class or something like that). I know they included the OffsetTime class, but it only stores the offset. Storing time zones along with date and time, instead of just store the offset, helps to deal with daylight savings easier. I'm not asking you to give me alternatives, I know there are many approaches; I'm just wondering why such class was not included, is it a design issue? or they just

Using JDK 7 Or Higher With Android Studio And Eclipse On Mac OSX

China☆狼群 提交于 2019-12-23 07:27:39
问题 Both Android Studio and Eclipse are asking me to install JDK 6 even though JDK 8 is already installed. Several workarounds online told me change the java_home to point the JDK 8 installation location, done that such that both /usr/libexec/java_home and java -version commands refer to the JDK 8 installation but Android Studio and Eclipse still asking me to install JDK 6. Anyone solved this issue. 回答1: Android Studio is based on Intellij Idea 13 . The solution here also applies. Just modifying