I am working on a Spring Boot application. Into a service class I have this service method: As usual it is calling the Spring Data JPA findById() method returning an Optional object. I am returning the User object. And here I have the following doubt: if the Optional is empty, it is throwing an exception when I this operation is
Tag: java-8
How do I check if two simple 2D arrays have the same 1D arrays? (order and repetitions doesn’t matter)
My main objective is to return if all elements, int[ ], of a 2D Array ,int[ ][ ], are present in another 2D Array. I already tried to use Arrays.deepEquals() but in this case, the order of the elements would matter and that’s not the purpose. Int[ ][ ] arrays wouldn’t be longer than 15, for example. Int[ ][ ]
How to get a generic type which is defined on method in Java?
Question 1: Suggest I have a example like this: then can I get type String in methodA without actually passing String to it? Question 2: If the previous example is transformed to this: I mean what was passed into methodA is no more a simple object but a lambda expression, also, can I get type String in methodA? Answer No,
Is the System.gc() call in sun.nio.ch.FileChannelImpl a bad case?
From jdk/FileChannelImpl.java at jdk8-b120. Does this help with exception recovery? Answer When an object allocation fails with an OutOfMemoryError, the garbage collector did already try its best to reclaim memory of unused objects or expanding the heap. So calling System.gc() after catching OutOfMemoryError would be pointless. A special case is the situation when the garbage collector repeatedly did reclaim a
void with Generics in Java
I have a function that returns void Generic method Implementation of generic interface How can I use void in IRequestHandler<DeleteProductCommand, Void> so that I can map void from iProductService.delete(deleteProductCommand.id); Answer Option 1: Just return null: Option 2: Update the IProductService::delete method to return something meaningful, e.g. a boolean value like Collection::remove does:
Downgrade from Java OpenJDK 64-Bit Server VM 1.8.0_292
Good morning, I have an application that works only with Java OpenJDK 64-Bit Server VM 1.8.0_292, however we are in the Java OpenJDK 64-Bit Server VM 1.8.0_312 version. How can I install it? Answer Go here: https://adoptium.net/releases.html?variant=openjdk8 download package extract package try calling it direclty (/new/path/jdk8/bin/java -version). This should now differ from calling the default java (java -version) instead of
Map a List to DTO inside map – java
I have such collection: Map<Integer, List<MyObject>> collection I would like to map the whole list of MyObject to MyObjectDTO and return the whole map with the mapped list. So return will be: Map<Integer, List<MyObjectDto>> collectionWithDtos What is the easiest and quickest way? I’ve checked a couple of ways with streams but none of that produced results as I expected. Thanks
Compare Map Value with list using Stream API
Need to collect the common value from a List of String and from a MAP having String as KEY and List as VALUE. Snippet: In third method need to compare the List and MAP value and collect the common no in a List. Please guide using JAVA 8 Stream API Answer
arranging the records in customised way
Sort Procedure First sort by the delivered date & in case of multiple records on same date sort by delivered time (Note: if a record has both ordered and Delivered Date ,it should be sorted by Delivered date only) 08-JUN: 03.00, 08-JUN: 04.00 Then look at the ordered date without delivered date which also exists for 08-JUN & sort by
JAXB UnMarshalling Renders Null Values (JAXB, Java 1.8)
I have an XSD which I have written like this: Now I generate the JAXB classes using CXF maven plugin. And i get a JAXB class like (used a decompiler for this): Now to get it out, I wrote a simple class that just takes a string and unmarshalls it: But that last line prints null. I was expecting it