Posts

Showing posts from September, 2019

Java 9 Stream dropWhile method with examples

Java 8 introduced the Stream API which allows processing and applying some operations to the elements in a Collection.  Java 9 has made some improvements to the Stream API. One such improvement is the dropWhile method. In this article, I will be covering the Java 9 Stream dropWhile method in detail. I will also be writing some Java 9 dropWhile examples. The dropWhile method is the exact opposite of the takeWhile method that I had covered earlier here . It behaves differently on ordered and unordered Streams as explained subsequently.   dropWhile on Ordered Stream As per the API documentation, when the dropWhile is applied on an ordered Stream, it returns, a stream consisting of the remaining elements after dropping the longest prefix of elements that match the given predicate. So basically, the dropWhile method applies the Predicate on each element in the input Stream and drops it if the condition is true. It continues this until the condition becomes false. The following code demonstr

Important JAX-RS annotations explained

In this article, I will be explaining some important annotations in JAX-RS What is JAX-RS JAX-RS is a Java API that you can use for building a REST service. JAX-RS stands for Java API for RESTful Web Services (JAX-RS). It is part of the Java EE since Java 6. The latest version of JAX-RS is JAX-RS 2.0 and is part of Java EE 7. JAX-RS is just an API; it has some annotations that you can use to build a REST application.  JAX-RS does not include an implementation. Jersey framework is the reference implementation of JAX-RS specification and implements JAX-RS 2.0. There are other implementations of JAX-RS like RESTEasy, etc. Important JAX-RS annotations @Get/@Post/@Put/@Delete/@Head You need to specify these annotations on a JAX-RS method. These annotations indicate the HTTP method that the JAX-RS method maps to. So when a client request comes in, JAX-RS matches the HTTP method in the client request to the method that has the appropriate annotation. If there is no annotation on a method, JAX

Java Final Keyword Explained with Code Samples

In this article, I will be covering Java’s final keyword. I will be demonstrating the final keyword in Java with an example. Introduction You can use the final keyword with a method or instance field. When you use it with a method, it prevents the method from being overridden. When you use it with a field, it prevents the field from being modified. Final Method public class Base {public final void show() {System.out.println("In show”);}}public class Sub extends Base {// This method causes a compilation errorpublic void show() {}}}     Class Base has a method show which is marked as final Sub is a sub-class, it overrides show There is a compilation error in Sub as the final method cannot be overridden   Final Field Declaring a variable as final prevents its contents from being modified. This means that you must initialize a final variable when it is declared. The following code demonstrates a final field: public void aMethod() {final int d = 10;d = 3; // compilation error} The

Java 8 DoubleFunction Example

In this blog post, I will be explaining how the Java 8 functional interface DoubleFunction works. To know more about functional interfaces, you can refer  this  blog post. Edit The   DoubleFunction interface provides a method called apply . It accepts a single parameter of double data type. It returns a result of any data type. So it basically applies the logic in the apply method to the input parameter and returns the result. The DoubleFunction interface is a specialization of the Function interface. While the Function interface accepts any data type, the DoubleFunction interface accepts a long value. To see an example of the Function interface, refer to  this  blog post. DoubleFunction Interface that returns an Integer Consider the following code snippet: DoubleFunction<Integer> integerRetriever = input -> new Double(input).intValue();Double input = 4.8;System.out.println("Integer Value "+integerRetriever.apply(input) );   Here, we have written a   Doubl

Java 8 SplitIterator Explained with code samples

Another new feature in Java 8 is the SplitIterator interface. This article will explain what a SplitIterator is and how you can use it. Edit What is a SplitIterator? Just like a normal Iterator, you can use a SplitIterator to iterate over the elements in a Collection. A new method called splitIterator has been added to all the Collection interfaces. This returns a SplitIterator instance. The following code demonstrates this: public class SplitIteratorDemo { public static void main(String[] args) { List<Integer> input = Arrays.asList(5, 3, 21, 15, 9, 2, 12, 11); Spliterator<Integer> sitr1 = input.spliterator(); sitr1.forEachRemaining(num -> System.out.println(num)); }} The above code obtains a SplitIterator over the input list. Just like the Iterator interface, the forEachRemaning method is also available on the SplitIterator interface. This can be used to iterate through the elements in the input collection. So when you execute the above code, it pri