SlideShare a Scribd company logo
1 of 21
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

Similar to JAVA(UNIT 4)

Development of Java tools using SWT and WALA af Hans Søndergaard, ViaUC
Development of Java tools using SWT and WALA af Hans Søndergaard, ViaUCDevelopment of Java tools using SWT and WALA af Hans Søndergaard, ViaUC
Development of Java tools using SWT and WALA af Hans Søndergaard, ViaUCInfinIT - Innovationsnetværket for it
 
Unit3 packages &amp; interfaces
Unit3 packages &amp; interfacesUnit3 packages &amp; interfaces
Unit3 packages &amp; interfacesKalai Selvi
 
oops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaoops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaCPD INDIA
 
11000121065_NAITIK CHATTERJEE.ppt
11000121065_NAITIK CHATTERJEE.ppt11000121065_NAITIK CHATTERJEE.ppt
11000121065_NAITIK CHATTERJEE.pptNaitikChatterjee
 
adjava_23_bcs_vision_academy_sachinsir.pdf
adjava_23_bcs_vision_academy_sachinsir.pdfadjava_23_bcs_vision_academy_sachinsir.pdf
adjava_23_bcs_vision_academy_sachinsir.pdfakankshasorate1
 
Generics and collections in Java
Generics and collections in JavaGenerics and collections in Java
Generics and collections in JavaGurpreet singh
 
Statics in java | Constructors | Exceptions in Java | String in java| class 3
Statics in java | Constructors | Exceptions in Java | String in java| class 3Statics in java | Constructors | Exceptions in Java | String in java| class 3
Statics in java | Constructors | Exceptions in Java | String in java| class 3Sagar Verma
 
Java collections
Java collectionsJava collections
Java collectionsAmar Kutwal
 
More topics on Java
More topics on JavaMore topics on Java
More topics on JavaAhmed Misbah
 
Unit 1 of java part 2 basic introduction
Unit 1 of java part 2 basic introduction Unit 1 of java part 2 basic introduction
Unit 1 of java part 2 basic introduction AKR Education
 
Java collections
Java collectionsJava collections
Java collectionspadmad2291
 
Java Course 4: Exceptions & Collections
Java Course 4: Exceptions & CollectionsJava Course 4: Exceptions & Collections
Java Course 4: Exceptions & CollectionsAnton Keks
 

Similar to JAVA(UNIT 4) (20)

Development of Java tools using SWT and WALA af Hans Søndergaard, ViaUC
Development of Java tools using SWT and WALA af Hans Søndergaard, ViaUCDevelopment of Java tools using SWT and WALA af Hans Søndergaard, ViaUC
Development of Java tools using SWT and WALA af Hans Søndergaard, ViaUC
 
Java util
Java utilJava util
Java util
 
Unit3 packages &amp; interfaces
Unit3 packages &amp; interfacesUnit3 packages &amp; interfaces
Unit3 packages &amp; interfaces
 
Lecture 9
Lecture 9Lecture 9
Lecture 9
 
oops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaoops concept in java | object oriented programming in java
oops concept in java | object oriented programming in java
 
11000121065_NAITIK CHATTERJEE.ppt
11000121065_NAITIK CHATTERJEE.ppt11000121065_NAITIK CHATTERJEE.ppt
11000121065_NAITIK CHATTERJEE.ppt
 
adjava_23_bcs_vision_academy_sachinsir.pdf
adjava_23_bcs_vision_academy_sachinsir.pdfadjava_23_bcs_vision_academy_sachinsir.pdf
adjava_23_bcs_vision_academy_sachinsir.pdf
 
Generics and collections in Java
Generics and collections in JavaGenerics and collections in Java
Generics and collections in Java
 
Statics in java | Constructors | Exceptions in Java | String in java| class 3
Statics in java | Constructors | Exceptions in Java | String in java| class 3Statics in java | Constructors | Exceptions in Java | String in java| class 3
Statics in java | Constructors | Exceptions in Java | String in java| class 3
 
Collections in Java Notes
Collections in Java NotesCollections in Java Notes
Collections in Java Notes
 
Java interview questions
Java interview questionsJava interview questions
Java interview questions
 
Java collections
Java collectionsJava collections
Java collections
 
Java collections
Java collectionsJava collections
Java collections
 
More topics on Java
More topics on JavaMore topics on Java
More topics on Java
 
Unit 1 of java part 2 basic introduction
Unit 1 of java part 2 basic introduction Unit 1 of java part 2 basic introduction
Unit 1 of java part 2 basic introduction
 
Java mcq
Java mcqJava mcq
Java mcq
 
Java.util
Java.utilJava.util
Java.util
 
Collections Training
Collections TrainingCollections Training
Collections Training
 
Java collections
Java collectionsJava collections
Java collections
 
Java Course 4: Exceptions & Collections
Java Course 4: Exceptions & CollectionsJava Course 4: Exceptions & Collections
Java Course 4: Exceptions & Collections
 

More from SURBHI SAROHA

Cloud Computing (Infrastructure as a Service)UNIT 2
Cloud Computing (Infrastructure as a Service)UNIT 2Cloud Computing (Infrastructure as a Service)UNIT 2
Cloud Computing (Infrastructure as a Service)UNIT 2SURBHI SAROHA
 
