Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Tuesday, 25 October 2016

What is AbstractQueue In Java ?

Previously we have discussed about
Here we learn about AbstractQueue in Java,but before that we must know What is Queue Interface in Java ? and What is Collection Interface In Java ?
Class AbstractQueue<E>

Type Parameters:
E - the type of elements held in this collection

All Implemented Interfaces:
Iterable<E>, Collection<E>, Queue<E>

Direct Known Subclasses:
ArrayBlockingQueue, ConcurrentLinkedQueue, DelayQueue, LinkedBlockingDeque, LinkedBlockingQueue, LinkedTransferQueue, PriorityBlockingQueue, PriorityQueue, SynchronousQueue

Declaration of AbstractQueue:
public abstract class AbstractQueue<E>
extends AbstractCollection<E>
implements Queue<E>

AbstractQueue class provides skeletal implementations of some Queue operations.The implementations in this class are appropriate when the base implementation does not allow null elements.
Methods add, remove, and element are based on offer, poll, and peek, respectively, but throw exceptions instead of indicating failure via false or null returns.
This class is a member of the Java Collections Framework.

Constructor Summary of AbstractQueue:

Constructor
Description
protected AbstractQueue()
Constructor for use by subclasses.

Method Summary of AbstractQueue:

Modifier and Method Name
Description
boolean add(E e)
Inserts the specified element into this queue if it is possible to do so immediately without violating capacity restrictions, returning true upon success and throwing an IllegalStateException if no space is currently available.
boolean addAll(Collection<? extends E> c)
Adds all of the elements in the specified collection to this queue.
void clear()
Removes all of the elements from this queue.
E element()
Retrieves, but does not remove, the head of this queue.
E remove()
Retrieves and removes the head of this queue.
      
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 »

Java Program to find Duplicate Character In String ?

Previously We have discussed The program for

Here we learn the Program for counting the Duplicate character from String using HashMap class and Set Interface.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class DuplicateCharacterFromString {

  public static void method(String str){
   Map<Character, Integer> map = new HashMap<>();
   for(int i=0;i<str.length();i++){
     if(map.containsKey(str.charAt(i))){
      map.put(str.charAt(i), map.get(str.charAt(i))+1);
     }
     else{
      map.put(str.charAt(i), 1);
     }
   }
   Set<Character> set = map.keySet();
   for(Character c : set){
     if(map.get(c) > 1){
      System.out.println("Duplicate Character : "+c+" Occurence "+map.get(c));
     }
   }
  }
  public static void main(String[] args) {
   DuplicateCharacterFromString.method("abca");
  }

}

Output:

Duplicate Character : a Occurence 2




      
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 »

Java Program to Reverse Numbers ?

Previously we have discussed the Program for



Here we learn the program for Reversing the Numerical digits.

public class ReverseNumbers {

 public static void reverse(int num){
 int reverse = 0;
 while(num != 0){
 reverse = (reverse*10)+(num%10);
  num = num/10;
 }
 System.out.println("REVERSE : "+reverse);
}
public static void main(String[] args) {
ReverseNumbers.reverse(123);
ReverseNumbers.reverse(1425);
}

}

Program Output:

REVERSE : 321
REVERSE : 5241




      
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, 24 October 2016

What is PriorityQueue In Java ?

Previously we Have discussed about


Here we learn about PriorityQueue Class in Java, but before that we must know What is Queue Interface Interface in Java? and What is Collection Interface In Java?
Class PriorityQueue<E>

Type Parameters:
E - the type of elements held in this collection

All Implemented Interfaces:
Serializable, Iterable<E>, Collection<E>, Queue<E>

Declaration of PriorityQueue:
public class PriorityQueue<E>
extends AbstractQueue<E>
implements Serializable

The elements of the priority queue are ordered according to their natural ordering, or by a Comparator provided at Queue creation time, depending on which constructor is used.A priority queue does not permit null elements.

A priority queue is unbounded, but has an internal capacity governing the size of an array used to store the elements on the queue. It is always large as the queue size. As elements are added to a priority queue, its capacity grows automatically. 

The head of this queue is the least element with respect to the specified ordering. The queue retrieval operations poll, remove, peek, and element access the element at the head of the queue.

PriorityQueue implementation is not synchronized. Multiple threads should not access a PriorityQueue instance concurrently if any of the threads modifies the queue. We can use thread-safe class i.e PriorityBlockingQueue class.
This class is a member of the Java Collections Framework.

Constructor Summary of PriorityQueue:

Constructor
Description
PriorityQueue()
Creates a PriorityQueue with the default initial capacity (11) that orders its elements according to their natural ordering.
PriorityQueue(Collection<? extends E> c)

