Unit-5 : Collections
ArrayList supports dynamic arrays that can grow or shrink as needed.
The ArrayList class implements the List interface.
Can contain duplicate elements.
Maintains insertion order.
Non synchronized.
Manipulation is slow.
LinkedList class uses a doubly linked list to store the elements.
It implements the List interface.
Can contain duplicate elements.
Maintains insertion order.
Non synchronized.
Manipulation is fast
HashSet class is used to create a collection that uses a hash table for storage.
It implements the Set interface.
Contains unique elements only.
Allows null value.
Non synchronized.
HashSet doesn't maintain the insertion order.
HashSet is the best for search operations
TreeSet class implements the Set interface that uses a tree for storage.
Contains unique elements only like HashSet.
Access and retrieval times are quiet fast.
Doesn't allow null element.
Non synchronized.
Maintains ascending order.
// 6 methods from collection/ ArrayList/LinkedList/HashSet/TreeSet
Methods of Collection Interface:
1)public boolean add(Object element)- It is used to insert an element in this collection.
2)public boolean addAll(Collection c)- It is used to insert the specified collection elements in the invoking collection.
3)public boolean remove(Object element)- It is used to delete an element from the collection.
4)public boolean removeAll(Collection c)- It is used to delete all the elements of the specified collection from the invoking collection.
5)public int size()- It returns the total number of elements in the collection.
6)Public void clear()- It removes the total number of elements from the collection.
1) collections program with 6-8 methods. (ArrayList/LinkedList/HashSet/TreeSet as code is same).
import java.util.*;
class ArrayListDemo {
public static void main(String[] args) {
ArrayList<String> al1 = new ArrayList<>();
//in place of ArrayList use can use LinkedList / HashSet / TreeSet
al1.add("Apple");
al1.add("Banana");
al1.add("Mango");
System.out.println("al1 after add(): " + al1);
ArrayList<String> al2 = new ArrayList<>();
al2.add("Orange");
al2.add("Grapes");
System.out.println("al2 after add(): " + al2);
al1.addAll(al2);
System.out.println("al1 after adding al2: " + al1);
al1.remove("Banana");
System.out.println("al1 after remove: " + al1);
al1.removeAll(al2);
System.out.println( al1);
System.out.println("al1 contains Apple" + al1.contains("Apple"));
System.out.println("al1 contains Grapes " + al1.contains("Grapes"));
System.out.println("al1 contains all elements of al2? " + al1.containsAll(al2));
System.out.println("Size of al1: " + al1.size());
al1.clear();
System.out.println("al1 after clear(): " + al1);
}
}
2) Write a program for read, write and copy data from source file to destination file.
import java.io.*;
// Reading source file and writing content to target file byte by byte
}
}
}
3)Write java program to read the data from consoleand write to console.
Import java.io.*;
class ConsoleIOUsingJavaIO {
public static void main(String[] args) {
try {
BufferedReader br = new BufferedReader (new InputStreamBr(System.in));
System.out.print("Enter your name: ");
String name = br.readLine();
System.out.print("Enter your age: ");
int age = Integer.parseInt(br.readLine());
System.out.print("Enter your marks: ");
float marks = Float.parseFloat(br.readLine());
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Marks: " + marks);
}
catch (IOException e) {
System.out.println("An error occurred during input.");
}
}
}
4)StringTokenizer program
It allows you to break a String into tokens.
public static void main(String[] args)
StringTokenizer str = new StringTokenizer("My Name is Khanishk");
StringTokenizer temp = new StringTokenizer("");
int count = str.countTokens();
System.out.println(count);
System.out.println("(Empty String) : "+temp.hasMoreTokens());
System.out.println("\nTraversing the String:");
while(str.hasMoreTokens()){
System.out.println(str.nextElement());
}
}




Comments
Post a Comment