java-8

Java 8 Path Stream and FileSystemException (Too many open files)

断了今生、忘了曾经 提交于 2020-02-14 08:03:30
问题 geniuses! I'm practicing Java 8. So if I do something like this: Files.walk(Paths.get(corpusPathStr)) .filter(path -> path.toFile().isFile()) .forEach(path -> { try { Files.lines(path) .forEach(...); } catch (IOException e) { e.printStackTrace(); } }); I got FileSystemException error. If I open a file under forEach, may too many files be opened? Or are there other reasons causing FileSystemException (Too many open files)? Thanks for your help in advance! 回答1: Use try(Stream<Path> stream =

Java 8 Path Stream and FileSystemException (Too many open files)

风流意气都作罢 提交于 2020-02-14 08:03:10
问题 geniuses! I'm practicing Java 8. So if I do something like this: Files.walk(Paths.get(corpusPathStr)) .filter(path -> path.toFile().isFile()) .forEach(path -> { try { Files.lines(path) .forEach(...); } catch (IOException e) { e.printStackTrace(); } }); I got FileSystemException error. If I open a file under forEach, may too many files be opened? Or are there other reasons causing FileSystemException (Too many open files)? Thanks for your help in advance! 回答1: Use try(Stream<Path> stream =

Java 8 lambda create list of Strings from list of objects

依然范特西╮ 提交于 2020-02-13 04:55:32
问题 I have the following qustion: How can I convert the following code snipped to Java 8 lambda style? List<String> tmpAdresses = new ArrayList<String>(); for (User user : users) { tmpAdresses.add(user.getAdress()); } Have no idea and started with the following: List<String> tmpAdresses = users.stream().map((User user) -> user.getAdress()); 回答1: You need to collect your stream into a List: List<String> adresses = users.stream() .map(User::getAdress) .collect(Collectors.toList()); For more

Java 8 lambda create list of Strings from list of objects

你离开我真会死。 提交于 2020-02-13 04:55:30
问题 I have the following qustion: How can I convert the following code snipped to Java 8 lambda style? List<String> tmpAdresses = new ArrayList<String>(); for (User user : users) { tmpAdresses.add(user.getAdress()); } Have no idea and started with the following: List<String> tmpAdresses = users.stream().map((User user) -> user.getAdress()); 回答1: You need to collect your stream into a List: List<String> adresses = users.stream() .map(User::getAdress) .collect(Collectors.toList()); For more

Is Java 8 missing an OptionalBoolean?

為{幸葍}努か 提交于 2020-02-12 19:14:14
问题 As a primitive version of Optional*, Java 1.8 provides OptionalInt, OptionalLong and OptionalDouble. But I cannot find the equivalent OptionalBoolean class. Are there any technical reasons against having an OptionalBoolean ? * An Optional may or may not have the presence of a value, is used as an alternative to null . 回答1: This quote explains the considerations behind having primitive streams. I'm assuming the same applied to primitive Optionals. In short, primitive streams (and probably

JSR 310 :: System.currentTimeMillis() vs Instant.toEpochMilli() :: TimeZone

旧城冷巷雨未停 提交于 2020-02-11 06:21:08
问题 Could you please shed some light on how to obtain correct epoch time in milliseconds for a default system timezone and given timezone. Given 1. TimeZone: GMT+3 2. The following code snippet: import java.time.*; public class Main { public static void main(String[] args) { System.out.println(LocalDateTime .now() .atZone(ZoneOffset.UTC) .toInstant() .toEpochMilli() ); System.out.println(LocalDateTime .now() .atZone(ZoneOffset.of("+3")) .toInstant() .toEpochMilli() ); System.out.println(System

Is it possible to retrieve lambda expression at runtime

≯℡__Kan透↙ 提交于 2020-02-10 15:02:53
问题 I was playing with Java8 Lambda last night and I was wondering if it is possible to retrieve the Lambda expression at runtime. In short and as far as I understood, Lambda expression are converted into (static) methods at runtime and then called using InvokeDynamics. Let's take an example like this: people.filter(person -> person.getAge() >= minAge); where filter would be a custom method taking a Predicate<T> as a parameter. Inside this filter method, how could I retrieve the argument in a

Split a collection of objects based on a condition

老子叫甜甜 提交于 2020-02-06 07:52:25
问题 I have a list of objects to be added in a bag and bags capacity is 100 qty The Object and Bag look like below public class MyObject{ String id; int qty; } public class MyBag{ String id; int qty; } Is there any way to split MyObject in multiple MyBags grouping on the qty limit using Java 8 streams For example: myObjects are [myObject1:{id1, 150}, myObject2:{id2, 30}, myObject3:{id3, 150}] Since bag has a capacity of 100. Bags should be grouped as [ bag1:[{id1, 100}], bag2:[{id1, 50},{id2, 30},

Split a collection of objects based on a condition

送分小仙女□ 提交于 2020-02-06 07:52:24
问题 I have a list of objects to be added in a bag and bags capacity is 100 qty The Object and Bag look like below public class MyObject{ String id; int qty; } public class MyBag{ String id; int qty; } Is there any way to split MyObject in multiple MyBags grouping on the qty limit using Java 8 streams For example: myObjects are [myObject1:{id1, 150}, myObject2:{id2, 30}, myObject3:{id3, 150}] Since bag has a capacity of 100. Bags should be grouped as [ bag1:[{id1, 100}], bag2:[{id1, 50},{id2, 30},

Split a collection of objects based on a condition

╄→гoц情女王★ 提交于 2020-02-06 07:52:05
问题 I have a list of objects to be added in a bag and bags capacity is 100 qty The Object and Bag look like below public class MyObject{ String id; int qty; } public class MyBag{ String id; int qty; } Is there any way to split MyObject in multiple MyBags grouping on the qty limit using Java 8 streams For example: myObjects are [myObject1:{id1, 150}, myObject2:{id2, 30}, myObject3:{id3, 150}] Since bag has a capacity of 100. Bags should be grouped as [ bag1:[{id1, 100}], bag2:[{id1, 50},{id2, 30},