Creates a PriorityQueue containing the elements in the specified collection.
PriorityQueue(int initialCapacity)
Creates a PriorityQueue with the specified initial capacity that orders its elements according to their natural ordering.
PriorityQueue(int initialCapacity, Comparator<? super E> comparator)
Creates a PriorityQueue with the specified initial capacity that orders its elements according to the specified comparator.
PriorityQueue(PriorityQueue<? extends E> c)
Creates a PriorityQueue containing the elements in the specified priority queue.
PriorityQueue(SortedSet<? extends E> c)
Creates a PriorityQueue containing the elements in the specified sorted set.
      
Method Summary of PriorityQueue:

Modifier and Method Name
Description
boolean add(E e)
Inserts the specified element into this priority queue.
void clear()
Removes all of the elements from this priority queue.
Comparator<? super E>                comparator()
Returns the comparator used to order the elements in this queue, or null if this queue is sorted according to the natural ordering of its elements.
boolean contains(Object o)
Returns true if this queue contains the specified element.
Iterator<E> iterator()
Returns an iterator over the elements in this queue.
boolean offer(E e)
Inserts the specified element into this priority queue.
E peek()
Retrieves, but does not remove, the head of this queue, or returns null if this queue is empty.
E poll()
Retrieves and removes the head of this queue, or returns null if this queue is empty.
boolean remove(Object o)
Removes a single instance of the specified element from this queue, if it is present.
int size()
Returns the number of elements in this collection.
Object[] toArray()
Returns an array containing all of the elements in this queue.
<T> T[] toArray(T[] a)
Returns an array containing all of the elements in this queue; the runtime type of the returned array is that of the specified array.

Example of PriorityQueue:

import java.util.PriorityQueue;
import java.util.Queue;

public class PriorityQueueClassExample {

public static void main(String[] args) {
Queue<Integer> que = new PriorityQueue<Integer>();
for(int i=1;i<=5;i++){
que.add(i);
}

System.out.println("Queue Elements : "+que);

System.out.println("\npeek(), This method gives head element : "+que.peek());
System.out.println("poll(), This method remove head element : "+que.poll());
System.out.println("remove(e), This method remove specified element : "+que.remove(2));

System.out.println("\nQueue After Operations : "+que);
}

}
Program Output:

Queue Elements : [1, 2, 3, 4, 5]

peek(), This method gives head element : 1
poll(), This method remove head element : 1
remove(e), This method remove specified element : true

Queue After Operations : [3, 4, 5]



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 »

Sunday, 23 October 2016

What is EnumSet In Java ?

Previously we have discussed about

Here we learn about EnumSet In Java, but before that we must know What is Enum in Java ? and What is Collection Interface in Java ?
Class EnumSet<E extends Enum<E>>

All Implemented Interfaces:
Serializable, Cloneable, Iterable<E>, Collection<E>, Set<E>

Declaration of EnumSet:
public abstract class EnumSet<E extends Enum<E>>
extends AbstractSet<E>
implements Cloneable, Serializable

EnumSet is a specialized Set implementation for use with Enum types. All of the elements in an enum set must come from a single enum type that is specified, explicitly or implicitly, when the set is created. Enum sets are represented internally as bit vectors.The space and time performance of this class is good enough, even bulk operation runs very quickly if their argument is also an enum set.

In EnumSet Null elements are not permitted. If we try to insert a null element it will throw NullPointerException

Iterator method traverses the elements in their natural order (the order in which the enum constants are declared). 

EnumSet is not synchronized. If multiple threads access an enum set concurrently, and at least one of the threads modifies the set, it should be synchronized externally.
This is best done at creation time, to prevent accidental unsynchronized access:
Set<MyEnum> s = Collections.synchronizedSet(EnumSet.noneOf(MyEnum.class));
This class is a member of the Java Collections Framework.

Method Summary of EnumSet:

