java-8

Providing Limit condition on Stream generation [duplicate]

ぐ巨炮叔叔 提交于 2019-12-29 09:01:33
问题 This question already has answers here : Limit a stream by a predicate (19 answers) Closed 5 years ago . I am writing a code to calculate Fibonacci numbers. With this code I can generate first n numbers of the Fibonacci sequence. Stream.generate(new Supplier<Long>() { private long n1 = 1; private long n2 = 2; @Override public Long get() { long fibonacci = n1; long n3 = n2 + n1; n1 = n2; n2 = n3; return fibonacci; } }).limit(50).forEach(System.out::println); The method limit returns the Stream

Android Studio: Error:org.gradle.tooling.BuildException: Failed to generate v1 signature

不问归期 提交于 2019-12-29 08:54:31
问题 The problem is arising in Android Studio 3.0. When I create a new project, everything works fine but after sometime, the error Error:org.gradle.tooling.BuildException: Failed to generate v1 signature Starts to appear automatically which causes the gradle to fail. When I run it with --stacktrace, here's what it shows me: Information:Gradle tasks [:app:assembleDebug] Error:org.gradle.tooling.BuildException: Failed to generate v1 signature Error:java.io.IOException: Failed to generate v1

How to convert the code to use CompletableFuture?

[亡魂溺海] 提交于 2019-12-29 08:22:20
问题 I used to have a callable class class SampleTask implements Callable<Double> { @Override public Double call() throws Exception { return 0d; } } I used to use ExecutorService to submit the Callable . How to change to use CompletableFuture.supplyAsync ? The following code cannot compile SampleTask task = new SampleTask(); CompletableFuture.supplyAsync(task); No instance of type of variable U exists so that SampleTask conforms to Supplier 回答1: supplyAsync() expects a Supplier<U> and you are

Parse clock time in java 8

安稳与你 提交于 2019-12-29 08:09:12
问题 I wonder is it possible to parse clock time hour:minute:second in Java 8? e.g. final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss"); final String str = "12:22:10"; final LocalDateTime dateTime = LocalDateTime.parse(str, formatter); I tried but get this exception: Exception in thread "main" java.time.format.DateTimeParseException: Text '12:22:10' could not be parsed: Unable to obtain LocalDateTime from TemporalAccessor: {},ISO resolved to 12:22:10 of type java.time

How to use lambda expression in jrxml file?

女生的网名这么多〃 提交于 2019-12-29 07:56:35
问题 I am trying to use lambda expression inside jrxml file to get values for my field. I imported appropriate classes but it is giving me error that p cannot be resolved to a variable. How to use a lambda expression inside JasperReports 's templates?. <textFieldExpression> <![CDATA[StringUtils.join(((Collection<Property>)((Object2)$F{field1}.getPropertyValue("property1")).getPropertyValue("property2")).stream().map(p->((Object1)p.getValue()).getName()).collect(toList()),',');]]> <

Java Time's week-of-week-based-year pattern parsing with DateTimeFormatter

帅比萌擦擦* 提交于 2019-12-29 07:56:08
问题 I need to output the current date in the format week-based-year - week-of-week-based-year , i.e. using the ISO week date where the week always starts on a Monday and the first week of the year is the first one that has at least four days in January (so the week with the first Thursday in January). Since 31 December 2015 was a Thursday, the Friday through Sunday, i.e. 1st through 3rd of January 2016, all belong in the 53rd week of 2015 (a "long year"), and the first week of 2016 starts on

Java 8 - fill ArrayList

扶醉桌前 提交于 2019-12-29 07:46:25
问题 Is there a better way to fill an ArrayList like this (I have done it like this in Java 7): List<ScheduleIntervalContainer> scheduleIntervalContainers = new ArrayList<>(); scheduleIntervalContainers.add(scheduleIntervalContainer); 回答1: To fill a List , it is possible to generate an infinite Stream using Stream.generate(s) and then limit the number of results with limit(maxSize). For example, to fill a List of 10 new ScheduleIntervalContainer objects: List<ScheduleIntervalContainer>

How to convert a collection/ array into JSONArray using stream in java 8

回眸只為那壹抹淺笑 提交于 2019-12-29 07:05:09
问题 Im having a double array , I need to convert the array into a JSONArray using java streams. I tried using forEach (shared mutability) which leads to loss of data. public static JSONArray arrayToJson(double[] array) throws JSONException{ JSONArray jsonArray = new JSONArray(); Arrays.stream(array) .forEach(jsonArray::put); return jsonArray; } Is there any way I could to create JSONArray using streams? 回答1: Your code works, but you can write something like this: return Arrays.stream(array)

Is there a way to force parallelStream() to go parallel?

北战南征 提交于 2019-12-29 06:59:47
问题 If the input size is too small the library automatically serializes the execution of the maps in the stream, but this automation doesn't and can't take in account how heavy is the map operation. Is there a way to force parallelStream() to actually parallelize CPU heavy maps? 回答1: There seems to be a fundamental misunderstanding. The linked Q&A discusses that the stream apparently doesn’t work in parallel, due to the OP not seeing the expected speedup. The conclusion is that there is no

Is there a way to force parallelStream() to go parallel?

℡╲_俬逩灬. 提交于 2019-12-29 06:59:14
问题 If the input size is too small the library automatically serializes the execution of the maps in the stream, but this automation doesn't and can't take in account how heavy is the map operation. Is there a way to force parallelStream() to actually parallelize CPU heavy maps? 回答1: There seems to be a fundamental misunderstanding. The linked Q&A discusses that the stream apparently doesn’t work in parallel, due to the OP not seeing the expected speedup. The conclusion is that there is no