java-8

Multiple aggregate functions in Java 8 Stream API

时光总嘲笑我的痴心妄想 提交于 2020-01-01 04:55:23
问题 I have a class defined like public class TimePeriodCalc { private double occupancy; private double efficiency; private String atDate; } I would like to perform the following SQL statement using Java 8 Stream API. SELECT atDate, AVG(occupancy), AVG(efficiency) FROM TimePeriodCalc GROUP BY atDate I tried : Collection<TimePeriodCalc> collector = result.stream().collect(groupingBy(p -> p.getAtDate(), .... What can be put into the code to select multiple attributes ? I'm thinking of using multiple

Android Studio: lambda does not work [duplicate]

别来无恙 提交于 2020-01-01 04:35:07
问题 This question already has answers here : Is it possible to use Java 8 for Android development? (24 answers) Closed 4 years ago . When trying to use lambda expressions, I got some Gradle build errors: Error:(41, 100) error: lambda expressions are not supported in -source 1.7 (use -source 8 or higher to enable lambda expressions) Error:Execution failed for task ':app:compileDebugJava'. Compilation failed; see the compiler error output for details. A quick search helped me understand that lambda

How to interleave (merge) two Java 8 Streams?

こ雲淡風輕ζ 提交于 2020-01-01 04:26:14
问题 Stream<String> a = Stream.of("one", "three", "five"); Stream<String> b = Stream.of("two", "four", "six"); What do I need to do for the output to be the below? // one // two // three // four // five // six I looked into concat but as the javadoc explains, it just appends one after the other, it does not interleave / intersperse. Stream<String> out = Stream.concat(a, b); out.forEach(System.out::println); Creates a lazily concatenated stream whose elements are all the elements of the first

Streams in Java, can't figure this code out

强颜欢笑 提交于 2020-01-01 04:11:05
问题 I've found the following code snippet: Function<Integer, Predicate<Integer>> smallerThan = x -> y -> y < x; List<Integer> l = Arrays.asList(5, 6, 7, 23, 4, 5645, 6, 1223, 44453, 60182, 2836, 23993, 1); List<Integer> list2 = l.stream() .filter(smallerThan.apply(l.get(0))) .collect(Collectors.toList()); System.out.println(list2); As output I receive: [4, 1] How does the smallerThan function in this example work, considering that we only pass one parameter smallerThan.apply(l.get(0)) ? 回答1:

“Good” method to call method on each object using Stream API

假装没事ソ 提交于 2020-01-01 04:08:10
问题 Is it possible to run a method, in a consumer, like a method reference, but on the object passed to the consumer: Arrays.stream(log.getHandlers()).forEach(h -> h.close()); would be a thing like: Arrays.stream(log.getHandlers()).forEach(this::close); but that's not working... Is there a possibility with method references, or is x -> x.method() the only way working here? 回答1: You don't need this . YourClassName::close will call the close method on the object passed to the consumer : Arrays

Arrow (->) operator precedence/priority is lowest, or priority of assignment/combined assignment is lowest?

蹲街弑〆低调 提交于 2020-01-01 04:01:31
问题 JLS: The lowest precedence operator is the arrow of a lambda expression (->) , followed by the assignment operators. Followed in which direction (increasing priority, decreasing priority)? - "followed" means assignment has higher priority or lower priority (with respect to arrow operator)? I guess, in increasing, because "lowest" (for arrow) means absolutely lowest. As I understand, arrow (->) should be at the very bottom of this Princeton operator precedence table (that is below all

TLS handshake fails between a Java 1.8 client and a Java 1.7 TLS 1.1 server running in FIPS mode, even after disabling TLS 1.2 in the client side

百般思念 提交于 2020-01-01 03:21:07
问题 The SSL / TLS handshake between a "Java 1.7 TLS 1.1 server" and a "Java 1.8 client" fails in my environment with the following exception on the server side: java.security.NoSuchAlgorithmException: no such algorithm: SunTls12MasterSecret for provider SunPKCS11-NSSfips Following are the details of the server and the client in my environment: Server: The server uses Java 1.7u45 and is running in FIPS 140 compliant mode as mentioned in http://docs.oracle.com/javase/7/docs/technotes/guides

Understanding 'TypeElement' and 'DeclaredType' interface in java

坚强是说给别人听的谎言 提交于 2020-01-01 02:52:12
问题 One usage of these two interface, is to write annotation processor. As a java beginner, I find the level of indirection that is added by these two packages: javax.lang.model.element & javax.lang.model.type to provide metadata about java interface and java class confusing. ......... java doc comments say, TypeElement represents a class or interface program element. Provides access to information about the type and its members. Note that an enum type is a kind of class and an annotation type is

3 ways to flatten a list of lists. Is there a reason to prefer one of them?

本小妞迷上赌 提交于 2020-01-01 02:01:06
问题 Assume we have a list as follows. CoreResult has a field of type List<Double> . final List<CoreResult> list = new LinkedList<>(SOME_DATA); The objective is to flatten the list after extracting that specific field from each CoreResult object. Here are 3 possible options. Is any one of them preferable to the others? Option 1 : extract a field via map() and flatten inside collector final List<Double> A = list.stream().map(CoreResult::getField) .collect(ArrayList::new, ArrayList::addAll,

Avro with Java 8 dates as logical type

徘徊边缘 提交于 2020-01-01 01:55:08
问题 Latest Avro compiler (1.8.2) generates java sources for dates logical types with Joda-Time based implementations. How can I configure Avro compiler to produce sources that used Java 8 date-time API? 回答1: Currently (avro 1.8.2) this is not possible. It's hardcoded to generate Joda date/time classes. The current master branch has switched to Java 8 and there is an open issue (with Pull Request) to add the ability to generate classes with java.time.* types. I have no idea on any kind of release