2. SYLLABUS
◦ The Collection Framework
◦ Collection interfaces
◦ Collection classes(ArrayList, LinkedList, Hash set)
◦ Accessing a collection via an iterator, Vector,
◦ more utility class: StringTokenizer
◦ Date
3. The Collection Framework
◦ Any group of individual objects which are represented as a single unit is known as a collection of objects.
◦ In Java, a separate framework named the “Collection Framework” has been defined in JDK 1.2 which holds all
the collection classes and interface in it.
◦ In Java, Collection interface (java.util.Collection) and Map interface (java.util.Map) are the two main
“root” interfaces of Java collection classes.
◦ A framework is a set of classes and interfaces which provide a ready-made architecture.
◦ In order to implement a new feature or a class, there is no need to define a framework.
◦ However, an optimal object-oriented design always includes a framework with a collection of classes such
that all the classes perform the same kind of task.
4. Advantages of the Collection Framework
◦ Since the lack of a collection framework gave rise to the above set of disadvantages, the following are the advantages of
the collection framework.
◦ Consistent API: The API has a basic set of interfaces like Collection, Set, List, or Map, all the classes (ArrayList, LinkedList,
Vector, etc) that implement these interfaces have some common set of methods.
◦ Reduces programming effort: A programmer doesn’t have to worry about the design of the Collection but rather he can
focus on its best use in his program. Therefore, the basic concept of Object-oriented programming (i.e.) abstraction has
been successfully implemented.
Increases program speed and quality: Increases performance by providing high-performance implementations of useful
data structures and algorithms because in this case, the programmer need not think of the best implementation of a specific
data structure. He can simply use the best implementation to drastically boost the performance of his algorithm/program.
5. Collection classes(ArrayList, LinkedList, Hash set)
◦ The Collection in Java is a framework that provides an architecture to store and manipulate the group of objects.
◦ Java Collections can achieve all the operations that you perform on a data such as searching, sorting, insertion,
manipulation, and deletion.
◦ Java Collection means a single unit of objects. Java Collection framework provides many interfaces (Set, List,
Queue, Deque) and classes (ArrayList, Vector, LinkedList, PriorityQueue, HashSet, LinkedHashSet, TreeSet).
◦ What is Collection framework
◦ The Collection framework represents a unified architecture for storing and manipulating a group of objects. It has:
◦ Interfaces and its implementations, i.e., classes
◦ Algorithm
6. ArrayList
◦ The ArrayList class implements the List interface.
◦ It uses a dynamic array to store the duplicate element of different data types.
◦ The ArrayList class maintains the insertion order and is non-synchronized.
◦ The elements stored in the ArrayList class can be randomly accessed. Consider the following example.
◦ import java.util.*;
◦ class TestJavaCollection1{
◦ public static void main(String args[]){
◦ ArrayList<String> list=new ArrayList<String>();//Creating arraylist
◦ list.add("Ravi");//Adding object in arraylist
◦ list.add("Vijay");
◦ list.add("Ravi");
8. LinkedList
◦ LinkedList implements the Collection interface.
◦ It uses a doubly linked list internally to store the elements.
◦ It can store the duplicate elements. It maintains the insertion order and is not synchronized.
◦ In LinkedList, the manipulation is fast because no shifting is required.
◦ Consider the following example.
◦ import java.util.*;
◦ public class TestJavaCollection2{
◦ public static void main(String args[]){
◦ LinkedList<String> al=new LinkedList<String>();
◦ al.add("Ravi");
◦ al.add("Vijay");
◦ al.add("Ravi");
10. HashSet
◦ HashSet class implements Set Interface.
◦ It represents the collection that uses a hash table for storage.
◦ Hashing is used to store the elements in the HashSet. It contains unique items.
◦ import java.util.*;
◦ public class TestJavaCollection7{
◦ public static void main(String args[]){
◦ //Creating HashSet and adding elements
◦ HashSet<String> set=new HashSet<String>();
◦ set.add("Ravi");
◦ set.add("Vijay");
◦ set.add("Ravi");
12. Accessing a collection via an iterator, Vector
◦ iterator() method of Vector class that is present inside java.util package is used to return an iterator of the same elements as that of
the Vector.
◦ The elements are returned in random order from what was present in the vector.
◦ Syntax:
◦ Iterator iterate_value = Vector.iterator();
◦ Parameters: The function does not take any parameter.
◦ Return Type: The method iterates over the elements of the vector and returns the values(iterators).
13. // Java code to illustrate iterator() Method
// of Vector class
// Importing required classes
import java.util.*;
import java.util.Vector;
// Main class
public class GFG {
// Main driver method
public static void main(String args[])
{
14. Cont…..
// Creating an empty Vector of string type
Vector<String> vector = new Vector<String>();
// Adding elements into the Vector
// using add() method
vector.add("Welcome");
vector.add("To");
vector.add("Geeks");
vector.add("4");
vector.add("Geeks");
15. Cont….
// Printing and displaying the Vector
System.out.println("Vector: " + vector);
// Now creating an iterator
Iterator value = vector.iterator();
// Display message only
System.out.println("The iterator values are: ");
// Condition holds true till there is single element
// remaining using hasNext() method
while (value.hasNext()) {
16. Cont…..
// Displaying the values
// after iterating through the vector
System.out.println(value.next());
}
}
}
17. more utility class: StringTokenizer
◦ The java.util.StringTokenizer class allows an application to break a string into tokens.
◦ This class is a legacy class that is retained for compatibility reasons although its use is discouraged in new code.
◦ Its methods do not distinguish among identifiers, numbers, and quoted strings.
◦ This class methods do not even recognize and skip comments.
◦ Class declaration
◦ Following is the declaration for java.util.StringTokenizer class −
◦ public class StringTokenizer
◦ extends Object
◦ implements Enumeration<Object>
19. Date
◦ The class Date represents a specific instant in time, with millisecond precision. The Date class of java.util package implements
Serializable, Cloneable and Comparable interface. It provides constructors and methods to deal with date and time with java.
◦ Constructors
◦ Date() : Creates date object representing current date and time.
◦ Date(long milliseconds) : Creates a date object for the given milliseconds since January 1, 1970, 00:00:00 GMT.
◦ Date(int year, int month, int date)
◦ Date(int year, int month, int date, int hrs, int min)
◦ Date(int year, int month, int date, int hrs, int min, int sec)
◦ Date(String s)
◦ Note : The last 4 constructors of the Date class are Deprecated.
20. // Java program to demonstrate
constuctors of Date
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Date d1 = new Date();
System.out.println("Current date is " + d1);
Date d2 = new Date(2323223232L);
System.out.println("Date represented is "+ d2 );
}
}