java-8

How to pass a Java 8 lambda with a single parameter

你说的曾经没有我的故事 提交于 2021-01-21 10:10:32
问题 I want to simply pass a lambda (chunk of code) and execute it when I need to. How do I implement the method executeLambda(...) in the code below (as well what is the method signature): public static void main(String[] args) { String value = "Hello World"; executeLambda(value -> print(value)); } public static void print(String value) { System.out.println(value); } public static void executeLambda(lambda) { someCode(); lamda.executeLambdaCode(); someMoreCode(); } 回答1: Your lambda takes one

How to pass a Java 8 lambda with a single parameter

佐手、 提交于 2021-01-21 10:09:21
问题 I want to simply pass a lambda (chunk of code) and execute it when I need to. How do I implement the method executeLambda(...) in the code below (as well what is the method signature): public static void main(String[] args) { String value = "Hello World"; executeLambda(value -> print(value)); } public static void print(String value) { System.out.println(value); } public static void executeLambda(lambda) { someCode(); lamda.executeLambdaCode(); someMoreCode(); } 回答1: Your lambda takes one

How to pass a Java 8 lambda with a single parameter

被刻印的时光 ゝ 提交于 2021-01-21 10:07:40
问题 I want to simply pass a lambda (chunk of code) and execute it when I need to. How do I implement the method executeLambda(...) in the code below (as well what is the method signature): public static void main(String[] args) { String value = "Hello World"; executeLambda(value -> print(value)); } public static void print(String value) { System.out.println(value); } public static void executeLambda(lambda) { someCode(); lamda.executeLambdaCode(); someMoreCode(); } 回答1: Your lambda takes one

Java 8 collect() only isPresent() Optional values [duplicate]

江枫思渺然 提交于 2021-01-21 00:45:34
问题 This question already has answers here : Using Java 8's Optional with Stream::flatMap (12 answers) Closed 4 years ago . Is there a more elegant way of practically achieving this in Java 8? list.stream() .map(e -> myclass.returnsOptional(e)) .filter(Optional::isPresent) .map(Optional::get) .collect(Collectors.toList()); I'm talking about filter(Optional::isPresent) followed by map(Optional::get) , I want to elegantly collect in a list only Optional results which have a value. 回答1: In your case

Java 8 collect() only isPresent() Optional values [duplicate]

让人想犯罪 __ 提交于 2021-01-21 00:36:49
问题 This question already has answers here : Using Java 8's Optional with Stream::flatMap (12 answers) Closed 4 years ago . Is there a more elegant way of practically achieving this in Java 8? list.stream() .map(e -> myclass.returnsOptional(e)) .filter(Optional::isPresent) .map(Optional::get) .collect(Collectors.toList()); I'm talking about filter(Optional::isPresent) followed by map(Optional::get) , I want to elegantly collect in a list only Optional results which have a value. 回答1: In your case

Java 8 collect() only isPresent() Optional values [duplicate]

我的未来我决定 提交于 2021-01-21 00:35:42
问题 This question already has answers here : Using Java 8's Optional with Stream::flatMap (12 answers) Closed 4 years ago . Is there a more elegant way of practically achieving this in Java 8? list.stream() .map(e -> myclass.returnsOptional(e)) .filter(Optional::isPresent) .map(Optional::get) .collect(Collectors.toList()); I'm talking about filter(Optional::isPresent) followed by map(Optional::get) , I want to elegantly collect in a list only Optional results which have a value. 回答1: In your case

Convert Stream to IntStream

只愿长相守 提交于 2021-01-20 21:54:05
问题 I have a feeling I'm missing something here. I found myself doing the following private static int getHighestValue(Map<Character, Integer> countMap) { return countMap.values().stream().mapToInt(Integer::intValue).max().getAsInt(); } My problem is with the silly conversion from Stream to IntStream via the mapToInt(Integer::intValue) Is there a better way of doing the conversion? all this is to avoid using max() from Stream , which requires passing a Comparator but the question is specifically

Is there a reason for java.time.ZoneId not including an Enum of ZoneIds?

六眼飞鱼酱① 提交于 2021-01-20 18:50:47
问题 To get a ZoneId it goes like: ZoneId.of("America/Sao_Paulo"); or ZoneId.of(ZoneId.SHORT_IDS.get("BET")); Why would be the reason for not existing an Enum of such values, like: ZoneId.of(ZoneIds.AMERICA_SAO_PAULO); which seems less error-prone and a lot more auto-complete friendly? 回答1: I believe it's because the list of all possible timezones names can change regardless of Java version. Timezone information comes with Java installation (usually in the folder <java-home>/lib/zi , or in jre/lib

Is the writer's reason correct for using thenCompose and not thenComposeAsync

家住魔仙堡 提交于 2021-01-20 18:20:52
问题 This question is different from this one Difference between Java8 thenCompose and thenComposeAsync because I want to know what is the writer's reason for using thenCompose and not thenComposeAsync . I was reading Modern Java in action and I came across this part of code on page 405: public static List<String> findPrices(String product) { ExecutorService executor = Executors.newFixedThreadPool(10); List<Shop> shops = Arrays.asList(new Shop(), new Shop()); List<CompletableFuture<String>>

Why use reflection to access class members when MethodHandle is faster?

不羁的心 提交于 2021-01-20 14:50:34
问题 With the release of Java 7 came the MethodHandle, which allows a user to invoke a method as if using its underlying bytecode. In particular, the MethodHandles.Lookup class provides factory methods to create method handles to access class members: The factory methods on a Lookup object correspond to all major use cases for methods, constructors, and fields. Each method handle created by a factory method is the functional equivalent of a particular bytecode behavior. Functionally, this is more