Error when using LogManager (l4j2) with Java 8 (java.lang.reflect.AnnotatedElement cannot be resolved)

When using JDK 8 and an IDE (or any other code processing tool/framework) with its own compiler, like Eclipse, you have to update the tool to a version with Java 8 support, even if you are not using the newer Java 8 features. The reason is that the compiler must be able to load the newer class files … Read more

PermGen elimination in JDK 8

Reasons of ignoring these argument is permanent generation has been removed in HotSpot for JDK8 because of following drawbacks Fixed size at startup – difficult to tune. Internal Hotspot types were Java objects : Could move with full GC, opaque, not strongly typed and hard to debug, needed meta-metadata. Simplify full collections : Special iterators … Read more

Group by multiple field names in java 8

You have a few options here. The simplest is to chain your collectors: Map<String, Map<Integer, List<Person>>> map = people .collect(Collectors.groupingBy(Person::getName, Collectors.groupingBy(Person::getAge)); Then to get a list of 18 year old people called Fred you would use: map.get(“Fred”).get(18); A second option is to define a class that represents the grouping. This can be inside Person. This … Read more

Collect successive pairs from a stream

The Java 8 streams library is primarily geared toward splitting streams into smaller chunks for parallel processing, so stateful pipeline stages are quite limited, and doing things like getting the index of the current stream element and accessing adjacent stream elements are not supported. A typical way to solve these problems, with some limitations, of … Read more

Comparator.reversed() does not compile using lambda

This is a weakness in the compiler’s type inferencing mechanism. In order to infer the type of u in the lambda, the target type for the lambda needs to be established. This is accomplished as follows. userList.sort() is expecting an argument of type Comparator<User>. In the first line, Comparator.comparing() needs to return Comparator<User>. This implies … Read more

How will Java lambda functions be compiled?

The VM decides how to implement lambda, not a compiler. See Translation strategy section in Translation of Lambda Expressions. Instead of generating bytecode to create the object that implements the lambda expression (such as calling a constructor for an inner class), we describe a recipe for constructing the lambda, and delegate the actual construction to … Read more