Java
Collection: Interface
Collection
add(Obj) addAll(Col) remove(Obj) removeAll(Col) retainAll(Col): remove everything except the `Col` collections. contains(Obj) containsAll(Col) size() iterator() toArray()
- Array-Like
List: Ordered, Allows Duplicates, Heterogeneous
set(int index, int number) get(int index) firstElement() lastElement() isEmpty()
- ArrayList: Dynamic Array, Best for retrieval
- CopyOnWriteArrayList: Thread-safe, iterator can't perform remove operation.
- CopyOnWriteArrayList: Thread-safe, iterator can't perform remove operation.
- LinkedList: Best for Intersection
- Vector: Thread-safe
Stack: LIFO
push(int) Object pop(int) peek(): returns top element of the stack int search(int num)
- ArrayList: Dynamic Array, Best for retrieval
Set: Not Allows Duplicates, Not Ordered, Heterogeneous
- HashSet: Hash Table
- LinkedHashSet: Ordered is Preserved,
- LinkedSet
- CopyOnWriteArraySet: same as CopyOnWriteArraySet
Shorted Set
last() first() headSet() tailSet() subSet() comparator()
Navigable Set
floor() lower() ceiling() higher() pollFirst() pollLast() decendingSet()
- TreeSet: Balanced Tree, Only can use with compareable interface
- TreeSet: Balanced Tree, Only can use with compareable interface
- HashSet: Hash Table
Queue
add() & offer(): adds element remove(): removes head of queue peek(): returns head of queue poll(): return and removes head of queue
- PriorityQueue: Binary heap
- Key-Value
- Map
- HashMap: Not ordered
- LinkedHashMap: Ordered, Doubly Link List
- ConcurrentHashMap: Thread Safe
- WeakHashMap: if a key in the map is no longer referenced anywhere outside the WeakHashMap, it becomes eligible for garbage collection.
- IdentityHashMap: equal only if they are the exact same object in memory, not merely equal in content.
- Hashtable -- Dictionary
- Properties
- ShortedMap
- NavigableMap
- TreeMap: Balanced Tree, Only can use with compareable interface
- NavigableMap
- HashMap: Not ordered
- Map
- Array-Like
Example
import java.util.function.*;
import java.util.*;
import java.util.stream.*;
public class Collections {
Collections(int a, int b) {
System.out.println(a / b);
}
static void add(int a, int b) {
System.out.println(a + b);
}
void sub(int a, int b) {
System.out.println(a - b);
}
<T, V> T num(T a, V b) {
return a;
}
public static void main(String[] args) {
// Lambda =============================================================
Lambda lambda = (a, b) -> System.out.println(a + b);
lambda.run(32, 32);
Lambda coleanStatic = Collections::add;
coleanStatic.run(32, 32);
var col = new Collections(32, 32);
Lambda colean = col::sub;
colean.run(32, 32);
Lambda coleanNew = Collections::new;
coleanNew.run(32, 32);
Predicate<Float> p = (x) -> x > 32;
System.out.println(p.test(32.0f));
Function<Float, Boolean> f = (x) -> x > 32;
System.out.println(f.apply(32.0f));
Consumer<Float> c = (x) -> System.out.println(x);
c.accept(32.0f);
Supplier<String> s = () -> "Hello World";
System.out.println(s.get());
// Streams ============================================================
var list = new ArrayList<Integer>(List.of(32, 25, 89, 245, 13, 67, 90));
List<Integer> li = list.stream().filter(i -> i > 30).collect(Collectors.toList());
System.out.println(li);
long count = list.stream().map(i -> i*23).count();
var time1 = System.currentTimeMillis();
li = list.stream().map(i -> i*23).collect(Collectors.toList());
var time2 = System.currentTimeMillis();
System.out.println("Time Sequential: " + (time1 - time2));
time1 = System.currentTimeMillis();
li = list.parallelStream().map(i -> i*23).collect(Collectors.toList());
time2 = System.currentTimeMillis();
System.out.println("Time Paraller: " + (time1 - time2));
// Generics ============================================================
var g = new Generics<String>("This is Generics");
System.out.println(g.getData());
System.out.println(col.num(32, 32));
}
}
@FunctionalInterface
interface Lambda {
void run(int a, int b);
}
class Generics<T> {
private T data;
Generics(T data) {
this.data = data;
}
T getData() {
return this.data;
}
}
Collections: Utility class
- Collections
- sort()
- reverse()
- shuffle()
- binarySearch()
- disjoint()
Cursors
Properties | Enumeration | Iterator | ListIterator |
---|---|---|---|
Used For | Legacy classes objects | For any collection object | For list class objects |
Legacy | Yes | No | No |
Direction Flow | Forward | Forward | Forward and Backward |
Access we can get | Only Read | Read and Remove | Read, Remove, Replace and add |
Object Creation | By using elements() method of Vector class | By using iterator() method of Collection(Interface) | By using ListIterator() method of List(interface) |
Methods | hasMoreElements() nextElement() |
hasNext() next() remove() |
hasNext() next() nextIndex() hasPrevious() Previous() PreviousIndex() remove() Add(Object o) Set(Object o) |
Threads
Executor Framework: https://www.perplexity.ai/search/explain-this-https-web-archive-nFLVgSGPSy64MUl0.Uo3lA
Blocking Thread
import java.math.BigInteger;
public class Multithreading {
public static void main(String[] args) {
Thread thread = new Thread(() -> {
System.out.println(
"Thread Name: " + Thread.currentThread().getName() +
", Thread Priority: " + Thread.currentThread().getPriority() + "\n"
);
});
thread.setName("Thread1");
thread.setPriority(Thread.MAX_PRIORITY);
thread.setUncaughtExceptionHandler((t, e) -> {
System.out.println("Error in Thread: " + t.getName());
System.out.println("Error Message: " + e.getMessage());
});
System.out.println(
"Thread Name: " + Thread.currentThread().getName() +
", Thread Priority: " + Thread.currentThread().getPriority() + "\n"
);
thread.start();
var myThread = new MyThread();
myThread.start();
var blockThread = new BlockingThread(
"Thread 3",
new BigInteger("2"), new BigInteger("10")
);
blockThread.start();
blockThread.interrupt();
}
static class MyThread extends Thread {
@Override
public void run() {
this.setName("Thread2");
System.out.println(
"Thread Name: " + this.getName() +
", Thread Priority: " + this.getPriority() + "\n"
);
}
}
static class BlockingThread extends Thread {
private BigInteger base;
private BigInteger power;
BlockingThread(String string, BigInteger base, BigInteger power) {
this.setName(string);
this.base = base;
this.power = power;
}
@Override
public void run() {
BigInteger result = BigInteger.ONE;
for (
BigInteger i = BigInteger.ZERO;
i.compareTo(power) != 0;
i = i.add(BigInteger.ONE)
) {
if (this.currentThread().isInterrupted()) {
System.out.println("Error: System taking too long to calc.");
result = BigInteger.ZERO;
break;
}
System.out.println(i);
result = result.multiply(this.base);
}
System.out.println("Calc: " + result);
}
}
}
Java Swing
Every Layout Information: https://www.iitk.ac.in/esc101/05Aug/tutorial/uiswing/layout/visual.html
Exercise
Write a program to check number is even or odd using if else.
import java.util.Scanner;
/**
* Java Program - Check if Number is Even or Odd
*/
public class Example {
public static void main(String[] args) {
//create a scanner to read bytes from console entered by user via keyboard
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number : ");
//read integer from user
int a = scanner.nextInt();
//check if the number is even or odd
if(a%2==0) {
System.out.println("The number is even.");
} else {
System.out.println("The number is odd.");
}
//close the scanner
scanner.close();
}
}
O/P
Enter a number : 4
The number is even.
Write a program to check switch case statement using character datatype.
public class SwitchExample {
public static void main(String[] args) {
//Declaring a variable for switch expression
String name="a";
//Switch expression
switch(name){
//Case statements
case "a": System.out.println("Aman is in Database.");
break;
case "r": System.out.println("Ravi is in Database.");
break;
case "g": System.out.println("Gita is in Database.");
break;
//Default case statement
default:System.out.println("Name is not in database.");
}
}
}
Aman is in Database.
Develop a program to print command line argument using for loop.
class CommandLineExample{
public static void main(String args[]){
System.out.println("Your first argument is: "+args[0]);
for (int i; i <= 5; i++) {
System.out.println(arg[0]);
}
}
}
O/P
> java CommandLineExample Hello
Your first argument is: Hello
Hello
Hello
Hello
Hello
Hello
Hello
Develop a program to show the use of implicit typecasting.
public class ImplicitTypecastingExample {
public static void main(String args[]) {
byte p = 12;
System.out.println("byte value : "+p);
// Implicit Typecasting
short q = p;
System.out.println("short value : "+q);
int r = q;
System.out.println("int value : "+r);
long s = r;
System.out.println("long value : "+s);
float t = s;
System.out.println("float value : "+t);
double u = t;
System.out.println("double value : "+u);
}
}
byte value : 12
short value : 12
int value : 12
long value : 12
float value : 12.0
double value : 12.0
Write a program to implement different types of constructors to perform addition of complex numbers.
public class ComplexNumber{
//for real and imaginary parts of complex numbers
double real, img;
//constructor to initialize the complex number
ComplexNumber(double r, double i){
this.real = r;
this.img = i;
}
public static ComplexNumber sum(ComplexNumber c1, ComplexNumber c2)
{
//creating a temporary complex number to hold the sum of two numbers
ComplexNumber temp = new ComplexNumber(0, 0);
temp.real = c1.real + c2.real;
temp.img = c1.img + c2.img;
//returning the output complex number
return temp;
}
public static void main(String args[]) {
ComplexNumber c1 = new ComplexNumber(5.5, 4);
ComplexNumber c2 = new ComplexNumber(1.2, 3.5);
ComplexNumber temp = sum(c1, c2);
System.out.printf("Sum is: "+ temp.real+" + "+ temp.img +"i");
}
}
O/P
Sum is: 6.7 + 7.5i
Write a program to show the use of all methods of String class.
public class StringMethodsDemo {
public static void main(String[] args) {
String targetString = "Java is fun to learn";
String s1= "JAVA";
String s2= "Java";
String s3 = " Hello Java ";
System.out.println("Char at index 2(third position): " + targetString.charAt(2));
System.out.println("After Concat: "+ targetString.concat("-Enjoy-"));
System.out.println("Checking equals ignoring case: " +s2.equalsIgnoreCase(s1));
System.out.println("Checking equals with case: " +s2.equals(s1));
System.out.println("Checking Length: "+ targetString.length());
System.out.println("Replace function: "+ targetString.replace("fun", "easy"));
System.out.println("SubString of targetString: "+ targetString.substring(8));
System.out.println("SubString of targetString: "+ targetString.substring(8, 12));
System.out.println("Converting to lower case: "+ targetString.toLowerCase());
System.out.println("Converting to upper case: "+ targetString.toUpperCase());
System.out.println("Triming string: " + s3.trim());
System.out.println("searching s1 in targetString: " + targetString.contains(s1));
System.out.println("searching s2 in targetString: " + targetString.contains(s2));
char [] charArray = s2.toCharArray();
System.out.println("Size of char array: " + charArray.length);
System.out.println("Printing last element of array: " + charArray[3]);
}
}
O/P
Char at index 2(third position): v
After Concat: Java is fun to learn-Enjoy-
Checking equals ignoring case: true
Checking equals with case: false
Checking Length: 20
Replace function: Java is easy to learn
SubString of targetString: fun to learn
SubString of targetString: fun
Converting to lower case: java is fun to learn
Converting to upper case: JAVA IS FUN TO LEARN
Triming string: Hello Java
searching s1 in targetString: false
searching s2 in targetString: true
Size of char array: 4
Printing last element of array: a
Write a program to implement all methods of StringBuffer class.
class StringBufferExample{
public static void main(String args[]){
StringBuffer sb=new StringBuffer("Hello");
sb.append(" Java");
System.out.println(sb);
sb.insert(1,"Java");
System.out.println(sb);
sb.replace(1,3,"Java");
System.out.println(sb);
sb.delete(1,3);
System.out.println(sb);
sb.reverse();
System.out.println(sb);
System.out.println(sb.capacity());
sb.ensureCapacity(10);
System.out.println(sb.capacity());
}
}
O/P
Hello Java
HJavaello Java
HJavavaello Java
Hvavaello Java
avaJ olleavavH
21
21
Write a program to implement single inheritance.
class Animal{
void eat(){System.out.println("eating...");}
}
class Dog extends Animal{
void bark(){System.out.println("barking...");}
}
class TestInheritance{
public static void main(String args[]){
Dog d=new Dog();
d.bark();
d.eat();
}}
O/P
barking...
eating...
Write a program to implement multilevel inheritance.
class Animal{
void eat(){System.out.println("eating...");}
}
class Dog extends Animal{
void bark(){System.out.println("barking...");}
}
class BabyDog extends Dog{
void weep(){System.out.println("weeping...");}
}
class TestInheritance2{
public static void main(String args[]){
BabyDog d=new BabyDog();
d.weep();
d.bark();
d.eat();
}}
O/P
weeping...
barking...
eating...
Develop a program to find area of rectangle and circle using interface.
class FindLargestShape {
public static void main(String arg[]) {
Rectangle r = new Rectangle(10, 4);
System.out.println("Rectangle Area : " + r.getArea());
System.out.println();
}
}
class Rectangle {
double length;
double breadth;
Rectangle(double length, double breadth) {
this.length = length;
this.breadth = breadth;
}
double getArea() {
return length * breadth;
}
}
Write a program to implement user defined packages in terms of creating a new package and importing the same.
package example;
public class gfg {
public void show()
{
System.out.println("Hello geeks!! How are you?");
}
public static void main(String args[])
{
gfg obj = new gfg();
obj.show();
}
}
import example.gfg;
public class GFG {
public static void main(String args[])
{
gfg obj = new gfg();
System.out.println(obj.show());
}
}
O/P
Hello geeks!! How are you?
Write a program to implement two thread use sleep method.
class TestSleepMethod1 extends Thread{
public void run(){
for(int i=1;i<5;i++){
// the thread will sleep for the 500 milli seconds
try{Thread.sleep(500);}catch(InterruptedException e){System.out.println(e);}
System.out.println(i);
}
}
public static void main(String args[]){
TestSleepMethod1 t1=new TestSleepMethod1();
TestSleepMethod1 t2=new TestSleepMethod1();
t1.start();
t2.start();
}
}
11
2
2
3
3
4
4
Develop a program to accept a password from the user and throw "Authentication Failure" exception if the password is incorrect.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
class AuthenticationException extends Exception {
public AuthenticationException(String message) {
super(message);
}
}
public class AuthenticationExcDemo {
public static void main(String[] args) {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String pwd;
try {
System.out.print("Enter password :: ");
pwd = br.readLine();
if(!pwd.equals("123"))
throw new AuthenticationException("Incorrect password\nType correct password");
else
System.out.println("Welcome User !!!");
}
catch (IOException e) {
e.printStackTrace();
}
catch (AuthenticationException a) {
a.printStackTrace();
}
System.out.println("BYE BYE");
}
}
Develop a program to draw a polygon using applet.
import java.awt.*;
import javax.swing.*;
public class poly extends JApplet {
// called when applet is started
public void init()
{
// set the size of applet to 300, 300
setSize(200, 200);
show();
}
// invoked when applet is started
public void start()
{
}
// invoked when applet is closed
public void stop()
{
}
public void paint(Graphics g)
{
// x coordinates of vertices
int x[] = { 10, 30, 40, 50, 110, 140 };
// y coordinates of vertices
int y[] = { 140, 110, 50, 40, 30, 10 };
// number of vertices
int numberofpoints = 6;
// set the color of line drawn to blue
g.setColor(Color.blue);
// draw the polygon using drawPolygon function
g.drawPolygon(x, y, numberofpoints);
}
}
Develop an applet for drawing a human face.
import java.applet.*;
import java.awt.*;
public class Human_Face extends Applet
{
//Initialize the applet
public void init()
{
setBackground(Color.white);
}
//Draw the human face
public void paint(Graphics g)
{
//Change color to cream
Color clr=new Color(255,179,86);
g.setColor(clr);
//Draw and fill the face
g.drawOval(100,100,250,300);
g.fillOval(100,100,250,300);
//Change color to black
g.setColor(Color.black);
//Draw the left eye
g.drawOval(160,185,40,25);
g.fillOval(160,185,40,25);
//Draw the right eye
g.drawOval(250,185,40,25);
g.fillOval(250,185,40,25);
//Draw the Left Eyebrow
g.drawArc(160,170,35,10,0,180);
//Draw the Right Eyebrow
g.drawArc(250,170,35,10,0,180);
//Draw the Nose
g.drawLine(210,265,210,275);
g.drawLine(240,265,240,275);
g.drawArc(210,275,30,10,0,-180);
//Draw the smile
g.drawArc(175,300,100,50,0,-180);
}
}
Develop a program to draw square inside a circle using applet.
import java.applet.";
import java.awt.*;
/* <applet code=shapes width=10 height=10> </applet>*/
public class shapes extends Applet {
public void paint(Graphics g) {
g.drawString("Square inside a circle", 150,110);
g.drawOval(180,10,80,80);
g.drawRect(192,22,55,55):
g.drawString("Circle inside a square", 290, 110);
g.drawRect(290, 10, 80, 80);
g.drawOval(290, 10, 80, 80);
}
}
Develop a program to copy characters from one file to another.
import java.io.*;
import java.util.*;
class Copyfile {
public static void main(String arg[]) throws Exception {
Scanner sc = new Scanner(System.in);
System.out.print("Provide source file name :");
String sfile = sc.next();
System.out.print("Provide destination file name :");
String dfile = sc.next();
FileReader fin = new FileReader(sfile);
FileWriter fout = new FileWriter(dfile, true);
int c;
while ((c = fin.read()) != -1) {
fout.write(c);
}
System.out.println("Copy finish...");
fin.close();
fout.close();
}
}