Unit 5
Unit 5
implements
Set List
HashSet
SortedSet
extends
implements
extends
implements
SortedMap
implements
extends
LinkedHashMap
Collection Implementations
index 0 1 2 3 4
import java.util.ArrayList;
One
public class MyArrayList { Two
public static void main(String args[ ]) { Three
“John” “Luke”
“Paul”
“Mark” “Fred”
“Peter”
Table of Contents:
{address=Manila, name=Jody, id=1001}
Map Implementations - Linked Hash Map
Jody
import java.util.*;
446
public class MyLinkedHashMap { Manila
public static void main(String args[ ]) { Savings
int iNum = 0;
LinkedHashMap myMap = new LinkedHashMap( );
myMap.put("name", "Jody");
myMap.put("id", new Integer(446));
myMap.put("address", "Manila");
myMap.put("type", "Savings");
Collection values = myMap.values( );
Iterator iterator = values.iterator( );
while(iterator.hasNext()) {
System.out.println(iterator.next( ));
}
}
}
Map Implementations- Tree Map
import java.util.*;
Printing the VALUES....
public class MyTreeMap {
Manila
public static void main(String args[])446
{
Jody
TreeMap treeMap = new TreeMap( );
treeMap.put("name", "Jody");
treeMap.put("id", new Integer(446));
treeMap.put("address", "Manila");
Collection values = treeMap.values()
Iterator iterator = values.iterator( );
System.out.println("Printing the
VALUES....");
while (iterator.hasNext()) {
System.out.println(iterator.next(
));
}
}
}
Collection Classes Summary
Class Map Set List Ordered Sorted
HashMap X No No
Hashtable X No No
TreeMap X Sorted By natural order or custom
comparison rules
LinkedHashMap X By insertion order or No
last access order
HashSet X No No
TreeSet X Sorted By natural order or custom
comparison rules
LinkedHashSet X By insertion order or No
last access order
ArrayList X By index No
Vector X By index No
LinkedList X By index No
I/O Stream
• Input Stream: It is a stream which may be connected with different i/p devices like
keyboard, file or network socket. It is used to generate data.
• Output Stream: It is used to produce data and may be connected with different output
devices like monitor, file, network socket.
Files and Streams
• File processing with classes in package java.io
• FileInputStream for
byte-by-byte input from a file
• FileOutputStream for
byte-by-byte output to a file
• FileReader for char-by-char
input from a file
• FileWriter for char-by-char
output to a file
Difference
byte Stream char Stream
1. It deals with byte. 1. It deals with char or Strings.
2. Used to manipulate data in 2. Used to manipulate data in
binary format. Unicode.
3. Mainly uses audio, video, img, 3. Can be used only with txt files.
txt files. 4. Reader & Writer are top two
4. InputStream and OutputStream abstract classes of char Stream.
are top two abstract classes of
byte Stream.
byte-by-byte
• Constructors, Methods Of FileInputStream
• CONSTRUCTORS:
• FileInputStream(String fname);
• FileInputStream(File F);
• FileInputStream(String fold, String fname);
• METHODS:
• int read(); // n= fis.read(); // n is the byte read.
Assignment Questions
Write a Java program to create an ArrayList of integers and perform the following operations:
• Add 5 integers entered by the user.
• Remove the element at index 2.
• Update the value at index 1.
• Print the final ArrayList.
Write a Java program that demonstrates the use of a Vector to store student names. Perform the following
operations:
• Add 4 student names to the Vector.
• Insert a new student at index 2.
• Remove the last student from the Vector.
• Display the Vector before and after modifications.
Write a Java program using a Hashtable to store employee IDs (Integer) and their corresponding names
(String). Perform the following:
• Add 3 employee records.
• Remove an employee using their ID.
• Search for an employee using an ID and display their name.
PIMPRI CHINCHWAD UNIVERSITY
49
Assignment Questions
Write a Java program to implement a stack using the Stack class. The program should allow:
• Pushing 5 integer values onto the stack.
• Popping the top 2 values from the stack.
• Displaying the stack after each operation.
Write a Java program that demonstrates the use of Lambda Expressions to:
• Create a list of integers.
• Use the forEach method with a Lambda expression to print each element.
• Use a Lambda expression to filter and display only even numbers.
Write a Java program to copy the contents of one file to another using FileInputStream and
FileOutputStream (byte stream).
• Read data from source.txt and write it to destination.txt.
• Ensure proper exception handling.
Assignment Questions
Write a Java program to implement a stack using the Stack class. The program should allow:
• Pushing 5 integer values onto the stack.
• Popping the top 2 values from the stack.
• Displaying the stack after each operation.
Write a Java program that demonstrates the use of Lambda Expressions to:
• Create a list of integers.
• Use the forEach method with a Lambda expression to print each element.
• Use a Lambda expression to filter and display only even numbers.
Write a Java program to copy the contents of one file to another using FileInputStream and
FileOutputStream (byte stream).
• Read data from source.txt and write it to destination.txt.
• Ensure proper exception handling.
Assignment Questions
Write a Java program that reads a text file using FileReader and BufferedReader, counts the
number of lines, and displays the total count.
• Example: If data.txt has 10 lines, the program should output Total lines: 10.
Write a Java program using the File class to:
• Check if a file named example.txt exists.
• If it does not exist, create the file.
• Display the file's absolute path, size (in bytes), and whether it is readable/writable.
Write a GUI program using Swing to create a window with a button labeled "Click Me!".
• When the button is clicked, display a message "Button Clicked!" using an ActionListener.
Write a Java program using MouseListener to:
• Detect when the user clicks, enters, or exits a JFrame window.
• Display messages like "Mouse Clicked!", "Mouse Entered!", etc. in the console.