java-8

Java: Getting the generic type of a lambda

陌路散爱 提交于 2021-02-07 08:38:32
问题 First, I should say that I am able to get the generic type of instances or anonymous inner classes. Here is the code example, look for the catch block : public static interface MyInterface<E extends Throwable>{ void call() throws E; } public static <E extends Throwable> void method(MyInterface<E> lambda) throws E { try { lambda.call(); } catch(Throwable ex) { // Pseudo code Class<?> T = ((ParameterizedType)ex.getClass().getGenericInterfaces()[0]).getActualTypeArguments()[0]; if ( T.isInstance

Java: Getting the generic type of a lambda

最后都变了- 提交于 2021-02-07 08:37:03
问题 First, I should say that I am able to get the generic type of instances or anonymous inner classes. Here is the code example, look for the catch block : public static interface MyInterface<E extends Throwable>{ void call() throws E; } public static <E extends Throwable> void method(MyInterface<E> lambda) throws E { try { lambda.call(); } catch(Throwable ex) { // Pseudo code Class<?> T = ((ParameterizedType)ex.getClass().getGenericInterfaces()[0]).getActualTypeArguments()[0]; if ( T.isInstance

I want to calculate time in Asia/Dubai timezone from the time of Asia/Kolkata using java 8 Date Time API

跟風遠走 提交于 2021-02-07 08:16:23
问题 ZoneId dubai = ZoneId.of("Asia/Dubai"); LocalDate localDate = LocalDate.now(); LocalTime localTime = LocalTime.now(); ZonedDateTime zonedDateTime = ZonedDateTime.of(localDate, localTime, dubai); System.out.println("Dubai Tiime:"+zonedDateTime); Above code is still printing the time of my current zone (i.e Asia/Kolkata) Also i tried the following code to achieve the same but it is also printing time in my current zone(Asia/Kolkata): ZoneOffset offset = ZoneOffset.of("+04:00"); LocalDateTime

I want to calculate time in Asia/Dubai timezone from the time of Asia/Kolkata using java 8 Date Time API

拥有回忆 提交于 2021-02-07 08:15:10
问题 ZoneId dubai = ZoneId.of("Asia/Dubai"); LocalDate localDate = LocalDate.now(); LocalTime localTime = LocalTime.now(); ZonedDateTime zonedDateTime = ZonedDateTime.of(localDate, localTime, dubai); System.out.println("Dubai Tiime:"+zonedDateTime); Above code is still printing the time of my current zone (i.e Asia/Kolkata) Also i tried the following code to achieve the same but it is also printing time in my current zone(Asia/Kolkata): ZoneOffset offset = ZoneOffset.of("+04:00"); LocalDateTime

I want to calculate time in Asia/Dubai timezone from the time of Asia/Kolkata using java 8 Date Time API

大城市里の小女人 提交于 2021-02-07 08:14:07
问题 ZoneId dubai = ZoneId.of("Asia/Dubai"); LocalDate localDate = LocalDate.now(); LocalTime localTime = LocalTime.now(); ZonedDateTime zonedDateTime = ZonedDateTime.of(localDate, localTime, dubai); System.out.println("Dubai Tiime:"+zonedDateTime); Above code is still printing the time of my current zone (i.e Asia/Kolkata) Also i tried the following code to achieve the same but it is also printing time in my current zone(Asia/Kolkata): ZoneOffset offset = ZoneOffset.of("+04:00"); LocalDateTime

Find elements in a list that are not present in another list using java 8

ε祈祈猫儿з 提交于 2021-02-07 08:13:45
问题 I have 2 lists. The requirement is to filter out elements in list1 that are not in list2 based on condition. Class Fighter { String name; String address; } List<Fighter> pairs1 = new ArrayList(); pairs1.add(new Fighter("a", "a")); pairs1.add(new Fighter("b", "a")); List<Fighter> pairs2 = new ArrayList(); pairs2.add(new Fighter("a", "c")); pairs2.add(new Fighter("a", "d")); Set<Fighter> finalValues = new HashSet<>(); finalValues = pairs1.stream().filter(firstList -> pairs2.stream().noneMatch

Find elements in a list that are not present in another list using java 8

懵懂的女人 提交于 2021-02-07 07:59:29
问题 I have 2 lists. The requirement is to filter out elements in list1 that are not in list2 based on condition. Class Fighter { String name; String address; } List<Fighter> pairs1 = new ArrayList(); pairs1.add(new Fighter("a", "a")); pairs1.add(new Fighter("b", "a")); List<Fighter> pairs2 = new ArrayList(); pairs2.add(new Fighter("a", "c")); pairs2.add(new Fighter("a", "d")); Set<Fighter> finalValues = new HashSet<>(); finalValues = pairs1.stream().filter(firstList -> pairs2.stream().noneMatch

Nested collections lambda iteration

天涯浪子 提交于 2021-02-07 07:36:35
问题 Suppose I have an object containing a collection, each elements on the said collection contains a collection, and each collection contains a collection. And I want to iterate on the deepest objects and apply the same code to it. The imperative way is trivial, but is there a way to lambda-fy this all? Here is how the code looks today: My object o; SecretType computedThingy = 78; for (FirstLevelOfCollection coll : o.getList()) { for (SecondLevelOfCollection colColl : coll.getSet()) { for

java 8 optional to replace return null

廉价感情. 提交于 2021-02-07 07:36:34
问题 I am refactoring the code to Java 8 and I want to replace null checks with Optional. public Employee findEmployeeById(String id) { List<Employee> empList = .. //some db query return (empList.isEmpty() ? null : empList.get(0)); } Optional.ofNullable(empList.get(0)) won't work as when it will throw IndexOutofBoundException Or should I ideally replace null with Optional.empty() ? 回答1: As @Jesper already mentioned in the comments, you have to check whether the list is empty and then return an

Java type erasure - why can I see the type when I look at the bytecode?

蓝咒 提交于 2021-02-07 07:35:18
问题 I am trying to understand why writing both methods in a class is not allowed public bool plus(List<String>) {return true;} public bool plus(List<Integer>) {return true;} I try to figure how it is related to type erasure but when I decompile the following code public class Test<T> { boolean plus2(List<T> ss) {return false;} boolean plus(List<String> ss) {return false;} boolean plus(Set<Integer> ss) {return false;} } I get the same when I decompile it with Java decompiler (jd) Even when I print