SlideShare a Scribd company logo
4
Most read
15
Most read
21
Most read
JAVA(UNIT 4)
BY:SURBHI SAROHA
SYLLABUS
◦ The Collection Framework
◦ Collection interfaces
◦ Collection classes(ArrayList, LinkedList, Hash set)
◦ Accessing a collection via an iterator, Vector,
◦ more utility class: StringTokenizer
◦ Date
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.
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.
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
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");
Cont…..
◦ list.add("Ajay");
◦ //Traversing list through Iterator
◦ Iterator itr=list.iterator();
◦ while(itr.hasNext()){
◦ System.out.println(itr.next());
◦ }
◦ }
◦ }
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");
Cont…..
◦ al.add("Ajay");
◦ Iterator<String> itr=al.iterator();
◦ while(itr.hasNext()){
◦ System.out.println(itr.next());
◦ }
◦ }
◦ }
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");
Cont…..
◦ set.add("Ajay");
◦ //Traversing elements
◦ Iterator<String> itr=set.iterator();
◦ while(itr.hasNext()){
◦ System.out.println(itr.next());
◦ }
◦ }
◦ }
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).
// 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[])
{
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");
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()) {
Cont…..
// Displaying the values
// after iterating through the vector
System.out.println(value.next());
}
}
}
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>
Class constructors
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.
// 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 );
}
}
Thank you

More Related Content

PPTX
collectionsframework210616084411 (1).pptx
PDF
java unit 4 pdf - about java collections
PPTX
LJ_JAVA_FS_Collection.pptx
PPT
12_-_Collections_Framework
PPTX
Java Unit 2 (Part 2)
PPTX
oop lecture framework,list,maps,collection
PPTX
List interface in collections framework
collectionsframework210616084411 (1).pptx
java unit 4 pdf - about java collections
LJ_JAVA_FS_Collection.pptx
12_-_Collections_Framework
Java Unit 2 (Part 2)
oop lecture framework,list,maps,collection
List interface in collections framework

Similar to JAVA(UNIT 4) (20)

PPT
Java collection
PPT
11000121065_NAITIK CHATTERJEE.ppt
PDF
Java Collection Framework for BCA Students
PDF
Collection framework (completenotes) zeeshan
PPTX
Pptchdtdtfygugyxthgihhihigugufydtdfzrzrzrtdyfyfy
PPTX
22CS305-UNIT-1.pptx ADVANCE JAVA PROGRAMMING
PPTX
Advanced Java - UNIT 3.pptx
PPTX
Nature Activities Binder _ by Slidesgo.pptx
PPTX
Collections in java in detail for easy understanding
DOC
Advanced core java
PPT
Collections in Java
PDF
JAVA PROGRAMMING - The Collections Framework
PDF
Collections in Java Notes
PPTX
Collections lecture 35 40
PPT
Collections
PPT
JavaCollections.ppt
PPT
JavaCollections.ppt
PPT
Collection v3
ODP
Java Collections
PPT
Java Collection fundamentals and Uses Unit
Java collection
11000121065_NAITIK CHATTERJEE.ppt
Java Collection Framework for BCA Students
Collection framework (completenotes) zeeshan
Pptchdtdtfygugyxthgihhihigugufydtdfzrzrzrtdyfyfy
22CS305-UNIT-1.pptx ADVANCE JAVA PROGRAMMING
Advanced Java - UNIT 3.pptx
Nature Activities Binder _ by Slidesgo.pptx
Collections in java in detail for easy understanding
Advanced core java
Collections in Java
JAVA PROGRAMMING - The Collections Framework
Collections in Java Notes
Collections lecture 35 40
Collections
JavaCollections.ppt
JavaCollections.ppt
Collection v3
Java Collections
Java Collection fundamentals and Uses Unit
Ad

More from Dr. SURBHI SAROHA (20)

