Course
Introduction
Print an Integer (Entered by the User)Add Two IntegersMultiply two Floating Point NumbersFind ASCII Value of a characterCompute Quotient and RemainderSwap Two NumbersCheck Whether a Number is Even or OddCheck Whether an Alphabet is Vowel or ConsonantFind the Largest Among Three NumbersFind all Roots of a Quadratic EquationFind the Frequency of Character in a StringRemove All Whitespaces from a StringRound a Number to n Decimal PlacesCheck if a String is Empty or NullType Conversion
Convert Character to String and Vice-Versaconvert char type variables to intconvert int type variables to charconvert long type variables into intconvert int type variables to longconvert boolean variables into stringconvert string type variables into booleanconvert string type variables into intconvert int type variables to Stringconvert int type variables to doubleconvert double type variables to intconvert string variables to doubleconvert double type variables to stringconvert primitive types to objects and vice versaDecision Making and Loop
Check Leap YearCheck Whether a Number is Positive or NegativeCheck Whether a Character is Alphabet or NotCalculate the Sum of Natural NumbersFind Factorial of a NumberGenerate Multiplication TableDisplay Fibonacci SeriesFind GCD of two NumbersFind LCM of two NumbersDisplay Alphabets (A to Z) using loopCount Number of Digits in an IntegerReverse a NumberCalculate the Power of a NumberCheck PalindromeCheck Whether a Number is Prime or NotDisplay Prime Numbers Between Two IntervalsCheck Armstrong NumberDisplay Armstrong Number Between Two IntervalsDisplay Factors of a NumberMake a Simple Calculator Using switch...caseCount the Number of Vowels and Consonants in a SentenceSort Elements in Lexicographical Order (Dictionary Order)Create Pyramid and PatternFunctions
Display Prime Numbers Between Intervals Using FunctionDisplay Armstrong Numbers Between Intervals Using FunctionCheck Whether a Number can be Expressed as Sum of Two Prime NumbersFind the Sum of Natural Numbers using RecursionFind Factorial of a Number Using RecursionFind G.C.D Using RecursionConvert Binary Number to Decimal and vice-versaConvert Octal Number to Decimal and vice-versaConvert Binary Number to Octal and vice-versaReverse a Sentence Using Recursioncalculate the power using recursionCall One Constructor from anotherimplement private constructorspass lambda expression as a method argumentPass the Result of One Method Call to Another MethodCalculate the Execution Time of MethodsArrays
Calculate Average Using ArraysFind Largest Element of an ArrayCalculate Standard DeviationAdd Two Matrix Using Multi-dimensional ArraysMultiply Two Matrix Using Multi-dimensional ArraysMultiply two Matrices by Passing Matrix to a FunctionFind Transpose of a MatrixPrint an ArrayConcatenate Two ArraysCheck if An Array Contains a Given ValueObject and Class
Add Two Complex Numbers by Passing Class to a FunctionCalculate Difference Between Two Time PeriodsDetermine the class of an objectCreate an enum classPrint object of a classCreate custom exceptionCreate an Immutable ClassString
Convert String to DateConvert a Stack Trace to a StringCompare StringsCheck if a String is NumericCheck if two strings are anagramCompute all the permutations of the stringCreate random stringsClear the StringBufferCapitalize the first character of each word in a StringIterate through each characters of the string.Differentiate String == operator and equals() methodImplement switch statement on stringsCheck if a string contains a substringCheck if a string is a valid shuffle of two distinct stringsCollections
Join Two ListsConvert a List to Array and Vice VersaConvert Map (HashMap) to ListConvert Array to Set (HashSet) and Vice-VersaSort a Map By ValuesSort ArrayList of Custom Objects By PropertyImplement LinkedListImplement stack data structureImplement the queue data structureGet the middle element of LinkedList in a single iterationConvert the LinkedList into an Array and vice versaConvert the ArrayList into a string and vice versaIterate over an ArrayListIterate over a HashMapIterate over a SetMerge two listsUpdate value of HashMap using keyRemove duplicate elements from ArrayListGet key from HashMap using the valueDetect loop in a LinkedListCalculate union of two setsCalculate the intersection of two setsCalculate the difference between two setsCheck if a set is the subset of another setSort map by keysPass ArrayList as the function argumentIterate over ArrayList using Lambda ExpressionImplement Binary Tree Data StructurePerform the preorder tree traversalPerform the postorder tree traversalPerform the inorder tree traversalCount number of leaf nodes in a treeImplement the graph data structureRemove elements from the LinkedList.Add elements to a LinkedListAccess elements from a LinkedList.Algorithms
Implement Bubble Sort algorithmImplement Quick Sort AlgorithmImplement Merge Sort AlgorithmImplement Binary Search AlgorithmFiles
Get Current Working DirectoryCreate String from Contents of a FileAppend Text to an Existing FileConvert File to byte array and Vice-VersaCreate File and Write to the FileRead the Content of a File Line by LineDelete File in JavaDelete Empty and Non-empty DirectoryGet the File ExtensionGet the name of the file from the absolute pathGet the relative path from two absolute pathsCount number of lines present in the fileI/O Stream
Convert InputStream to StringConvert OutputStream to StringConvert a String into the InputStreamConvert the InputStream into Byte ArrayLoad File as InputStreamAdvanced
Get Current Date/TimeConvert Milliseconds to Minutes and SecondsAdd Two DatesConvert Byte Array to HexadecimalLookup enum by String valueCalculate simple interest and compound interestImplement multiple inheritanceDetermine the name and version of the operating systemCheck if two of three boolean variables are trueIterate over enumCheck the birthday and print Happy Birthday messageAccess private members of a classJava Program to Implement the queue data structure
To understand this example, you should have the knowledge of the following Java programming topics:
Example 1: Java program to implement Queue
public class Queue {
int SIZE = 5;
int items[] = new int[SIZE];
int front, rear;
Queue() {
front = -1;
rear = -1;
}
// check if the queue is full
boolean isFull() {
if (front == 0 && rear == SIZE - 1) {
return true;
}
return false;
}
// check if the queue is empty
boolean isEmpty() {
if (front == -1)
return true;
else
return false;
}
// insert elements to the queue
void enQueue(int element) {
// if queue is full
if (isFull()) {
System.out.println("Queue is full");
}
else {
if (front == -1) {
// mark front denote first element of queue
front = 0;
}
rear++;
// insert element at the rear
items[rear] = element;
System.out.println("Insert " + element);
}
}
// delete element from the queue
int deQueue() {
int element;
// if queue is empty
if (isEmpty()) {
System.out.println("Queue is empty");
return (-1);
}
else {
// remove element from the front of queue
element = items[front];
// if the queue has only one element
if (front >= rear) {
front = -1;
rear = -1;
}
else {
// mark next element as the front
front++;
}
System.out.println( element + " Deleted");
return (element);
}
}
// display element of the queue
void display() {
int i;
if (isEmpty()) {
System.out.println("Empty Queue");
}
else {
// display the front of the queue
System.out.println("\nFront index-> " + front);
// display element of the queue
System.out.println("Items -> ");
for (i = front; i <= rear; i++)
System.out.print(items[i] + " ");
// display the rear of the queue
System.out.println("\nRear index-> " + rear);
}
}
public static void main(String[] args) {
// create an object of Queue class
Queue q = new Queue();
// try to delete element from the queue
// currently queue is empty
// so deletion is not possible
q.deQueue();
// insert elements to the queue
for(int i = 1; i < 6; i ++) {
q.enQueue(i);
}
// 6th element can't be added to queue because queue is full
q.enQueue(6);
q.display();
// deQueue removes element entered first i.e. 1
q.deQueue();
// Now we have just 4 elements
q.display();
}
}
Output
Queue is empty
Insert 1
Insert 2
Insert 3
Insert 4
Insert 5
Queue is full
Front index-> 0
Items ->
1 2 3 4 5
Rear index-> 4
1 Deleted
Front index-> 1
Items ->
2 3 4 5
Rear index-> 4
In the above example, we have implemented the queue data structure in Java.
To learn the working about the queue, visit Queue Data Structure.
Example 2: Implement queue using Queue interface
Java provides a built Queue
interface that can be used to implement a queue.
import java.util.Queue;
import java.util.LinkedList;
class Main {
public static void main(String[] args) {
// Creating Queue using the LinkedList class
Queue<Integer> numbers = new LinkedList<>();
// enqueue
// insert element at the rear of the queue
numbers.offer(1);
numbers.offer(2);
numbers.offer(3);
System.out.println("Queue: " + numbers);
// dequeue
// delete element from the front of the queue
int removedNumber = numbers.poll();
System.out.println("Removed Element: " + removedNumber);
System.out.println("Queue after deletion: " + numbers);
}
}
Output
Queue: [1, 2, 3]
Removed Element: 1
Queue after deletion: [2, 3]
In the above example, we have used the Queue
interface to implement the queue in Java. Here, we have used the LinkedList
class that implements the Queue
interface.
- numbers.offer() - insert elements to the rear of the queue
- numbers.poll() - remove an element from the front of the queue
Notice, we have used the angle brackets <Integer>
while creating the queue. It represents that the queue is of the generic type.
We can also use other interfaces and classes instead of Queue
and LinkedList
. For example,