java-8

Understand the compile time error with Method Reference

雨燕双飞 提交于 2021-01-04 06:41:39
问题 As per the Documentation, Method Reference is absolutely not a static call. It works on both static and non- static methods. When we define our own non-static method in a given class and try to use it using Method Reference then the compile-time-error "cannot make static reference to non static method" is NOT seen in case of Function but only seen in case of Supplier, Consumer and Predicate. Why is that so? class Demo{ private Function<Student, Integer> p= Student::getGradeLevel; // fine

Understand the compile time error with Method Reference

扶醉桌前 提交于 2021-01-04 06:41:31
问题 As per the Documentation, Method Reference is absolutely not a static call. It works on both static and non- static methods. When we define our own non-static method in a given class and try to use it using Method Reference then the compile-time-error "cannot make static reference to non static method" is NOT seen in case of Function but only seen in case of Supplier, Consumer and Predicate. Why is that so? class Demo{ private Function<Student, Integer> p= Student::getGradeLevel; // fine

How to use BiConsumer to accept an object having an object, list of strings, and variables?

…衆ロ難τιáo~ 提交于 2021-01-01 09:24:43
问题 I am trying to use BiConsumer to accept an object that contains variables, an object and a list of strings in Java. I am not sure how to set the values into one object if using just BiConsumer. Maybe, if I tried to wrap Student object in a List and pass it into a new Student might help, but so far I get a null object. I haven't seen a lot of post with object containing just variables in one object and using BiConsumer. @Test public void testStudent() { List<Object> objectList1 = new ArrayList

How to use BiConsumer to accept an object having an object, list of strings, and variables?

会有一股神秘感。 提交于 2021-01-01 09:23:09
问题 I am trying to use BiConsumer to accept an object that contains variables, an object and a list of strings in Java. I am not sure how to set the values into one object if using just BiConsumer. Maybe, if I tried to wrap Student object in a List and pass it into a new Student might help, but so far I get a null object. I haven't seen a lot of post with object containing just variables in one object and using BiConsumer. @Test public void testStudent() { List<Object> objectList1 = new ArrayList

Sum of values from hashmap using streams

偶尔善良 提交于 2021-01-01 04:51:25
问题 I have an hashmap of huge size (around 10^60). I am putting values in each entry one by one. Problem is to get the sum of values from hashmap for given range of keys. eg: Putting it in simple Hashmap has entries from 0 to 1000 as a key and every key has an value(BigInteger). Now problem is to get sum of values from range (say) 37 to 95. I have tried with iterator but when we go for huge map of size 10^60, it is time taking operation for large range of indices. I am trying it with streams but

Sum of values from hashmap using streams

*爱你&永不变心* 提交于 2021-01-01 04:49:04
问题 I have an hashmap of huge size (around 10^60). I am putting values in each entry one by one. Problem is to get the sum of values from hashmap for given range of keys. eg: Putting it in simple Hashmap has entries from 0 to 1000 as a key and every key has an value(BigInteger). Now problem is to get sum of values from range (say) 37 to 95. I have tried with iterator but when we go for huge map of size 10^60, it is time taking operation for large range of indices. I am trying it with streams but

Get timezone of area with country code in Java

限于喜欢 提交于 2021-01-01 02:17:49
问题 I have to pass a message (jms) with timezone info like (America/Los_Angeles) but I have only country name and code. If it possible get timezone info with Java code. Somewhere I read this: System.out.println(TimeZone.getTimeZone("US")); But its giving output as sun.util.calendar.ZoneInfo[id="GMT",offset=0,dstSavings=0,useDaylight=false,transitions=0,lastRule=null] I am expecting List of "America/Los_Angeles", ... 回答1: As per the documentation the getTimeZone method returns the specified

Get timezone of area with country code in Java

孤街醉人 提交于 2021-01-01 02:15:14
问题 I have to pass a message (jms) with timezone info like (America/Los_Angeles) but I have only country name and code. If it possible get timezone info with Java code. Somewhere I read this: System.out.println(TimeZone.getTimeZone("US")); But its giving output as sun.util.calendar.ZoneInfo[id="GMT",offset=0,dstSavings=0,useDaylight=false,transitions=0,lastRule=null] I am expecting List of "America/Los_Angeles", ... 回答1: As per the documentation the getTimeZone method returns the specified

Java 8: List files from multiple paths

自古美人都是妖i 提交于 2020-12-31 04:49:31
问题 How to search files from multiple paths in Java 8. These are not sub/sibling directories. For example, if I want to search json files in a path, I have: try (Stream<Path> stream = Files.find(Paths.get(path), Integer.MAX_VALUE, (p, attrs) -> attrs.isRegularFile() && p.toString().endsWith(".json"))) { stream.map((p) -> p.name).forEach(System.out::println); } Is there a better way to search in multiple paths? Or do I have to run the same code for multiple paths? 回答1: Yes you can do it. Assuming

How to execute an Array of CompletableFuture and compose their results

泪湿孤枕 提交于 2020-12-31 04:48:20
问题 I am investigating Java 8 CompletableFutures and read (and seen) that I should employ thenCompose instead of thenApply . I have converted my code to use thenCompose but I have a feeling in an incorrect manner. Here is my controlling code... final CompletableFuture<List<String>> extractor = get(htmlPageSource); @SuppressWarnings("unchecked") final CompletableFuture<List<Documentable>>[] completableFutures = new CompletableFuture[ENDPOINT.EXTRACTABLES.size()]; int index = 0; for( ENDPOINT