0% found this document useful (0 votes)
3 views20 pages

UNIT4

The document provides an overview of Java's Collection Framework and various I/O operations, including examples of classes like ArrayList, Vector, Hashtable, and Stack. It also covers concepts such as Generics, Enumeration, Iterator, and file handling with Byte and Character Streams. Additionally, it discusses user input with Scanner and date/time management with Calendar.

Uploaded by

sidhsingh250
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views20 pages

UNIT4

The document provides an overview of Java's Collection Framework and various I/O operations, including examples of classes like ArrayList, Vector, Hashtable, and Stack. It also covers concepts such as Generics, Enumeration, Iterator, and file handling with Byte and Character Streams. Additionally, it discusses user input with Scanner and date/time management with Calendar.

Uploaded by

sidhsingh250
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

MCA SEM2

UNIT 4
Presented By : Dr. Abhishek Roy
COURSE CODE :CSE21949
COURSE : Object Oriented Programming with
Java
Introduction to Collection Framework
import java.util.*;

public class ListDemo {


public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("Java");
System.out.println(list);
}
}

Explanation: ArrayList is a resizable array implementation of List interface.


Generics
import java.util.*;

public class GenericsDemo {


public static void main(String[] args) {
List<Integer> list = new ArrayList<>();
list.add(10);
System.out.println(list.get(0));
}
}

Explanation: Generics allow type-safe collections (e.g., only Integer in the list).
ArrayList

import java.util.*;

public class ArrayListDemo {


public static void main(String[] args) {
ArrayList<String> names = new ArrayList<>();
names.add("Alice");
System.out.println(names);
}
}

Explanation: ArrayList dynamically resizes as elements are added.


Vector

import java.util.*;

public class VectorDemo {


public static void main(String[] args) {
Vector<Integer> v = new Vector<>();
v.add(5);
System.out.println(v);
}
}

Explanation: Vector is synchronized and similar to ArrayList.


HashTable

import java.util.*;

public class HashtableDemo {


public static void main(String[] args) {
Hashtable<Integer, String> map = new Hashtable<>();
map.put(1, "Java");
System.out.println(map.get(1));
}
}

Explanation: Hashtable stores key-value pairs with synchronized access.


Stack

import java.util.*;

public class StackDemo {


public static void main(String[] args) {
Stack<String> stack = new Stack<>();
stack.push("Java");
System.out.println(stack.peek());
}
}

Explanation: Stack is LIFO - Last In First Out data structure.


Enumeration

import java.util.*;

public class EnumerationDemo {


public static void main(String[] args) {
Vector<Integer> v = new Vector<>();
v.add(1); v.add(2);
Enumeration<Integer> e = v.elements();
while(e.hasMoreElements())
System.out.println(e.nextElement());
}
}

Explanation: Enumeration is used to iterate legacy collections like Vector.


Iterator
import java.util.*;

public class IteratorDemo {


public static void main(String[] args) {
List<String> list = Arrays.asList("A", "B");
Iterator<String> it = list.iterator();
while(it.hasNext())
System.out.println(it.next());
}
}

Explanation: Iterator is the modern way to traverse collections.


Random

import java.util.*;

public class RandomDemo {


public static void main(String[] args) {
Random rand = new Random();
System.out.println("Random Number: " + rand.nextInt(100));
}
}

Explanation: Random is used to generate pseudo-random numbers (0 to 99 here).


Scanner

import java.util.*;

public class ScannerDemo {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter name: ");
String name = sc.nextLine();
System.out.println("Hello, " + name);
}
}

Explanation: Scanner is used for user input from the console (keyboard).
Calendar

import java.util.*;

public class CalendarDemo {


public static void main(String[] args) {
Calendar cal = Calendar.getInstance();
System.out.println("Year: " + cal.get(Calendar.YEAR));
}
}

Explanation: Calendar provides date and time-related functions.


Properties
import java.util.*;

public class PropertiesDemo {


public static void main(String[] args) {
Properties prop = new Properties();
prop.setProperty("user", "admin");
prop.setProperty("password", "1234");
System.out.println("User: " + prop.getProperty("user"));
}
}

Explanation: Properties is used to store key-value pairs (configurations).


Byte Streams

import java.io.*;

public class ByteStreamDemo {


public static void main(String[] args) throws IOException {
FileOutputStream out = new FileOutputStream("data.txt");
out.write(65); // Writes ASCII 'A'
out.close();
}
}

Explanation: FileOutputStream writes raw bytes to a file.


Character Streams

import java.io.*;

public class CharStreamDemo {


public static void main(String[] args) throws IOException {
FileWriter fw = new FileWriter("output.txt");
fw.write("Hello Java");
fw.close();
}
}

Explanation: FileWriter writes characters to a file.


Text Input/Output

import java.io.*;

public class TextIODemo {


public static void main(String[] args) throws IOException {
BufferedWriter bw = new BufferedWriter(new FileWriter("note.txt"));
bw.write("Java I/O Example");
bw.close();
}
}

Explanation: BufferedWriter is used for efficient text output.


Binary Input/Output

import java.io.*;

public class BinaryIODemo {


public static void main(String[] args) throws IOException {
DataOutputStream dos = new DataOutputStream(new FileOutputStream("bin.dat"));
dos.writeInt(123);
dos.writeDouble(45.67);
dos.close();
}
}

Explanation: DataOutputStream writes primitive data types in binary format.


Random Access of File Operations

import java.io.*;

public class RandomAccessDemo {


public static void main(String[] args) throws IOException {
RandomAccessFile raf = new RandomAccessFile("random.txt", "rw");
raf.writeUTF("Hello");
raf.seek(0);
System.out.println(raf.readUTF());
raf.close();
}
}

Explanation: RandomAccessFile allows read/write at any position in the file.


File Management
import java.io.*;

public class FileMgmtDemo {


public static void main(String[] args) {
File file = new File("test.txt");
if(file.exists())
System.out.println("File exists.");
else
System.out.println("File not found.");
}
}

Explanation: File class is used to check file properties.


THE END

You might also like