Modifier and Method Name
Description
static <E extends Enum<E>>
EnumSet<E>      allOf(Class<E> elementType)
Creates an enum set containing all of the elements in the specified element type.
EnumSet<E>      clone()
Returns a copy of this set.
static <E extends Enum<E>>
EnumSet<E>      complementOf(EnumSet<E> s)
Creates an enum set with the same element type as the specified enum set, initially containing all the elements of this type that are not contained in the specified set.
static <E extends Enum<E>>
EnumSet<E>      copyOf(Collection<E> c)
Creates an enum set initialized from the specified collection.
static <E extends Enum<E>>
EnumSet<E>      copyOf(EnumSet<E> s)
Creates an enum set with the same element type as the specified enum set, initially containing the same elements (if any).
static <E extends Enum<E>>
EnumSet<E>      noneOf(Class<E> elementType)
Creates an empty enum set with the specified element type.
static <E extends Enum<E>>
EnumSet<E>      of(E e)
Creates an enum set initially containing the specified element.
static <E extends Enum<E>>
EnumSet<E>      of(E first, E... rest)
Creates an enum set initially containing the specified elements.
static <E extends Enum<E>>
EnumSet<E>      of(E e1, E e2)
Creates an enum set initially containing the specified elements.
static <E extends Enum<E>>
EnumSet<E>      of(E e1, E e2, E e3)
Creates an enum set initially containing the specified elements.
static <E extends Enum<E>>
EnumSet<E>      of(E e1, E e2, E e3, E e4)
Creates an enum set initially containing the specified elements.
static <E extends Enum<E>>
EnumSet<E>      of(E e1, E e2, E e3, E e4, E e5)
Creates an enum set initially containing the specified elements.
static <E extends Enum<E>>
EnumSet<E>      range(E from, E to)
Creates an enum set initially containing all of the elements in the range defined by the two specified endpoints.

Example of EnumSet:

import java.util.EnumSet;

enum size{
S, M, L, XL, XXL, XXXL;
}
public class EnumSetExample {

public static void main(String[] args) {
EnumSet<size> en = EnumSet.of(size.S,size.L,size.M,size.XXL);
System.out.println("EnumSet Size : "+en.size());
System.out.println("range(), This method gets elements between range : "+en.range(size.S, size.XL));
System.out.println("contains(), This method returns true/false : "+en.contains(size.L));
System.out.println("\nTraversing EnumSet Using for loop : ");
for(size s : en){
System.out.println(s);
}
System.out.println("\nTraversing EnumSet Using Lambda Expression : ");
en.forEach(ele -> System.out.println(ele));
}

}
Program Output:

EnumSet Size : 4
range(), This method gets elements between range : [S, M, L, XL]
contains(), This method returns true/false : true

Traversing EnumSet Using for loop :
S
M
L
XXL

Traversing EnumSet Using Lambda Expression :
S
M
L
XXL
    

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 AbstractSet In Java ?

Previously we Have discussed about

Here we learn about AbstractSet in Java ,but before that you must know What is Collection Interface In Java ?
Class AbstractSet<E>

Type Parameters:
E - the type of elements maintained by this set

All Implemented Interfaces:
Iterable<E>, Collection<E>, Set<E>

Direct Known Subclasses:
ConcurrentSkipListSet, CopyOnWriteArraySet, EnumSet, HashSet, TreeSet

Declaration of AbstractSet:
public abstract class AbstractSet<E>
extends AbstractCollection<E>
implements Set<E>

AbstractSet class provides a skeletal implementation of the Set interface.
The process of implementing a set by extending this class is identical to that of implementing a Collection by extending AbstractCollection, except that all of the methods and constructors in subclasses of this class must obey the additional constraints imposed by the Set interface.

AbstractSet class does not override any of the implementations from the AbstractCollection class. It merely adds implementations for equals and hashCode methods.
This class is a member of the Java Collections Framework.

Constructor Summary of AbstractSet:

Constructor
Description
protected AbstractSet()
Sole constructor.

Method Summary of AbstractSet:

Modifier and Method Name
Description
boolean equals(Object o)
Compares the specified object with this set for equality.
int hashCode()
Returns the hash code value for this set.
boolean removeAll(Collection<?> c)
Removes from this set all of its elements that are contained in the specified collection (optional operation).
      
Example of AbstractSet:

import java.util.AbstractSet;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

class Employee extends AbstractSet<Integer> {

Set<Integer> s = new HashSet<Integer>();

@Override
public Iterator<Integer> iterator() {
return s.iterator();
}
@Override
public int size() {
return s.size();
}
@Override
public boolean add(Integer arg0) {
return s.add(arg0);
}
}
public class AbstractSetClassExample {

public static void main(String[] args) {
Employee e =new Employee();
for(int i=1;i<=5;i++){
e.add(i);
}
e.add(3); //Set Will Not add this element,because no duplicate elements are allowed in set.
System.out.println("AbstractSet Size : "+e.size());

System.out.println("\nTraversing AbstractSet using Iterator : ");
Iterator<Integer> it = e.iterator();
while(it.hasNext()){
System.out.println(it.next());
}

System.out.println("\nTraversing AbstractSet using Lambda Expression : ");
e.forEach( i -> System.out.println(i));
}

}
Program Output:

AbstractSet Size : 5

Traversing AbstractSet using Iterator : 
1
2
3
4
5

Traversing AbstractSet using Lambda Expression : 
1
2
3
4
5


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 »