java-8

Java 8 Lambdas - equivalent of c# OfType

不羁的心 提交于 2020-01-12 13:50:10
问题 I am learning the new java 8 features now, after 4 years exclusively in C# world, so lambdas are on top for me. I am now struggling to find an equivalent for C#'s "OfType" method. What I have is a List myNodes , I want to get a List out of it, where Node is an interface, and SpecificNode is implementing it. In C# it would be IList<INode> myNodes = new List<INodes>(){new SpecificNode(), new OtherNode()} IList<SpecificNode> specificNodes = myNodes.OfType<SpecificNode>() 回答1: There is no exact

Java 8 Lambdas - equivalent of c# OfType

随声附和 提交于 2020-01-12 13:50:09
问题 I am learning the new java 8 features now, after 4 years exclusively in C# world, so lambdas are on top for me. I am now struggling to find an equivalent for C#'s "OfType" method. What I have is a List myNodes , I want to get a List out of it, where Node is an interface, and SpecificNode is implementing it. In C# it would be IList<INode> myNodes = new List<INodes>(){new SpecificNode(), new OtherNode()} IList<SpecificNode> specificNodes = myNodes.OfType<SpecificNode>() 回答1: There is no exact

Map values in Collectors.groupingBy()

旧城冷巷雨未停 提交于 2020-01-12 13:43:08
问题 For the sake of this example, let's assume I have a simple type Tuple with two attributes: interface Tuple<T, U> { T getFirst(); U getSecond(); } Now I want to transform a collection of (first, second) tuples into a map which maps each first value to a set of all second values contained in tuples with that specific first value. The method groupSecondByFirst() shows a possible implementation doing what I want: <T, U> Map<T, Set<U>> groupSecondByFirst(Set<Tuple<T, U>> tuples) { Map<T, Set<U>>

Map values in Collectors.groupingBy()

只愿长相守 提交于 2020-01-12 13:42:49
问题 For the sake of this example, let's assume I have a simple type Tuple with two attributes: interface Tuple<T, U> { T getFirst(); U getSecond(); } Now I want to transform a collection of (first, second) tuples into a map which maps each first value to a set of all second values contained in tuples with that specific first value. The method groupSecondByFirst() shows a possible implementation doing what I want: <T, U> Map<T, Set<U>> groupSecondByFirst(Set<Tuple<T, U>> tuples) { Map<T, Set<U>>

Lambda reference to a field

▼魔方 西西 提交于 2020-01-12 11:09:54
问题 I'd like to know how to get lambda reference to a field. I don't want to use a method because my field is public final. I suspect this is impossible but I don't see an obvious statement. class A { public final String id; ... } Map<String, A> f(List<A> l) { return l.stream().collect(Collectors.toMap(A::id, Function.identity())); } 回答1: You can always use a lambda expression: return l.stream().collect(Collectors.toMap(a -> a.id, Function.identity())); I think that "method references" are called

ZonedDateTime change behavior jdk 8/11

≡放荡痞女 提交于 2020-01-12 10:28:19
问题 I am migrating an application from jdk 8 to 11 and I can see ZonedDateTime change is behavior about daylight saving time. JDK8 ZonedDateTime parse = ZonedDateTime.parse("2037-05-10T19:15:00.000+01:00[Europe/Paris]"); System.out.println(parse); output: 2037-05-10T19:15+02:00[Europe/Paris] JDK11/12 ZonedDateTime parse = ZonedDateTime.parse("2037-05-10T19:15:00.000+01:00[Europe/Paris]"); System.out.println(parse); 2037-05-10T20:15+02:00[Europe/Paris] Can someone explain to me why did they change

ZonedDateTime change behavior jdk 8/11

穿精又带淫゛_ 提交于 2020-01-12 10:27:12
问题 I am migrating an application from jdk 8 to 11 and I can see ZonedDateTime change is behavior about daylight saving time. JDK8 ZonedDateTime parse = ZonedDateTime.parse("2037-05-10T19:15:00.000+01:00[Europe/Paris]"); System.out.println(parse); output: 2037-05-10T19:15+02:00[Europe/Paris] JDK11/12 ZonedDateTime parse = ZonedDateTime.parse("2037-05-10T19:15:00.000+01:00[Europe/Paris]"); System.out.println(parse); 2037-05-10T20:15+02:00[Europe/Paris] Can someone explain to me why did they change

Solve no final variable inside Java 8 Stream

萝らか妹 提交于 2020-01-12 09:58:10
问题 Is there is a way to convert the following code to Java 8 Stream. final List ret = new ArrayList(values.size()); double tmp = startPrice; for (final Iterator it = values.iterator(); it.hasNext();) { final DiscountValue discountValue = ((DiscountValue) it.next()).apply(quantity, tmp, digits, currencyIsoCode); tmp -= discountValue.getAppliedValue(); ret.add(discountValue); } Java 8 streams complains about no final variable tmp ? Is there a way to solve such situations ? Local variable tmp

Is Method area still present in Java 8?

孤街醉人 提交于 2020-01-12 09:16:56
问题 Prior to Java 8 we had 5 major runtime data areas: Method Area Heap JVM Stacks PC registers Native method stacks With Java 8, there is no Perm Gen, that means there is no more “java.lang.OutOfMemoryError: PermGen” which is great but I also read Method Area is part of space in the Perm Gen but I can't seem to find anything which explicitly says Method area is no more in Java 8. So is Perm Gen along with Method area got removed or only Perm Gen got removed and Method area is still present in

Is Method area still present in Java 8?

荒凉一梦 提交于 2020-01-12 09:15:30
问题 Prior to Java 8 we had 5 major runtime data areas: Method Area Heap JVM Stacks PC registers Native method stacks With Java 8, there is no Perm Gen, that means there is no more “java.lang.OutOfMemoryError: PermGen” which is great but I also read Method Area is part of space in the Perm Gen but I can't seem to find anything which explicitly says Method area is no more in Java 8. So is Perm Gen along with Method area got removed or only Perm Gen got removed and Method area is still present in