java-8

Parsing an ISO 8601 date in Java8 when all fields (including separators, but not including years) are optional

耗尽温柔 提交于 2020-01-21 09:07:28
问题 I have a requirement for parsing ISO8601 formatted strings in Java with various levels of accuracy. Some examples of the string I need to parse are: 2018 2018-10 2018-10-15 2018-10-15T12:00 2018-10-15T12:00:30 2018-10-15T12:00:30.123 20181015 201810151200 20181015120030 20181015120030.123 20181015T12:00:30.123 Where I don't have a field then I am free to assume the lowest valid value that applies (for example, if the month is missing I can assume January, if the day is missing then assume the

Parsing an ISO 8601 date in Java8 when all fields (including separators, but not including years) are optional

佐手、 提交于 2020-01-21 09:07:24
问题 I have a requirement for parsing ISO8601 formatted strings in Java with various levels of accuracy. Some examples of the string I need to parse are: 2018 2018-10 2018-10-15 2018-10-15T12:00 2018-10-15T12:00:30 2018-10-15T12:00:30.123 20181015 201810151200 20181015120030 20181015120030.123 20181015T12:00:30.123 Where I don't have a field then I am free to assume the lowest valid value that applies (for example, if the month is missing I can assume January, if the day is missing then assume the

Java - Use predicate without lambda expressions

二次信任 提交于 2020-01-21 08:28:21
问题 I've below requirement:- Employee.java public boolean isAdult(Integer age) { if(age >= 18) { return true; } return false; } Predicate.java private Integer age; Predicate<Integer> isAdult; public PredicateAnotherClass(Integer age, Predicate<Integer> isAdult) { this.age = age; System.out.println("Test result is "+ isAdult(age)); } public void testPredicate() { System.out.println("Calling the method defined in the manager class"); } Now My goal is to test whether the age which i pass to

java8 stream of arrays to 2 dimensional array

夙愿已清 提交于 2020-01-21 07:39:06
问题 I'm new to Java8 and I can't use streams to map one array into another 2 dimensional array. I have one 2-dimensional array which is a pattern: boolean[][] pattern = { {true, true, false}, {true, false, true}, {false, true, true} }; And second array which contains keys. 0 means: take 0-element from pattern 1 means: take 1-element from pattern and so on int[] keys = {2, 1, 0}; From these 2 arrays I'd like to produce another 2-dimensional array. In this case the result will look like this:

List of objects to map in java 8

北城以北 提交于 2020-01-21 07:23:35
问题 I have a class like this ObjectA id type where id is unique but the type can be duplicate so if I have a list of ObjectA like this (id, type) = (1, "A") , (2, "B"), (3, "C"), (4, "A") then I want Map<type, List<id>> where map is < "A",[1,4], "B",[2], "C",[3] > It is working with Java 7 but can't find a way to do it in Java 8. In Java 8 I can create key, value pair but not able to create a list if a type is same. 回答1: yourTypes.stream() .collect(Collectors.groupingBy( ObjectA::getType,

List of objects to map in java 8

谁说胖子不能爱 提交于 2020-01-21 07:23:25
问题 I have a class like this ObjectA id type where id is unique but the type can be duplicate so if I have a list of ObjectA like this (id, type) = (1, "A") , (2, "B"), (3, "C"), (4, "A") then I want Map<type, List<id>> where map is < "A",[1,4], "B",[2], "C",[3] > It is working with Java 7 but can't find a way to do it in Java 8. In Java 8 I can create key, value pair but not able to create a list if a type is same. 回答1: yourTypes.stream() .collect(Collectors.groupingBy( ObjectA::getType,

How can I make a DateTimeFormatter that accepts trailing junk?

て烟熏妆下的殇ゞ 提交于 2020-01-21 06:51:54
问题 I'm retrofitting old some SimpleDateFormat code to use the new Java 8 DateTimeFormatter . SimpleDateFormat , and thus the old code, accepts strings with stuff in them after the date like "20130311nonsense". The DateTimeFormat I created throws a DateTimeParseException for these strings, which is probably the right thing to do, but I'd like to maintain compatibility. Can I modify my DateTimeFormat to accept these strings? I'm currently creating it like this: DateTimeFormatter.ofPattern(

How can I make a DateTimeFormatter that accepts trailing junk?

橙三吉。 提交于 2020-01-21 06:50:51
问题 I'm retrofitting old some SimpleDateFormat code to use the new Java 8 DateTimeFormatter . SimpleDateFormat , and thus the old code, accepts strings with stuff in them after the date like "20130311nonsense". The DateTimeFormat I created throws a DateTimeParseException for these strings, which is probably the right thing to do, but I'd like to maintain compatibility. Can I modify my DateTimeFormat to accept these strings? I'm currently creating it like this: DateTimeFormatter.ofPattern(

How to filter only specific elements by Java 8 Predicate?

﹥>﹥吖頭↗ 提交于 2020-01-21 05:18:38
问题 I have collection List<Foo> of Foo elements: class Foo { private TypeEnum type; private int amount; //getters, setters ... } Foo type can be TypeEnum.A and TypeEnum.B . I would like to get only those Foo elements from list which if the element have type == TypeEnum.B then amount is greater than zero ( amount > 0 ). How can I do it by Java 8 Streams filter() method? If I use: List<Foo> l = list.stream() .filter(i -> i.getType().equals(TypeEnum.B) && i.getAmount() > 0) .collect(Collectors.<Foo

Is there any advantage of calling map after mapToInt, where ever required

拈花ヽ惹草 提交于 2020-01-21 04:17:45
问题 I am trying to calculate the sum of squares of values in the list. Below are three variations which all calculates the required value. I want to know which one is the most efficient. I am expecting the third one to be more efficient as auto boxing is only done once. // sum of squares int sum = list.stream().map(x -> x * x).reduce((x, y) -> x + y).get(); System.out.println("sum of squares: " + sum); sum = list.stream().mapToInt(x -> x * x).sum(); System.out.println("sum of squares: " + sum);