java-8

How to read all lines of a file in parallel in Java 8

喜夏-厌秋 提交于 2019-12-28 05:58:14
问题 I want to read all lines of a 1 GB large file as fast as possible into a Stream<String> . Currently I'm using Files(path).lines() for that. After parsing the file, I'm doing some computations ( map() / filter() ) At first I thought this is already done in parallel, but it seems I'm wrong: When reading the file as it is, it takes about 50 seconds on my dual CPU laptop. However, if I split the file using bash commands and then process them in parallel, it only takes about 30 seconds. I tried

How to get a Stream from a float[]

痴心易碎 提交于 2019-12-28 05:57:08
问题 I was learning how to use java 8 streams when I noticed something weird. Arrays.stream() has methods for everything but float arrays : Arrays.stream(int[]) : IntStream Arrays.stream(long[]) : LongStream Arrays.stream(double[]) : DoubleStream Similarly, there are Stream implementations for int, double etc but not floats : IntStream LongStream DoubleStream Is there a reason for that? what is the recommended way to work with float streams? 回答1: from Java SE 8 for the Really Impatient by Cay S.

How to get a Stream from a float[]

馋奶兔 提交于 2019-12-28 05:57:02
问题 I was learning how to use java 8 streams when I noticed something weird. Arrays.stream() has methods for everything but float arrays : Arrays.stream(int[]) : IntStream Arrays.stream(long[]) : LongStream Arrays.stream(double[]) : DoubleStream Similarly, there are Stream implementations for int, double etc but not floats : IntStream LongStream DoubleStream Is there a reason for that? what is the recommended way to work with float streams? 回答1: from Java SE 8 for the Really Impatient by Cay S.

JavaFX on Linux is showing a “Graphics Device initialization failed for : es2, sw”

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-28 05:56:35
问题 I've just started coding/testing JavaFX stuff on Linux and I'm facing an error at time to start a simple app. I found many people concerning about that in Foruns but I could find a clear explanation about the reasons why it happens. I'd like to understand what is missing in my scenario to get it working. Any suggestion will be really apreciated. Env info: java version "1.8.0-ea" Java(TM) SE Runtime Environment (build 1.8.0-ea-b123) Java HotSpot(TM) Client VM (build 25.0-b65, mixed mode) Linux

Convert String array to Map using Java 8 Lambda expressions

我怕爱的太早我们不能终老 提交于 2019-12-28 05:56:35
问题 Is there a better functional way of converting an array of Strings in the form of "key:value" to a Map using the Java 8 lambda syntax? Arrays.asList("a:1.0", "b:2.0", "c:3.0") .stream() .map(elem -> elem.split(":") .collect(Collectors.toMap(keyMapper?, valueMapper?)); The solution I have right now does not seem really functional: Map<String, Double> kvs = new HashMap<>(); Arrays.asList("a:1.0", "b:2.0", "c:3.0") .stream() .map(elem -> elem.split(":")) .forEach(elem -> kvs.put(elem[0], Double

Setting timezone for maven unit tests on Java 8

[亡魂溺海] 提交于 2019-12-28 05:32:05
问题 How do I set the timezone for unit tests in maven surefire on Java 8? With Java 7 this used to work with systemPropertyVariables like in the following configuration, but with Java 8 the tests just use the system timezone. <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <configuration> <systemPropertyVariables> <user.timezone>UTC</user.timezone> </systemPropertyVariables> Why is that, and how do I fix it? 回答1: Short answer Java now reads user

Do Java8 lambdas maintain a reference to their enclosing instance like anonymous classes?

送分小仙女□ 提交于 2019-12-28 05:18:15
问题 We know that anonymous classes maintain a reference to their enclosing instance and that this can lead to context leaks on Android. Since retrolambda backports lambdas to Java7, it could be worth a try. It seems that Java8 lambdas do not have this problem, but I can't find any official information on that. Any clue? 回答1: Here is some info. From the following link http://cr.openjdk.java.net/~briangoetz/lambda/lambda-state-final.html: This has a beneficial implication for memory management:

Java 8 Collectors.toMap SortedMap

佐手、 提交于 2019-12-28 04:59:42
问题 I'm using Java 8 lambdas and want to use Collectors toMap to return a SortedMap . The best I can come up with is to call the following Collectors toMap method with a dummy mergeFunction and mapSupplier equal to TreeMap::new . public static <T, K, U, M extends Map<K, U>> Collector<T, ?, M> toMap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper, BinaryOperator<U> mergeFunction, Supplier<M> mapSupplier) { BiConsumer<M, T> accumulator = (map, element) ->

Unable to obtain OffsetDateTime from TemporalAccessor

淺唱寂寞╮ 提交于 2019-12-28 04:23:28
问题 When I do this String datum = "20130419233512"; DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss").withZone(ZoneId.of("Europe/Berlin")); OffsetDateTime datetime = OffsetDateTime.parse(datum, formatter); I get the following exception: java.time.format.DateTimeParseException: Text '20130419233512' could not be parsed: Unable to obtain OffsetDateTime from TemporalAccessor: {InstantSeconds=1366407312},ISO,Europe/Berlin resolved to 2013-04-19T23:35:12 of type java.time

Super class method and Interface default method conflict resolution

懵懂的女人 提交于 2019-12-28 04:13:06
问题 Consider the below example, public class Testing extends SupCls implements Intf { public static void main(String[] args) { new Testing().test(); } } class SupCls { public void test() { System.out.println("From SupCls"); } } interface Intf { public default void test() { System.out.println("From Intf"); } } As you can see, there's no connection between SupCls class and Intf interface. But both are defining a common method. And Testing class is extending SupCls and implementing Intf . So, when I