Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Friday, 30 December 2016

forEach() method In Iterable Interface.


forEach() method in Iterable Interface:

Java 8 has introduced forEach method in java.lang.Iterable interface so that  while writing code we focus on business logic only. forEach() method takes java.util.function.Consumer  object as argument, so it helps in having our business logic at a separate location that we can reuse.

Example Showing forEach() method In Iterable Interface:

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

public class ForEachWithIterator {

public static void main(String[] args) {

List<Integer> list = new ArrayList<>();
for(int i=0;i<10;i++){
list.add(i);
}
System.out.println("Traversing list through forEach() method of iterable with ananomous class");
list.forEach(new Consumer<Integer>() {
@Override
public void accept(Integer t) {
System.out.print(t+",");
}
});
System.out.println("\n\nTraversing list through forEach() method of iterable with interface");
MyConsumer action = new MyConsumer();
list.forEach(action);
}
}

class MyConsumer implements Consumer<Integer>{
@Override
public void accept(Integer t) {
System.out.print(t+",");
}
}

Program Output:

Traversing list through forEach() method of iterable with ananomous class
0,1,2,3,4,5,6,7,8,9,

Traversing list through forEach() method of iterable with interface
0,1,2,3,4,5,6,7,8,9,



      
Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.

This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.



Read More »

Thursday, 29 December 2016

What is Functional Interface In Java ?


Functional Interface :

It belongs to java.util.function; package functional interface is defined as an interface with exactly one abstract method ,some functional interfaces are :
  1. Function<T,R> - takes an object of type T and returns R.
  2. Supplier<T> - just returns an object of type T.
  3. Predicate<T> - returns a boolean value based on input of type T.
  4. Consumer<T> - performs an action with given object of type T.
  5. BiFunction - like Function but with two parameters.
  6. BiConsumer - like Consumer but with two parameters. 
It also have some  interfaces for primitive data type :
  1. IntConsumer
  2. IntFunction<R>
  3. IntPredicate
  4. IntSupplier 

Example Showing Functional Interface:

import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Predicate;

public class FunctionalInterfaces {

public static void main(String[] args) {

List<Integer> list = Arrays.asList(1,2,5,8,3,6,9);
System.out.println("USE OF CUNSUMER INTERFACE TO TRAVERSE LIST");
list.forEach(new Consumer<Integer>() {
@Override
public void accept(Integer t) {
System.out.println(t);
}
});

System.out.println("\nEVEN NUMBERS FROM LIST");
Predicate<Integer> predicate = n-> n%2 == 0;
list.forEach(new Consumer<Integer>() {
@Override
public void accept(Integer t) {
if(predicate.test(t)){
System.out.println(t);
}
}
});

System.out.println("\nODD NUMBERS FROM LIST");
Predicate<Integer> oddPre = n-> n%2 != 0;
list.forEach(new Consumer<Integer>() {
@Override
public void accept(Integer t) {
if(oddPre.test(t)){
System.out.println(t);
}
}
});
System.out.println("\nNUMBERS GREATER THAN 3 IN LIST");
Predicate<Integer> pre1 = n -> n>3;
list.forEach(new Consumer<Integer>() {
@Override
public void accept(Integer t) {
if(pre1.test(t)){
System.out.println(t);
}
}
});
}
}
Program Output :

USE OF CUNSUMER INTERFACE TO TRAVERSE LIST
1
2
5
8
3
6
9

EVEN NUMBERS FROM LIST
2
8
6

ODD NUMBERS FROM LIST
1
5
3
9

NUMBERS GREATER THAN 3 IN LIST
5
8
6
9




                                                   
Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.

This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.



Read More »

What is Lambda Expression In Java ?


Lambda Expression

The biggest new feature of java-8 is Lambda Expression. It simplifies the development a lot means it cut lines of code. The syntax for it is “parameters -> body” , Some important rules to syntax is : 
  • Declaring the types of the parameters is optional.
  • Using parentheses around the parameter is optional if you have only one parameter.
  • Using curly braces is optional (unless you need multiple statements).
  • The “return” keyword is optional if you have a single expression that returns a value.

Example Showing Lambda Expression:

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

