Skip to main content

Posts

Showing posts with the label Core Java

Java 8 - default and static methods in interfaces

Java 8 introduced default and static methods in interfaces. These features allow us to add new functionality in the interfaces without breaking the existing contract for implementing classes. How do we define default and static methods? Default method has default and static method has static keyword in the method signature. public interface InterfaceA { double someMethodA(); default double someDefaultMethodB() { // some default implementation } static void someStaticMethodC() { //helper method implementation } Few important points for default method You can inherit the default method. You can redeclare the default method essentially making it abstract . You can redefine the default method (equivalent to overriding). Why do we need default and static methods? Consider an existing Expression interface with existing implementation like ConstantExpression , BinaryExpression , DivisionExpression and so on. Now, you want to add new functionalit

Introduction to Apache Ignite

This is an introduction series to Apache Ignite. We will discuss about Apache Ignite, its features, usage as in-memory data grid, compute grid, distributed caching, near real-time caching and persistence distributed database. What is Ignite? It is in-memory compute platform . It is in-memory data grid . Durable , strongly consistent and highly available. Providing option to run SQL like queries on cache (Providing JDBC API to support this). Durable memory Apache Ignite is memory-centric platform based on durable memory architecture. It allows you to store and processing data on in-memory(RAM) and on disk (If Ignite Native persistence is enabled). When the Ignite native persistence is enabled, it will treat disk as superset of data, which is cable of surviving crash and restarts. In-memory features RAM is always treated as first memory tier, all the processing happens there. It has following characteristics. Off-heap based: All the data and indexes are stored outs

Java 8 - Method references

This article is in continuation to my other posts on Functional Interfaces , static and default methods and Lambda expressions . Method references are the special form of Lambda expression . When your lambda expression are doing nothing other than invoking existing behaviour (method), you can achieve same by referring it by name. :: is used to refer to a method. Method type arguments are infered by JRE at runtime from context it is defined. Types of method references Static method reference Instance method reference of particular object Instance method reference of an arbitrary object of particular type Constructor reference Static method reference When you refer static method of Containing class. e.g. ClassName::someStaticMethodName class MethodReferenceExample { public static int compareByAge(Employee first, Employee second) { return Integer.compare(first.age, second.age); } } Comparator compareByAge = MethodReferenceExample::compareByAge; Instanc

Java 8 - Lambda expressions

In this post, we will cover following topics. What are Lambda expressions? Syntax for Lambda expression. How to define no parameter Lambda expression? How to define single/ multi parameter Lambda expression? How to return value from Lambda expression? Accessing local variables in Lambda expression. Target typing in Lambda expression. What are Lambda expressions? Lambda expressions are the first step of Java towards functional programming. Lambda expressions enable us to treat functionality as method arguments, express instances of single-method classes more compactly. Syntax for Lambda expression Lambda has three parts: comma separated list of formal parameters enclosed in parenthesis. arrow token -> . and, body of expression (which may or may not return value). (param) -> { System.out.println(param); } Lambda expression can only be used where the type they are matched are functional interfaces . How to define no parameter Lambda expression? If the la

Java 8 - What are functional interfaces in Java?

Java 8 reincarnated SAM interfaces and termed them Functional interfaces. Functional interfaces have single abstract method and are eligible to be represented with Lambda expression . @FunctionalInterface annotation is introduced in Java 8 to mark an interface as functional. It ensures at compile-time that it has only single abstract method, otherwise it will throw compilation error. Let's define a functional interface. @FunctionalInterface public interface Spec<T> { boolean isSatisfiedBy(T t); } Functional interfaces can have default and static methods in them and still remains functional interface. @FunctionalInterface public interface Spec<T> { boolean isSatisfiedBy(T t); default Spec<T> not() { return (t) -> !isSatisfiedBy(t); } default Spec<T> and(Spec<T> other) { return (t) -> isSatisfiedBy(t) && other.isSatisfiedBy(t); } default Spec<T> or(Spec<T> other) { return (t) -> isSa

Java 8 - Filtering with Predicates

In this post, we will cover following items. What is java.util.function.Predicate? How to filter data with Predicates? Predicate chaining. Java 8 introduced many new features like Streaming API, Lambdas , Functional interfaces , default methods in interfaces and many more. Today, we will discuss about Predicate interface added in java.util.function package and its usage in filtering in-memory data. What is java.util.function.Predicate? Predicate is like a condition checker, which accepts one argument of type T and return the boolean value. It's a functional interface with functional method test(Object) . Here, Object is typed. @FunctionalInterface interface Predicate < T > { public boolean test ( T t ); } How we can filter data with Predicates? Consider we have Collection of employees and we want to filter them based on age, sex, salary and/ or with any other combinations. We can do that with Predicate . Let's understand this with one short