Management Information System(Unit 2).pptx
Management Information System(Unit 2).pptxManagement Information System(Unit 2).pptx
Management Information System(Unit 2).pptxSURBHI SAROHA
 
Searching in Data Structure(Linear search and Binary search)
Searching in Data Structure(Linear search and Binary search)Searching in Data Structure(Linear search and Binary search)
Searching in Data Structure(Linear search and Binary search)SURBHI SAROHA
 
Management Information System(UNIT 1).pptx
Management Information System(UNIT 1).pptxManagement Information System(UNIT 1).pptx
Management Information System(UNIT 1).pptxSURBHI SAROHA
 
Introduction to Cloud Computing(UNIT 1).pptx
Introduction to Cloud Computing(UNIT 1).pptxIntroduction to Cloud Computing(UNIT 1).pptx
Introduction to Cloud Computing(UNIT 1).pptxSURBHI SAROHA
 
Keys in dbms(UNIT 2)
Keys in dbms(UNIT 2)Keys in dbms(UNIT 2)
Keys in dbms(UNIT 2)SURBHI SAROHA
 
Database Management System(UNIT 1)
Database Management System(UNIT 1)Database Management System(UNIT 1)
Database Management System(UNIT 1)SURBHI SAROHA
 
Object-Oriented Programming with Java UNIT 1
Object-Oriented Programming with Java UNIT 1Object-Oriented Programming with Java UNIT 1
Object-Oriented Programming with Java UNIT 1SURBHI SAROHA
 
Database Management System(UNIT 1)
Database Management System(UNIT 1)Database Management System(UNIT 1)
Database Management System(UNIT 1)SURBHI SAROHA
 
OOPS USING C++(UNIT 2)
OOPS USING C++(UNIT 2)OOPS USING C++(UNIT 2)
OOPS USING C++(UNIT 2)SURBHI SAROHA
 

More from SURBHI SAROHA (20)

Cloud Computing (Infrastructure as a Service)UNIT 2
Cloud Computing (Infrastructure as a Service)UNIT 2Cloud Computing (Infrastructure as a Service)UNIT 2
Cloud Computing (Infrastructure as a Service)UNIT 2
 
Management Information System(Unit 2).pptx
Management Information System(Unit 2).pptxManagement Information System(Unit 2).pptx
Management Information System(Unit 2).pptx
 
Searching in Data Structure(Linear search and Binary search)
Searching in Data Structure(Linear search and Binary search)Searching in Data Structure(Linear search and Binary search)
Searching in Data Structure(Linear search and Binary search)
 
Management Information System(UNIT 1).pptx
Management Information System(UNIT 1).pptxManagement Information System(UNIT 1).pptx
Management Information System(UNIT 1).pptx
 
Introduction to Cloud Computing(UNIT 1).pptx
Introduction to Cloud Computing(UNIT 1).pptxIntroduction to Cloud Computing(UNIT 1).pptx
Introduction to Cloud Computing(UNIT 1).pptx
 
JAVA (UNIT 5)
JAVA (UNIT 5)JAVA (UNIT 5)
JAVA (UNIT 5)
 
DBMS (UNIT 5)
DBMS (UNIT 5)DBMS (UNIT 5)
DBMS (UNIT 5)
 
DBMS UNIT 4
DBMS UNIT 4DBMS UNIT 4
DBMS UNIT 4
 
OOPs & C++(UNIT 5)
OOPs & C++(UNIT 5)OOPs & C++(UNIT 5)
OOPs & C++(UNIT 5)
 
OOPS & C++(UNIT 4)
OOPS & C++(UNIT 4)OOPS & C++(UNIT 4)
OOPS & C++(UNIT 4)
 
DBMS UNIT 3
DBMS UNIT 3DBMS UNIT 3
DBMS UNIT 3
 
JAVA (UNIT 3)
JAVA (UNIT 3)JAVA (UNIT 3)
JAVA (UNIT 3)
 
Keys in dbms(UNIT 2)
Keys in dbms(UNIT 2)Keys in dbms(UNIT 2)
Keys in dbms(UNIT 2)
 
DBMS (UNIT 2)
DBMS (UNIT 2)DBMS (UNIT 2)
DBMS (UNIT 2)
 
JAVA UNIT 2
JAVA UNIT 2JAVA UNIT 2
JAVA UNIT 2
 
Database Management System(UNIT 1)
Database Management System(UNIT 1)Database Management System(UNIT 1)
Database Management System(UNIT 1)
 
Object-Oriented Programming with Java UNIT 1
Object-Oriented Programming with Java UNIT 1Object-Oriented Programming with Java UNIT 1
Object-Oriented Programming with Java UNIT 1
 
Database Management System(UNIT 1)
Database Management System(UNIT 1)Database Management System(UNIT 1)
Database Management System(UNIT 1)
 
OOPs & C++ UNIT 3
OOPs & C++ UNIT 3OOPs & C++ UNIT 3
OOPs & C++ UNIT 3
 
OOPS USING C++(UNIT 2)
OOPS USING C++(UNIT 2)OOPS USING C++(UNIT 2)
OOPS USING C++(UNIT 2)
 

Recently uploaded

Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxsocialsciencegdgrohi
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,Virag Sontakke
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitolTechU
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerunnathinaik
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupJonathanParaisoCruz
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentInMediaRes1
 

Recently uploaded (20)

Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptx
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developer
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized Group
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media Component
 

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 ); } }