interface MathOperation{
int operation(int a,int b);
}
public class LambdaExample {

public static void main(String[] args) {
// with variable type declaration
MathOperation add = (int a,int b) -> a+b;
System.out.println("ADDITION\t:"+add.operation(5, 2));
// without variable type declaration
MathOperation sub = (a,b) -> a-b;
System.out.println("SUBSTRACTION\t:"+sub.operation(5, 3));
// with return statement along with curly braces
MathOperation div = (a,b) -> {return a/b ; };
System.out.println("DIVIDE\t\t:"+div.operation(10, 5));
new Thread( () -> System.out.println("In Java8, Lambda expression rocks !!") ).start();
// Traversing Arraylist in one line
List<Integer> l=new ArrayList<Integer>();
for(int i=0;i<10;i++){
l.add(i);
}
l.forEach( n -> System.out.print(n));
// Providing the type of parameter is optional 
l.forEach((Integer n) -> System.out.print(n));
// Sorting Arraylist using Lambda Expression in one line
List<Integer> list = new ArrayList<>();
list.add(2);
list.add(5);
list.add(3);
Collections.sort(list , (i,j) -> i.compareTo(j));
System.out.println("\nSORTED LIST : "+list);
// Traversing Map using Lambda Expression 
Map<String, Integer> items = new HashMap<>();
items.put("A", 10);
items.put("B", 20);
items.put("C", 30);
items.put("D", 40);
items.put("E", 50);
items.put("F", 60);
items.forEach((k,v)->System.out.println(k+":::"+v));
}
}

Program Output :

ADDITION :7
SUBSTRACTION :2
DIVIDE :2
In Java8, Lambda expression rocks !!
01234567890123456789
SORTED LIST : [2, 3, 5]
A:::10
B:::20
C:::30
D:::40
E:::50
F:::60


Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.

This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.



Read More »

Monday, 26 December 2016

What is difference between include directive and include action ?


Here get all the differences between jsp include directive and include action.

Include Directive
Include Action
It includes the content of page at Translation Time.
It includes the content of page at Request Time.
It is mostly use for static pages.
It is mostly use for dynamic pages.
Changes are not visible until the jsp file will compile again.
Changes are visible as we request the Jsp page again.
It uses file attribute to specify resource.
It uses page attribute to specify resource.
It include those files which are available in same application.
It include file which are available in same application or another web application.
Syntax :
<%@ include file = ‘file_name’ %>
Syntax :
<jsp:include page = ‘file_name’>

      
Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.

This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.



Read More »

What are the JSP implicit objects ?


In jsp their are 9 Implicit objects, Jsp container provide these objects for each page and developer can call any object directly ,without declaring those objects.We use these objects in scriptlets tags because these objects goes to service method. We cannot use these objects in declaration tags because code written in this tag go to class level.

These objects are also known as Pre-defined variables.

Sl No.
Object
Object Type
1
out
javax.servlet.jsp.JspWriter
2
request
javax.servlet.http.HttpServletRequest
3
response
javax.servlet.http.HttpServletResponse
4
config
javax.servlet.ServletConfig
5
application
javax.servlet.ServletContext
6
session
javax.servlet.http.HttpSession
7
pageContext
javax.servlet.jsp.PageContext
8
page
java.lang.Object
9
exception
java.lang.Throwable

      
Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.

This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.



Read More »

Tuesday, 20 December 2016

What is the difference between List and Set Interface In Java ?


Here we list all the differences between List interface and Set interface In Java.


List
Set
List contains duplicate elements.
Set contains only Unique elements.
List is an ordered collection. It maintains insertion order of elements.
Set is an unordered collection. It doesn’t maintain insertion order of elements.
List allows any number of null values.
Set allows only single null value.
List has one Legacy class i.e. vector.
Set don’t have any Legacy class.
List can be traversed with ListIterator and Iterator.
Set can only traversed with Iterator.
List implementation classes are ArrayList, LinkedList, Stack, Vector.
Set implementation classes are HashSet, LinkedHashSet, TreeSet.
List allows index based search of any element, using get(E) method.
Set doesn’t allows index based search of element.

      
Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.

This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.



Read More »

Wednesday, 14 December 2016

What is the difference between Iterator and Enumeration In Java ?


Here we list all the differences between Iterator and Enumeration In Java.

Know Also : Iterator

Iterator
Enumeration
This interface is introduced in JDK 1.2.
This interface is introduced in JDK 1.0.
Iterator is used to traverse the collection object as well as it can modify the collection also using remove() method.
It doesn’t allow doing any modifications.
It is not a legacy interface.
It is a legacy interface that’s why it traverses only legacy class’s i.e Vector, Stack and HashTable.
It is fail-fast in nature that means it can throw ConcurrentModificationException.
It is fail-safe in nature that’s mean it cannot throw any Exception.
Iterator is safe and secure.
Enumeration is not safe and secure due to its fail-safe nature.
Methods : hasNext(), next() and remove().
Methods : hasMoreElements() and nextElement().


     
Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.

This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.



Read More »