java-8

Android N Java8 java.time

混江龙づ霸主 提交于 2020-01-09 05:21:32
问题 I updated to the latest Android N sdk. The only thing I don't understand is why I cannot import java.time into my code? I thought Java8 is available through Android N. Then why didn't Google add java.time package? 回答1: Android N is not supporting all the features of Java 8. Following features are only supported: Default and static interface methods Lambda expressions Repeatable annotations Reflection and language-related APIs: java.lang.FunctionalInterface java.lang.annotation.Repeatable java

How can I mock java.time.LocalDate.now()

二次信任 提交于 2020-01-09 04:22:50
问题 In my test case, I need test time sensitive method, in that method we're using java 8 class LocalDate, it is not Joda . What can I do to change time, when I'm running test 回答1: In your code, replace LocalDate.now() with LocalDate.now(clock);. You can then pass Clock.systemDefaultZone() for production and a fixed clock for testing. This is an example : First, inject the Clock . If you are using spring boot just do a : @Bean public Clock clock() { return Clock.systemDefaultZone(); } Second,

Java 8: Reference to [method] is ambiguous [duplicate]

这一生的挚爱 提交于 2020-01-09 03:43:06
问题 This question already has answers here : Java type inference: reference is ambiguous in Java 8, but not Java 7 (2 answers) Closed 4 years ago . Does anybody understand why the following code will compile fine in Java 7 and below, but fails with Java 8. public static void main(String[] args) throws Exception { put(get("hello")); } public static <R> R get(String d) { return (R)d; } public static void put(Object o) { System.err.println("Object " + o); } public static void put(CharSequence c) {

Java 8 lambdas group list into map

时光总嘲笑我的痴心妄想 提交于 2020-01-08 17:13:10
问题 I want to take a List<Pojo> and return a Map<String, List<Pojo>> where the Map 's key is a String value in Pojo , let's call it String key . To clarify, given the following: Pojo 1: Key:a value:1 Pojo 2: Key:a value:2 Pojo 3: Key:b value:3 Pojo 4: Key:b value:4 I want a Map<String, List<Pojo>> with keySet() sized 2, where key "a" has Pojos 1 and 2, and key "b" has pojos 3 and 4. How could I best achieve this using Java 8 lambdas? 回答1: It seems that the simple groupingBy variant is what you

Java how to add time and date

不想你离开。 提交于 2020-01-08 06:37:07
问题 Using JDK 1.8 I have Time in ms for a day (not since 1970) and I have a Date how do I add the two and create a datetime. Thanks Tried @Kikos soln does not produce correct result: Somehow my orig time 930 hrs in this case changes to 9:40, the orig date itself should not have any time gets time (??) - so the addition math fails. String testdate = "2015/10/25"; Date date = new SimpleDateFormat("yyyy/mm/dd").parse(testdate); String timehrs= "930"; long ltime = Long.parseLong("930"); long

String to 2D Array with streams [Java 8]

一个人想着一个人 提交于 2020-01-07 14:47:21
问题 I am currently learning how to do streams, while doing some assignments for fun (Advent of code). While I know how how to do it with temporary arrays, I do not know the correct syntax for streams or if it is even possible to do it in one line. public class M3 { public static void main(String[] args) { String s = "104\t240\t147\t246\t123\t175\t372\t71\t116\t230\t260\t118\t202\t270\t277\t292\n" + "740\t755\t135\t205\t429\t822\t844\t90\t828\t115\t440\t805\t526\t91\t519\t373\n" + "1630\t991\t1471

How to get the sum from nested stream

喜你入骨 提交于 2020-01-07 08:48:38
问题 I am new to Java 8 streams, I am unable to get the correct output from the nested loop operational sum. In the nested loop, I am calling a method which is returning an integer; by this doing finally summing up the resultant total This is what I tried: import java.util.ArrayList; import java.util.List; import java.util.stream.IntStream; public class NestedLoop { public void callTask(int i, int j) { System.out.println("i:"+i+"j:"+j); } public int getCount(Count count, String j) { if(count

HashMap storing same values for all Keys

时光怂恿深爱的人放手 提交于 2020-01-07 08:27:11
问题 I want to store different values for unique coordinates.I am using integer array to store those values in HashMap to corresponding coordinates but every key maps to last calculated value. Code : import java.util.*; import java.awt.Point; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Set; public class Solution { @SuppressWarnings("empty-statement") public static void main(String[] args) { Scanner

JavaDoc - Undeclared Type Variable

北城以北 提交于 2020-01-07 04:57:27
问题 I think everybody who has to work with Maven and Java8 knows of this bug that release builds suddenly fail for spelling mistakes in JavaDoc. As a company we decided to let some poor sap (aka me) work all of them out. Now I'm stuck with the following "error": [ERROR] Failed to execute goal org.apache.maven.plugins:maven-javadoc-plugin:2.10.1:javadoc (default-cli) on project org.acme.project.demo: An error has occurred in JavaDocs report generation: [ERROR] Exit code: 1 - C:\jenkins\workspace

Code compiler error between IDE language level 7 and 8

心不动则不痛 提交于 2020-01-07 04:15:25
问题 List<Restriction> restrictions = new ArrayList<>(); //list operation....... when(restrictionUpdator.executeBatch(anyInt(), any(List.class), any(RequestorInfo.class), (Class<Obj>) any()) ).thenReturn(restrictions); excuteBatch method: public <T extends Obj> List<T> executeBatch(int Id, List<UpdateContext> contexts, RequestorInfo requestor, Class<T> tClass) Class: public class Restriction extends Obj Can someone have a look? Why above code has compiler error in IDE language 8? If I change IDE