java-8

Lambda 'special void-compatibility rule' - statement expression

冷暖自知 提交于 2019-12-27 11:05:39
问题 Im reading Java 8 in Action. In section 3.5.2 there is a paragraph about 'void-compatibility rule': If a lambda has a statement expression as its body, it’s compatible with a function descriptor that returns void (provided the parameter list is compatible too). For example, both of the following lines are legal even though the method add of a List returns a boolean and not void as expected in the Consumer context (T -> void): // Predicate has a boolean return Predicate<String> p = s -> list

Format Instant to String

泄露秘密 提交于 2019-12-27 11:02:50
问题 I'm trying to format an Instant to a String using the new java 8 time-api and a pattern: Instant instant = ...; String out = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(instant); Using the code above I get an Exception which complains an unsupported field: java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: YearOfEra at java.time.Instant.getLong(Instant.java:608) at java.time.format.DateTimePrintContext.getValue(DateTimePrintContext.java:298) ... 回答1: Time

Why does a Java method reference with return type match the Consumer interface?

无人久伴 提交于 2019-12-27 09:04:09
问题 I am confused by the following code class LambdaTest { public static void main(String[] args) { Consumer<String> lambda1 = s -> {}; Function<String, String> lambda2 = s -> s; Consumer<String> lambda3 = LambdaTest::consume; // but s -> s doesn't work! Function<String, String> lambda4 = LambdaTest::consume; } static String consume(String s) { return s;} } I would have expected the assignment of lambda3 to fail as my consume method does not match the accept method in the Consumer Interface - the

Java8 : how to aggregate objects from a stream?

混江龙づ霸主 提交于 2019-12-25 18:37:34
问题 Edit IMHO : I think it is not a duplicate because the two questions are trying to solve the problem in different ways and especially because they provide totally different technological skills (and finally, because I ask myself these two questions). Question How to aggregate items from an ordered stream, preferably in an intermediate operation ? Context Following my other question : Java8 stream lines and aggregate with action on terminal line I've got a very large file of the form : MASTER

Convert List of List of Object to a map - using lambdas in java 8

妖精的绣舞 提交于 2019-12-25 18:19:21
问题 I have object structure like below: Order { int code; int id; List<Item>; } Item { int code; int quantity; List<Suborder>; } Suborder { int code; int quantity; } I have an object of O and I want a map from code to B. Whats the correct way to do this? What I tried : 1 - Not working order.getOrderItems().stream().flatMap(l -> l.getOrderItemSuborder().stream()).collect(Collectors.toMap(x -> x.getCode() , Function.identity())); // x.getCode() seems to be not available here :( 2 - Working order

how can i get value through complex logic from list

谁说胖子不能爱 提交于 2019-12-25 17:26:19
问题 I have filter personCountFilter=3 , and have list as below: Rate{ PersonCount:1, LOS:1} Rate{ PersonCount:1, LOS:2} Rate{ PersonCount:1, LOS:3} Rate{ PersonCount:2, LOS:1} Rate{ PersonCount:2, LOS:2} Rate{ PersonCount:2, LOS:3} Rate{ PersonCount:3, LOS:2} Rate{ PersonCount:3, LOS:4} Rate{ PersonCount:3, LOS:5} Rate{ PersonCount:3, LOS:6} Rate{ PersonCount:4, LOS:3} Rate{ PersonCount:5, LOS:7} Rate{ PersonCount:6, LOS:7} After filter my expected: Rate{ PersonCount:2, LOS:1} Rate{ PersonCount:3

How to use String.format in java 8?

試著忘記壹切 提交于 2019-12-25 17:15:17
问题 Today I wrote a simple program in eclipse Kepler in java 8. Actually, I copied it from some video tutorial. In that tutorial, it ran, but in my computer it didn't. Error line is String.format("%02d:%02d:%02d",hour,minute,second); I don't understand what the error is here. It highlights the method format(String,object[]) in the type String are not applicable for the argument(String, int, int, int) public class Demo { private int hour; private int second; private int minute; public void setTime

Java-8 simplify lambda expression with embedded Streams

眉间皱痕 提交于 2019-12-25 09:15:16
问题 Is there a more simple and performant way of doing this, At the end I would need a list of scheduleContainers (List<ScheduleContainer>) final List<ScheduleResponseContent> scheduleResponseContents = new ArrayList<>(); scheduleResponseWrappers.parallelStream().forEach(srw -> scheduleResponseContents.addAll(srw.getScheduleResponseContents())); final List<List<ScheduleContainer>> schedulesOfWeek = new ArrayList<>(); scheduleResponseContents.parallelStream().forEach(src -> schedulesOfWeek.addAll

Severe exception on servlet with java.lang.NoClassDefFoundError

我的梦境 提交于 2019-12-25 09:03:41
问题 I get this exception on my tomcat8 running on this java version OpenJDK Runtime Environment (build 1.8.0_121-8u121-b13-0ubuntu1.16.04.2-b13) SEVERE [http-nio-80-exec-9] org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [restSdkService] in context with path [/RestService] threw exception [java.lang.NoClassDefFoundError: Could not initialize class com.example.bean.SomeBean] with root cause java.lang.NoClassDefFoundError: Could not initialize class com.example

Why Entitys uninitialized collection is initialized automatically only for Entities persisted before current transaction?

醉酒当歌 提交于 2019-12-25 08:53:50
问题 (Please feel free to edit the title after reading this question) I have quite simple @ManyToOne bidirectional mapping between entities Parent and Child . The list of children Collection<Child> children in Parent is never initialized so it should be null . When using EntityManager.find(...) for previously persisted Parent and then getting the list from that Parent gives ArrayList even there are no children yet with this Parent and it is fine. However if persisting or merging a new Parent in