java-8

Is Stream.count() guranteed to visit each element?

落花浮王杯 提交于 2020-01-10 19:33:30
问题 In other words, is the following line guranteed to print num lines? int num = list.stream().peek(System.out::println).count(); This question was triggered by a discussion in the comments of https://stackoverflow.com/a/41346586/2513200 I vaguely remember a discussion that optimizations that avoid iteration might be legal, but didn't find anything conclusive during a quick search. The JavaDocs for Stream.count contain this statement: This is a special case of a reduction and is equivalent to:

Java SE 8 TemporalAccessor.from issues when used with a java.time.Instant object

随声附和 提交于 2020-01-10 17:22:47
问题 java.time has an Instant class which encapsulates a position (or 'moment') on the timeline. While I understand that this is a seconds/nanoseconds value so not directly related to time zones or times offsets, its toString returns a date and time formatted as a UTC date/time, eg 2014-05-13T20:05:08.556Z. Also anInstant.atZone(zone) and anInstant.atOffset(offset) both produce a value that is consistent with treating the Instant as having an implied UTC time-zone/'zero' offset. I would have

The target type of this expression must be a functional interface

非 Y 不嫁゛ 提交于 2020-01-10 13:31:32
问题 I created a function to filter with multiple predicates for which I perform a logical AND for them: @SafeVarargs public static <T> Stream<T> filter(Stream<T> source, Predicate<T>... predicates) { return source.filter(Arrays.stream(predicates).reduce(predicates[0], Predicate::and)); } When calling: filter(IntStream.range(0, 10).boxed(), x -> x % 2 != 0, x -> x%3 == 0).forEach(System.out::println); It works fine and prints 3 and 9. However when I pass a single predicate such as: filter

Are interfaces a valid substitute for utility classes in Java 8? [duplicate]

北城余情 提交于 2020-01-10 11:50:00
问题 This question already has answers here : Java 8: Interface with static methods instead of static util class (4 answers) Closed 4 years ago . For the past decade or so, I've been using the pattern below for my Java utility classes. The class contains only static methods and fields, is declared final so it can't be extended, and has a private constructor so it can't be instantiated. public final class SomeUtilityClass { public static final String SOME_CONSTANT = "Some constant"; private

Are interfaces a valid substitute for utility classes in Java 8? [duplicate]

情到浓时终转凉″ 提交于 2020-01-10 11:48:38
问题 This question already has answers here : Java 8: Interface with static methods instead of static util class (4 answers) Closed 4 years ago . For the past decade or so, I've been using the pattern below for my Java utility classes. The class contains only static methods and fields, is declared final so it can't be extended, and has a private constructor so it can't be instantiated. public final class SomeUtilityClass { public static final String SOME_CONSTANT = "Some constant"; private

Java 8 Stream - Reduce function's combiner not getting executed [duplicate]

一曲冷凌霜 提交于 2020-01-10 04:35:21
问题 This question already has answers here : Java8 stream.reduce() with 3 parameters - getting transparency (2 answers) Closed 3 years ago . I am using a simple reduce method with three arguments viz. identity, accumulator and combiner. Here is my code... Integer ageSumComb = persons .stream() .reduce(0, (sum, p) -> { System.out.println("Accumulator: Sum= "+ sum + " Person= " + p); return sum += p.age; }, (sum1, sum2) -> { System.out.format("Combiner: Sum1= " + sum1 + " Sum2= "+ sum2); return

arg max in Java 8 streams?

别说谁变了你拦得住时间么 提交于 2020-01-10 01:45:11
问题 I often need the maximum element of a collection according to the maximization of a criterion which produces a double or int value. Streams have the max() function which requires me to implement a comparator, which I find cumbersome. Is there a more concise syntax, such as names.stream().argmax(String::length) in the following example? import java.util.Arrays; import java.util.Comparator; import java.util.List; public class ArgMax { public static void main(String[] args) { List<String> names

Reference to methods with different parameters in Java8

∥☆過路亽.° 提交于 2020-01-10 01:13:28
问题 I'm wondering how does all this stuff with method references and functional interfaces works on lower level. The easiest example is where we have some List List<String> list = new ArrayList<>(); list.add("b"); list.add("a"); list.add("c"): Now we want to sort it with Collections class, so we can call: Collections.sort(list, String::compareToIgnoreCase); But if we define custom comparator it could be something like: Comparator<String> customComp = new MyCustomOrderComparator<>(); Collections

Converting Consumers to Functions

萝らか妹 提交于 2020-01-10 01:05:51
问题 Many lambdas for the Function interface take the form t -> { // do something to t return t; } I do this so often that I have written a method for it like this. static <T> Function<T, T> consumeThenReturn(Consumer<T> consumer) { return t -> { consumer.accept(t); return t; }; } This enables me to do really nice things like this: IntStream.rangeClosed('A', 'Z') .mapToObj(a -> (char) a) .collect(Collectors.collectingAndThen(Collectors.toList(), consumeThenReturn(Collections::shuffle))) .forEach

.forEach and .sort don't work and cannot set breakpoints in blocks

拥有回忆 提交于 2020-01-09 11:53:14
问题 I am using Java 8 (build 1.8.0_25), Netbeans 8.0.2 and am incorporating some of the Java 8 features into an existing app. Sorting and .forEach is not working so I have created some test code to ensure I understand lambdas, etc. and to diagnose the problem. Below is a mix of new code as well as code to interact with the data from my system: public void test(Registration reg) { /* new code */ List<String> family = new ArrayList<>(); family.add("Mom"); family.add("Dad"); family.add("Brother");