Powered by Blogger.

Thursday 23 June 2022

Functional Interface in Java

No comments :
Introduction
An Interface which has only one abstract method (SAM: Single Abstract Method) is called Functional Interface. It is used annotation @FunctionalInterface: An informative annotation type used to indicate that an interface type declaration is intended to be a functional interface as defined by the Java Language Specification. Conceptually, a functional interface has exactly one abstract method. Since default methods have an implementation, they are not abstract. If an interface declares an abstract method overriding one of the public methods of java.lang.Object, that also does not count toward the interface's abstract method count since any implementation of the interface will have an implementation from java.lang.Object or elsewhere.

@FunctionalInterface 
 interface adition{
 void add(int a, int b); 
 }

Runnable, ActionListener, Comparable, Comparator, Callable Are some of the examples of functional interface. There is no need to use the abstract Keyword as it is optional to use the interface
why use Functional Interface?
    It is used for enabling functional programming to java language. Functional Interface is also used for defining Lambda Expressions to Pass a function directly as argument to method.

package com.a143mk.SpringBoot.interfaces; 
public class Java8InmlInterfaceLambda { 
public static void main(String[] args) { 
// lambda expression to create the object 
new Thread(() -> { System.out.println("New thread created Lambda"); }).start();
 }
 }

Some Other Functional Interfaces
  • Consumer Interface
  • Predicate Interface 
  • Marker Interfaces
Consumer Interface:  Consumer interface has no return value it returns nothing. there are functional variables of the consumer -DoubleConsumer, IntConsumer and LongConsumer. These variables accept primitives values as arguments.

Predicate Interface : it is a functional interface whose functional method is test(obj), that takes an argument and return boolean value.

The predicate functionl interface can also be implemented using a class.
class ClassNme implements Predicate {}

Marker Interfaces: Marker Interface is an empity interface (no field or argument).
Marker Interface are serializable, cloneable and remote interface. All these interfaces are empity interfae.

Thursday 7 July 2016

Map Interfaces


The Map interface maps unique keys to values. A key is an object that you use to retrieve a value at a later date.
Architecture of Map interface:

Hashtable: Hashtable is similar to HashMap except it is synchronized.

Output:         String, hashtable Contains :{Total=654, Sum=67}
                     Integer, hashtable Contains :{3=4, 1=2}  
LinkedHashMap: It is Hashtable and LinkedList implementation of  map interface. LinkedHashMap has a doublelinkedlist Running through all its elements.

Output:                Put, LinkedHashMap Contains :{Hi=1, manoj=2, welcom=3}
                             Remove, LinkedHashMap Contains :{Hi=1, welcom=3}
HashMap: A HashMap is a Hashtable implementation of map interface, unlike HashMap it contains null keys and values. HashMap does not guarantee that the order of the objects will remain same over the time.

Output:                Put, HashMap Contains :{Hi=1, welcom=3, manoj=2}
                             Remove, HashMap Contains :{Hi=1, welcom=3}
TreeMap: A TreeMap Provides red-block tree based implementation of map interface.

Output:               Put, treeMap Contains :{Hi=1, manoj=2, welcom=3}
                            Remove, treeMap Contains :{Hi=1, welcom=3}

Wednesday 6 July 2016

Collection Framework Implementations


A Collection represents a group of objects known as its elements. The Collection interface is used to pass around collections of objects where maximum generality is desired.
Architecture of collection Framework:


List Interface: A List is a ordered collection that can contain duplicate values.
  1.  ArrayList.
  2. LinkedList.
  3. Vector. 

ArrayList: ArrayList is said to be the best performing List implementation in normal conditions. In expendable array of value or objects.
Example: Copy Element of java ArrayList to Another java ArrayList.
Output:       Before copy, Second ArrayList Contains : [One, Two, Three, Four, Five]
                  After copy, Second ArrayList Contains : [1, 2, 3, Four, Five]
LinkedList: LinkedList is a bit slower than ArrayList but it performs better in certain conditions.
Example: Copy Elements Of LinkedList To another LinkedList.

Output:           Before copy, Second LinkedList Contains : [One, Two, Three, Four, Five]
                        After copy, Second LinkedList Contains : [1, 2, 3, Four, Five]
Vector: Vector is also a grow-able  array of objects but unlike ArrayList. vector is thread safe in nature.
Example: Copy Elements Of Vector To another Vector.

Output:             Before copy, Second Vector Contains : [One, Two, Three, Four, Five]
                        After copy, Second Vector Contains : [1, 2, 3, Four, Five]
Queue Interface: A Queue interface  Provides implementation to hold elements prior to their prcessing.
Example: Add Elements Of Queue java.

Output:            Before Add, one Queue Contains : [1, 2, 3]
                        After Add, Second Queue Contains : [1, 2, 3, manoj]
PriorityQueue: The PriorityQueue class is implemented as a priority heap.
Example: Add Elements Of PriorityQueue java.

Output:        Before Add, One PriorityQueue Contains : [1, 2, 3]
                    After add, Second PriorityQueue Contains : [1, 2, 3, manoj]
Deque Interface: Deque is an interface you need to instantiate a concrete implementation of the interface in order to use it.
Example: Add Elements Of Deque java.

Output:         Before Add, One DequeQueue Contains : [1, 2, 3]
                     After Add, Second DequeQueue Contains : [1, 2, 3, manoj]
ArrayDeque: ArrayDeque stores its elements internally in an array
Example: Add Elements Of ArrayDeque java.

Output:          Before Add, One ArrayDeque Contains : [1, 2, 3]
                      After Add, Second ArrayDeque Contains : [1, 2, 3, manoj]
Set Interface: A Set is a Interface that does not contain duplicate values in java.
  1. Hashset.
  2. TreeSet.
  3. LinkedHashSet.

HashSet: HashSet is the best performing implementation of set interface. if stored its elements in a HashTable an does not guarantee of any type of ordering in iteration.
Example: Find Minimum element of Java HashSet Example.

Output:            Element of Java HashSet is : [3298, 9237, 2374, 2473, 4298]
                          Minimum Element of Java HashSet is : 2374
TreeSet: TreeSet is a Little show than HashSet and it stored its elements in a red-block tree structure. TreeSet orders its elements on the basis of their values.
Example: Add element of Java TreeSet Example.

Output:          TreeSet Output:[m, n]
                       Add elements output:[Java, m, n]
LinkedHashSet: LinkedHashSet is implemented as a Hashtable with a LinkedList running through it. it orders its elements on the basis of order in which they were inserted in the set.
Example: Add element of Java LinkedHashSet Example.

Output:                      LinkedHashSet Output:[m, n]
                                  Add elements output:[m, n, Java]