Skip to content

Tag: java-8

How to Iterate over a String Array inside .map()

So i have these lines in a method – where Permission is a class and Name is a static inner class inside Permission Class, and getName() is a non-static method inside Permission class and getValues() is a static method inside Name class. The problem is getValues method returns a String Array. I am not ab…

Serial version uid in abstract exception class

I have a base custom exception class BaseException and several custom exceptions that extends BaseException I got a warning about serial version uid being not declared in the BaseException class. Is it needed in an abstract class? Is it a good practice? Is any way to get rid of the warning? Answer Yes, you ne…

parallelStream() java 1.8 vs 11

Consider the following code: When it is compiled and run with java 11, it returns the following: But with java 1.8, it returns different result: Why results are different? Answer Both results are consistent with the Java Memory Model. One possible ordering in which execution occurs is: but, because you don&#8…

How to throw an Exception in a Consumer Java 8

Is there any way to throw an Exception while using a consumer in java 8? For example: This gives me a compiler error saying: Unhandled exception type Exception What is the correct way to throw an exception in this case? Answer Since Exception and its subclass (other than RuntimeException) are checked Exceptio…

Direct conversion from Collection to LongStream

I have a Collection<Long> (obtained from a Map<UUID, Long>’s values() method) and I would like to convert it into a LongStream. The simplest way I can think of is: However it strikes me that there should be a simpler way to obtain primitive streams from Collections of boxed equivalents. I ch…