Class Running Notes From 15th To 19th Nov
Class Running Notes From 15th To 19th Nov
Note:
=>In realtime Stack<E> and Queue<E> are used in Algorithmic design part of
==================================================================
3.Queue<E>:
i
thi
=>Queue<E> organizes elements based on the algorithm First-In-First-Out or
Last-In-Last-Out.
ipa
=>The following are some important methods of Queue<E>:
Ex-Program : DemoQueue.java
Ve
package maccess;
import java.util.*;
public class DemoQueue {
@SuppressWarnings("removal")
public static void main(String[] args) {
Queue<Integer> ob = new PriorityQueue<Integer>();
Scanner s = new Scanner(System.in);
try(s;){
try {
while(true) {
System.out.println("****Choice****");
System.out.println("1.add(E)\n2.remove()\n3.poll()\n4.element()\
n5.peek()\n6.exit");
System.out.println("Enter the Choice:");
switch(s.nextInt()) {
case 1:
System.out.println("Enter the ele:");
ob.add(new Integer(s.nextInt()));
System.out.println(ob.toString());
break;
i
case 2:
thi
if(ob.isEmpty()) {
System.out.println("Queue is
empty...");
}else {
ipa
ob.remove();
System.out.println("Ele removed
from Queue...");
Ma System.out.println(ob.toString());
}
break;
case 3:
if(ob.isEmpty()) {
System.out.println("Queue is
empty...");
sh
}else {
ob.poll();
ate
System.out.println(ob.toString());
}
break;
case 4:
nk
if(ob.isEmpty()) {
System.out.println("Queue is
empty...");
}else {
Ve
System.out.println("element:"+ob.element());
System.out.println(ob.toString());
}
break;
case 5:
if(ob.isEmpty()) {
System.out.println("Queue is
empty...");
}else {
System.out.println("peek:"+ob.peek());
System.out.println(ob.toString());
}
break;
case 6:
System.out.println("Operations stopped
on Queue...");
System.exit(0);
default:
System.out.println("Invalid Choice..");
i
}//end of switch
thi
}//end of loop
}catch(Exception e) {e.printStackTrace();}
}//end of try
}
ipa
}
o/p: Ma
****Choice****
1.add(E)
2.remove()
sh
3.poll()
ate
4.element()
5.peek()
nk
6.exit
11
[11]
****Choice****
1.add(E)
2.remove()
3.poll()
4.element()
5.peek()
6.exit
i
thi
Enter the Choice:
ipa
Enter the ele:
12
[11, 12]
Ma
****Choice****
1.add(E)
sh
2.remove()
3.poll()
ate
4.element()
5.peek()
nk
6.exit
13
****Choice****
1.add(E)
2.remove()
3.poll()
4.element()
5.peek()
6.exit
i
thi
Enter the Choice:
ipa
peek:11
1.add(E)
2.remove()
sh
3.poll()
4.element()
ate
5.peek()
6.exit
nk
==================================================================
Ve
faq:
define Deque<E>?
from Queue<E>.
=>We perform operations on both ends of Deque<E> and which is also known as
Double-Ended-Queue.
i
thi
public abstract E removeFirst();
ipa
public abstract E pollFirst();
-------------------------------------------------------------------
nk
(i)ArrayDeque<E> - Sequence
Ve
(ii)LinkedList<E> - NonSequence
-----------------------------------------------------------
Ex : DemoDeque.java
package maccess;
import java.util.*;
public class DemoDeque {
@SuppressWarnings("removal")
public static void main(String[] args) {
Deque<Integer> ob = new ArrayDeque<Integer>();
for(int i=1;i<=5;i++)
{
ob.add(new Integer(i));
}//end of loop
System.out.println(ob.toString());
ob.addFirst(new Integer(11));
ob.addLast(new Integer(12));
System.out.println(ob.toString());
ob.removeFirst();
i
ob.removeLast();
thi
System.out.println(ob.toString());
ob.pollFirst();
ob.pollLast();
System.out.println(ob.toString());
ipa
System.out.println("First ele:"+ob.getFirst());
System.out.println("Last ele:"+ob.getLast());
System.out.println(ob.toString());
MaSystem.out.println("peek first:"+ob.peekFirst());
System.out.println("peek last:"+ob.peekLast());
System.out.println(ob.toString());
ob.addFirst(new Integer(11));
ob.addFirst(new Integer(12));
ob.addLast(new Integer(11));
ob.addLast(new Integer(12));
sh
System.out.println(ob.toString());
ob.removeFirstOccurrence(new Integer(11));
System.out.println(ob.toString());
ate
ob.removeLastOccurrence(new Integer(12));
System.out.println(ob.toString());
}
nk
o/p:
Ve
[1, 2, 3, 4, 5]
[11, 1, 2, 3, 4, 5, 12]
[1, 2, 3, 4, 5]
[2, 3, 4]
First ele:2
Last ele:4
[2, 3, 4]
peek first:2
peek last:4
[2, 3, 4]
i
thi
[12, 2, 3, 4, 11, 12]
[12, 2, 3, 4, 11]
ipa
===================================================================
Note:
Ma
=>LinkedList<E> is the implementation of both List<E> and Deque<E>.
==================================================================
faq:
sh
define Iterable<E>?
(i)iterator()
nk
(ii)spliterator()
(iii)forEach()
Ve
(i)iterator():
interface.
(ii)spliterator():
Spliterator<T> interface.
(iii)forEach():
i
thi
elements from Collection<E> objects directly.
============================================================================
ipa
Note:
Limitation of Collection<E>:
Note:
==========================================================================
faq:
Ve
define Map<K,V>?
K - Key
V - Value
=>The following are some important methods of Map<K,V>:
i
thi
public abstract V get(java.lang.Object);
ipa
public abstract V remove(java.lang.Object);
---------------------------------------------------------------------
(a)HashMap<K,V>
(b)LinkedHashMap<E>
Ve
(c)TreeMap<K,V>
(d)Hashtable<K,V>
(a)HashMap<K,V>:
(b)LinkedHashMap<E>:
NonSynchronized class.
i
thi
(c)TreeMap<K,V>:
ipa
Primary Key and which is also NonSynchronized class.
Ma
(d)Hashtable<K,V>:
class.
sh
==============================================================================
ate
Dt : 16/11/2022
Ex-program:
nk
Note:
Ve
=>Construct one User defined class having the variables equal to the NonPrimary
EmployeeValues.java
package test;
public class EmployeeValues extends Object{
public String name,desg;
public int bSal;
public float totSal;
public EmployeeValues(String name,String desg,int bSal,float
totSal)
{
this.name=name;
this.desg=desg;
this.bSal=bSal;
this.totSal=totSal;
}
i
@Override
thi
public String toString() {
return name+"\t"+desg+"\t"+bSal+"\t"+totSal;
}
}
ipa
DemoMap.java(MainClass)
Ma
package maccess;
import java.util.*;
import test.*;
sh
public class DemoMap {
Map<String,EmployeeValues> ob=null;
nk
try(s;){
Ve
try {
while(true) {
System.out.println("****Choice****");
System.out.println("1.HashMap\n2.LinkedHashMap\n3.TreeMap\n4.Hashtable\n5.exit");
System.out.println("Enter the Choice:");
switch(Integer.parseInt(s.nextLine())) {
case 1:
ob = new HashMap<String,EmployeeValues>();
name="HashMap";
break;
i
thi
case 2:
ob = new LinkedHashMap<String,EmployeeValues>();
ipa
name="LinkedHashMap";
break;
Ma
case 3:
ob = new TreeMap<String,EmployeeValues>();
name="TreeMap";
sh
break;
case 4:
ate
ob = new Hashtable<String,EmployeeValues>();
name="Hashtable";
nk
break;
case 5:
Ve
System.exit(0);
default:
System.out.println("Invalid Choice...");
}//end of switch
System.out.println("perform operations on "+name);
xyz:
while(true){
System.out.println("====Choice====");
System.out.println("1.put(K,V)\n2.remove(object)\n3.get(object)\n4.keySet()\n5.values()\n6.
exit");
i
thi
System.out.println("Enter the Choice:");
switch(Integer.parseInt(s.nextLine())) {
ipa
case 1:
ob.put(new String(id),
Ve
new EmployeeValues(eName,desg,bSal,totSal));
ob.forEach((p,q)->
System.out.println(p+"\t"+q);
});
break;
case 2:
if(ob.isEmpty()) {
System.out.println("Map is empty...");
}else {
System.out.println("Enter empId:");
i
thi
String eId = new String(s.nextLine());
if(ob.containsKey(eId)) {
ipa
ob.remove(eId);
System.out.println("Details removed...");
Ma {
ob.forEach((p,q)->
System.out.println(p+"\t"+q);
sh
});
}else {
ate
System.out.println("Invalid eId...");
}
nk
break;
Ve
case 3:
if(ob.isEmpty()) {
System.out.println("Map is empty...");
}else {
System.out.println("Enter empId:");
String eId = new String(s.nextLine());
if(ob.containsKey(eId)) {
EmployeeValues ev = ob.get(eId);
System.out.println(ev.toString());
}else {
System.out.println("Invalid eId...");
i
thi
}
ipa
break;
case 4:
Ma if(ob.isEmpty()) {
System.out.println("Map is empty...");
}else {
sh
Set<String> ob2 = ob.keySet();
ob2.forEach((z)->
ate
System.out.println(z.toString());
nk
});
}
Ve
break;
case 5:
if(ob.isEmpty()) {
System.out.println("Map is empty...");
}else {
Collection<EmployeeValues> ob3 = ob.values();
ob3.forEach((z)->
System.out.println(z.toString());
});
i
thi
break;
case 6:
ipa
System.out.println("Operations stopped on "+name);
break xyz;
Ma default:
System.out.println("Invalid Choice...");
}//end of switch
sh
}//end of loop
}//end of loop
ate
}catch(Exception e) {e.printStackTrace();}
}//end of try
nk
}
Ve
o/p:
****Choice****
1.HashMap
2.LinkedHashMap
3.TreeMap
4.Hashtable
5.exit
====Choice====
i
thi
1.put(K,V)
2.remove(object)
ipa
3.get(object)
4.keySet()
5.values()
6.exit
Ma
Enter the Choice:
sh
1
A121
Raj
SE
16000
====Choice====
1.put(K,V)
2.remove(object)
3.get(object)
4.keySet()
5.values()
6.exit
i
thi
Enter the Choice:
ipa
Enter the empId:
A001
Ma
Enter the empName:
Ram
15000
====Choice====
Ve
1.put(K,V)
2.remove(object)
3.get(object)
4.keySet()
5.values()
6.exit
A021
i
thi
Alex
ipa
ME
14000
A001 Ram
Ma TE 15000 38400.0
====Choice====
ate
1.put(K,V)
2.remove(object)
nk
3.get(object)
4.keySet()
Ve
5.values()
6.exit
A001
A021
A121
====Choice====
1.put(K,V)
2.remove(object)
3.get(object)
i
thi
4.keySet()
5.values()
ipa
6.exit
Ram
Ma
TE 15000 38400.0
====Choice====
ate
1.put(K,V)
2.remove(object)
nk
3.get(object)
4.keySet()
Ve
5.values()
6.exit
Enter the Choice:
i
thi
ipa
Ma
sh
ate
================================================================
*imp
=>The statements which are used to retrieve elements from Collection<E> objects
Ve
syntax:
i
thi
Iterator<E> it = obj.iterator();
ipa
(b)ListIterator<E>:
(c)Enumeration<E>:
Ve
=========================================================================
Dt : 17/11/2022
i
thi
Ex-program : DemoCursorStatements.java
package maccess;
import java.util.*;
ipa
public class DemoCursorStatements {
public static void main(String[] args) {
Vector<Integer> v = new Vector<Integer>();
for(int i=1;i<=10;i++)
{
Ma
v.add(new Integer(i));
}//end of loop
System.out.println("*****ListIterator<T>****");
ListIterator<Integer> li = v.listIterator();
//creating implementation object for ListIterator<T>
sh
//This object will hold the reference of List<E> object
System.out.print("Forward : ");
while(li.hasNext()) {
ate
System.out.print(li.next()+" ");
}//end of loop
System.out.print("\nBackward : ");
while(li.hasPrevious()) {
System.out.print(li.previous()+" ");
nk
}//end of loop
System.out.println("\n****Enumeration<E>*****");
Enumeration<Integer> e = v.elements();
Ve
o/p:
*****ListIterator<T>****
Forward : 1 2 3 4 5 6 7 8 9 10
Backward : 10 9 8 7 6 5 4 3 2 1
****Enumeration<E>*****
1 2 3 4 5 6 7 8 9 10
i
thi
diagram:
ipa
Ma
sh
ate
====================================================================
faq:
nk
faq:
define Consumer<T>?
i
thi
=>Consumer<T> is a functional interface introduced by Java8 version and which
ipa
to forEach() method on Collection<E> objects.
Ma
structure of Consumer<T>:
}
nk
faq:
define BiConsumer<T,U>?
Ve
structure of BiConsumer<T,U>:
public interface java.util.function.BiConsumer<T, U>
=======================================================================
i
thi
*imp
define Enum<E>?
ipa
=>Enum<E> is a abstract class from java.lang package.
syntax:
Ma
enum Enum_name
{
sh
//elements
//variables
ate
//methods
}
nk
=>The constructors which are declared within the Enum<E> are automatically
private constructors.
EX-program:
Cars.java
package test;
public enum Cars {
alto(1200),dezire(1400),figo(1600);
public int price;
private Cars(int price)
{
this.price=price;
}
public int getPrice() {
return price;
}
i
public void setPrice(int price) {
thi
this.price = price;
}
ipa
DemoEnum.java(MainClass)
Ma
package maccess;
import test.Cars;
import java.util.Scanner;
sh
public class DemoEnum {
for(Cars k : c)
{
Ve
}//end of loop
for(Cars p : c)
{
System.out.println("Enter the price for "+p.toString());
p.setPrice(s.nextInt());
}//end of loop
for(Cars q : c)
i
thi
System.out.println(q.toString()+" Costs "+q.getPrice()+" thousand dollars");
}//end of loop
ipa
s.close();
o/p:
Ma
alto Costs 1200 thousand dollars
sh
dezire Costs 1400 thousand dollars
1700
2300
1700
diagram:
i
thi
ipa
Ma
sh
ate
====================================================================
Note:
Ex:
Ve
Week Days
Months in a Year
==========================================================================
Dt: 18/11/2022
3.WrapperClass Objects
4.Array Objects
5.Collection<E> objects
6.Map<K,V< objects
7.Enum<E> objects
i
thi
Complete List of Objects:
ipa
1.User defined class objects
2.String Objects
Ma
(a)String class Objects
(a)Byte Objects
ate
(b)Short Objects
(c)Integer Objects
nk
(d)Long Objects
(e)Float Objects
Ve
(f)Double Objects
(g)Character Objects
(h)Boolean Objects
4.Array Objects
5.Collection<E> objects
(a)Set<E>
i
thi
(i)HashSet<E> Objects
(ii)LinkedHashSet<E> Objects
ipa
(iii)TreeSet<E> Objects
(b)List<E>
Ma
(i)ArrayList<E> Objects
(ii)LinkedList<E> Objects
(iii)Vector<E> Objects
sh
=>Stack<E> Objects
(c)Queue<E>
ate
=>PriorityQueue<E> Objects
(d)Deque<E>
nk
(i)ArrayDeque<E> Objects
(ii)LinkedList<E> Objects
Ve
6.Map<K,V< objects
(a)HahMap<K,V> Objects
(b)LinkedHashMap<K,V> Objects
(c)TreeMap<K,V> Objects
(d)Hashtable<K,V> Objects
7.Enum<E> objects
======================================================================
faq:
(a)Container Objects
(b)Utility Objects
i
thi
(c)Cursor Objects
ipa
(a)Container Objects:
=>The Objects which perform operations on other objects are known as Utility
sh
Objects.
Ex:
ate
Scanner
StringTokenizer
nk
StringJoiner
Arrays
Ve
Collections
(c)Cursor Objects:
=>The Objects which are used to retrieve data from Collection objects are known
as Cursor Objects.
Ex:
Iterator<E>
ListIterator<E>
Enumeration<E>
Spliterator<T>
========================================================================
i
thi
faq:
ipa
(a)Collection<E>
(b)Collections
Ma
(a)Collection<E>:
(b)Collections:
sort()
Ve
binarySearch()
======================================================================
*imp
Application.
define process?
i
thi
define Task?
ipa
=>The part of process is known as Task.
Note:
Ma
=>According to Java Application,each program in application is a Task.
sh
define Multi-Tasking?
Note:
=>In the process of executing multiple tasks only some part of task is executed
Ve
known as Thread.
define Thread?
define Multi-threading?
i
thi
Diagram:
ipa
Ma
sh
ate
=====================================================================
*imp
interface
step-2 : The user defined implementation class must construct body for "run()"
i
thi
step-4 : create object for pre-defined "Thread" class and while object creation
ipa
we pass User defined implementation class object-reference as parameter
Ma
step-5 : execute run() method using start() method.
----------------------------------------------------------------
sh
ate
nk
Ve
Ex:
Register.java
package test;
public class Register implements Runnable{
@Override
public void run() {
for(int i=1;i<=5;i++) {
System.out.println("Registration for
"+Thread.currentThread().getName());
try {
Thread.sleep(2000);
}catch(Exception e) {e.printStackTrace();}
}
}
}
i
thi
Login.java
package test;
public class Login implements Runnable{
ipa
@Override
public void run(){
for(int i=1;i<=5;i++) {
System.out.println("Login for
Ma
"+Thread.currentThread().getName());
try {
Thread.sleep(2000);
}catch(Exception e) {e.printStackTrace();}
}
}
sh
}
ate
DemoThread1.java(MainClass)
package maccess;
import test.*;
public class DemoThread1 {
nk
t1.setName("User-1");
t2.setName("User-2");
t1.setPriority(Thread.MAX_PRIORITY-2);//8
t2.setPriority(Thread.MAX_PRIORITY-1);//9
t1.start();
t2.start();
System.out.println("Min Priority :
"+Thread.MIN_PRIORITY);
System.out.println("Max Priority :
"+Thread.MAX_PRIORITY);
System.out.println("Normal Priority :
"+Thread.NORM_PRIORITY);
}
}
i
thi
o/p:
ipa
Login for User-2
============================================================================
Dt : 19/11/2022
Note: Ma
=>In the process of executing Multiple threads,Multiple thread-stacks are
============================================================================
sh
faq:
ate
============================================================================
faq:
for execution.
(a)Time-Slicing Algorthm
i
thi
(a)Time-Slicing Algorthm:
=>In Time-Slicing algorithm all the multiple threads are executed based on
ipa
defined time-slice.
=>In Priority Based algorithm the threads are executed based on thread
sh
priorities.
syntax:
t1.setPriority(Thread.MAX_PRIORITY-2);
t2.setPriority(Thread.MAX_PRIORITY-1);
=======================================================================
*
imp
Ex : DemoThread2.java
i
thi
package maccess;
public class DemoThread2 {
public static void main(String[] args) {
ipa
new Thread(()->
{
for(int i=1;i<=5;i++) {
System.out.println("Registration for
"+Thread.currentThread().getName());
Ma try {
Thread.sleep(2000);
}catch(Exception e) {e.printStackTrace();}
}
}).start();
sh
new Thread(()->
{
ate
for(int i=1;i<=5;i++) {
System.out.println("Login for
"+Thread.currentThread().getName());
try {
Thread.sleep(2000);
nk
}catch(Exception e) {e.printStackTrace();}
}
}).start();
Ve
new Thread(()->
{
for(int i=1;i<=5;i++) {
System.out.println("View Books for
"+Thread.currentThread().getName());
try {
Thread.sleep(2000);
}catch(Exception e) {e.printStackTrace();}
}
}).start();
}
}
======================================================================
i
thi
ipa
Ma
sh
ate
nk
Ve