Posts

Showing posts from March, 2015

queue Implementation

Queue Implementation in java : Standard library in java: java.util.queue: methods in standard library //to add element  add() : return true if it success to add otherwise it throw exception. offer(): return true if it success and false if it fail to add. // retrieve and remove first element remove(): throw exception NoSuchElementException if queue is  empty poll(): return  null if queue is empty //give to element but don't remove  element(): throw exception NoSuchElementException if queue is  empty peek(): return  null if queue is empty Queue implementation using: a) circular array b) dynamic array c) linked list a)Circular array: package queue; public class Queue { int front; int rear; int capacity; int[] array; Queue(int size) { this.capacity = size; this.rear = -1; this.front = -1; array = new int[capacity]; } boolean isEmpty() { return (front == -1); } boolean isFull() { return ...

Stack Implementation

Stack Implementation in java : there can be three approach to implement stack. 1) Array based 2) Dynamic array based 3) Linked list base 1) Array based Stack implementation: package stack; public class Stack { int data ; int top; int size; int[] array; Stack() { this.top = -1; this.size = 20; array = new int[this.size]; } Stack(int value, int size) { this.data = value; this.top = -1; this.size = size; array = new int[this.size]; } boolean  push(int node) { if (this.stackIsFull()) { return false; } array[++this.top] = node; return true; } int pop() { if (this.empty()) { return 0; } return array[this.top--]; } int peek() { if (this.empty()) { return 0; } return array[this.top]; } void delete() { this.top = -1; } boolean empty() { return   this.top == -1; } boolean  st...

LinkedList implementation

Single LinkedList implementation in java without using standard linked list library: package example.linkedlist; public class  LinkedList { int data ; LinkedList next; public LinkedList(int data) { this.data = data; this.next = null; } void setdata(int value) { this.data = value; } int getdata() { return this.data; } void setnext(LinkedList  node) { this.next = node; } LinkedList getnext() { return this.next; } int listLength(LinkedList node) { int count = 0; if (node == null) {} else { while (node != null) { count++; node = node.getnext(); } } return count; } boolean insert(LinkedList headnode, int data, int index) { LinkedList newnode = new LinkedList(data); if (headnode == null) { return true; } if (index == 1) { newnode.setnext(headnode.getnext()); headnode.setnext(newnode); return true; } if (index < 0...

Input / Output in Java

 Java Streams are classified into two basic types: a) Input Stream: Read data from Source and send it to program b) Output Stream: Takes data from program and send it to destination. Java define two types of Stream: a) Byte Stream: Handling input/ output  of bytes, it use to read and write binary data. b) Character Stream: Handles data in terms of Characters it use Unicode so its more efficient. ### Byte Stream Class: Reads 8 bits bytes as a stream. 1) InputStream Class: Its a abstract  class which define methods for Reading and Closing Stream.Most of the methods defined by InputStream Class is dummy. Actual body is implementing by sub-classes. close(); //Release the source related to the stream read();  // read next byte read(byte[] b) ;   //Read  Some number of bytes and store it into buffer array b read(byte[] b , int offset, int length) // 2) DataInputStream: Its an interface, Which defines methods for reading primitives from an InputSt...

Collections in Java

Collections in java is framework that provide an architecture to store and manipulate group of objects. 1) Iterator Interface: a) hasNext() b) next(); c) remove(); // remove last returned element. 2) ListIterator  Interface  // ListIterator extends Iterator, it allow to traverse  in either direction. a) add(Object obj); b) hasNext(); c) hasPrevious(); d) next(); e) previous(); f) remove(); g) set(Object obj ); // set last returned element  next/ previous 3) RandomAccess Interface // dummy or empty interface or a marker interface RandamAccess {} A Class implements only List interface provides sequential access but if a class also implements RandamAccess then it provide randam access. 4) Compatrator Interface : int compare (Object o1, Object o2) ;// o1 > o2  then positive value 5) Collection Interface : it declare fundamental mehods because all the collection implements  collection frameworks. boolean add ( object obj); ...

Generics in Java

Create a generic Class Demo: class Demo <T> { void method(T obj)    //method can take any type of data type by default it is object. {} } public static void main (String [] arg) { Demo demo = new Demo();    //T is a object. demo.method("string"); demo.method(10); } Demo <String> demo1 = new Demo<String>(); Demo <Integer> demo2 = new Demo<Integer>(); Generics works only with objects, so we cannot use for primitive data types. Demo <int> obj3 = new Demo<int>(); // Its illegal One generic type is not compatible with another version of the same generic type. demo1 = demo2 //will give error because both have different type compatible.

Mastering Multithreading in Java

Multithreading is one of Java's most powerful features, enabling applications to perform multiple tasks concurrently and maximize CPU utilization. In this comprehensive guide, we'll explore how threads work in Java, the different ways to create them, and critical concepts like synchronization and thread management. What is a Thread? A thread represents a single flow of control within a program. While your application runs as a process, threads allow multiple sequences of execution to run concurrently within that process, sharing the same memory space but executing independently. Java provides robust built-in support for multithreading, making it straightforward to create responsive applications that can handle multiple operations simultaneously. Creating Threads in Java Java offers two primary approaches to create threads: 1. Implementing the Runnable Interface The Runnable interface is the recommended approach for creating threads. It provides a clean separation between...