PPTX
Deep learning(UNIT 3) BY Ms SURBHI SAROHA
PPTX
MOBILE COMPUTING UNIT 2 by surbhi saroha
PPTX
Mobile Computing UNIT 1 by surbhi saroha
PPTX
DEEP LEARNING (UNIT 2 ) by surbhi saroha
PPTX
Introduction to Deep Leaning(UNIT 1).pptx
PPTX
Cloud Computing (Infrastructure as a Service)UNIT 2
PPTX
Management Information System(Unit 2).pptx
PPTX
Searching in Data Structure(Linear search and Binary search)
PPTX
Management Information System(UNIT 1).pptx
PPTX
Introduction to Cloud Computing(UNIT 1).pptx
PPTX
JAVA (UNIT 5)
PPTX
DBMS (UNIT 5)
PPTX
DBMS UNIT 4
PPTX
OOPs & C++(UNIT 5)
PPTX
OOPS & C++(UNIT 4)
PPTX
DBMS UNIT 3
PPTX
JAVA (UNIT 3)
PPTX
Keys in dbms(UNIT 2)
PPTX
DBMS (UNIT 2)
PPTX
JAVA UNIT 2
Deep learning(UNIT 3) BY Ms SURBHI SAROHA
MOBILE COMPUTING UNIT 2 by surbhi saroha
Mobile Computing UNIT 1 by surbhi saroha
DEEP LEARNING (UNIT 2 ) by surbhi saroha
Introduction to Deep Leaning(UNIT 1).pptx
Cloud Computing (Infrastructure as a Service)UNIT 2
Management Information System(Unit 2).pptx
Searching in Data Structure(Linear search and Binary search)
Management Information System(UNIT 1).pptx
Introduction to Cloud Computing(UNIT 1).pptx
JAVA (UNIT 5)
DBMS (UNIT 5)
DBMS UNIT 4
OOPs & C++(UNIT 5)
OOPS & C++(UNIT 4)
DBMS UNIT 3
JAVA (UNIT 3)
Keys in dbms(UNIT 2)
DBMS (UNIT 2)
JAVA UNIT 2
Ad

Recently uploaded (20)

PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
PDF
LDMMIA Reiki Yoga Finals Review Spring Summer
PPTX
UNIT III MENTAL HEALTH NURSING ASSESSMENT
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
History, Philosophy and sociology of education (1).pptx
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
01-Introduction-to-Information-Management.pdf
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
Classroom Observation Tools for Teachers
PPTX
Radiologic_Anatomy_of_the_Brachial_plexus [final].pptx
PDF
Updated Idioms and Phrasal Verbs in English subject
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PDF
Yogi Goddess Pres Conference Studio Updates
PDF
Practical Manual AGRO-233 Principles and Practices of Natural Farming
Chinmaya Tiranga quiz Grand Finale.pdf
Microbial disease of the cardiovascular and lymphatic systems
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
LDMMIA Reiki Yoga Finals Review Spring Summer
UNIT III MENTAL HEALTH NURSING ASSESSMENT
Final Presentation General Medicine 03-08-2024.pptx
History, Philosophy and sociology of education (1).pptx
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
01-Introduction-to-Information-Management.pdf
Microbial diseases, their pathogenesis and prophylaxis
Classroom Observation Tools for Teachers
Radiologic_Anatomy_of_the_Brachial_plexus [final].pptx
Updated Idioms and Phrasal Verbs in English subject
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Module 4: Burden of Disease Tutorial Slides S2 2025
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
Yogi Goddess Pres Conference Studio Updates
Practical Manual AGRO-233 Principles and Practices of Natural Farming

JAVA(UNIT 4)

  • 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");
  • 7. Cont….. ◦ list.add("Ajay"); ◦ //Traversing list through Iterator ◦ Iterator itr=list.iterator(); ◦ while(itr.hasNext()){ ◦ System.out.println(itr.next()); ◦ } ◦ } ◦ }
  • 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");
  • 9. Cont….. ◦ al.add("Ajay"); ◦ Iterator<String> itr=al.iterator(); ◦ while(itr.hasNext()){ ◦ System.out.println(itr.next()); ◦ } ◦ } ◦ }
  • 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");
  • 11. Cont….. ◦ set.add("Ajay"); ◦ //Traversing elements ◦ Iterator<String> itr=set.iterator(); ◦ while(itr.hasNext()){ ◦ System.out.println(itr.next()); ◦ } ◦ } ◦ }
  • 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 ); } }