SlideShare a Scribd company logo
Collections
Why collections?
• Collections are used to hold a collection of
objects.
• List holds objects based on order of insertion
and can hold non unique collection
• Set holds objects without any order and are
unique
• Maps holds objects based on a key and value
pair, unique key and no specific order
Different Collections Interfaces
• List
• Set
• Maps
Hierarchy
Contract
• If two objects are equal according to the
equals(Object) method, then
calling the hashCode() method on each of the
two objects must produce the
same integer result.
• If two objects are unequal according to the
equals(Object) method, there's no requirement
about hashcode().
• If calling hashcode() on two objects produce
different integer result, then both of them
must be unequal according to the equals(Object).
List
• List is an interface with ArrayList, Vector and LinkedList
as concrete implementations.
• List hold objects based on the insertion order and are
non unique.
• Vector is deprecated
• Depending on the requirement ArrayList or LinkedList
is used.
• Arraylist is used when more retrievals and less
removals
• Linkedlist is used when more removals and less
retrievals
List example
class mylist{
public static void main(String args[]){
List l = new ArrayList();
Object o = new Object();
l.add(o);// add o to list
Object x =(Object)l.get(0); // get the object in 0 position
System.out.println(l.size()); // Print the size
}
}
• This example creates an arrayList and adds an object ‘o’ to it.
• We get the object back and print the size of the list.
• Calling the get method does not remove the object from the list.
• You can use a list when the ordering is of prime importance.
Set
• A set interface has concrete implementation of
HashSet, LinkedHashSet and TreeSet.
• The HashSet holds objects in no specific order (random
order) and holds a unique set of objects.
• There is no get method in the Set interface. You will
need to iterate over the set to get your object.
• Hence Set is used only as a collection of objects.
• A TreeSet stores objects in a sorted order. Every object
to be inserted in the TreeSet has to implement the
Comparable or Comparator interface else would result
in a classcastexception.
Set example
class mysetExample{
public static void main(String args[]){
Set myset = new HashSet(); // create a hashset
Object o = new Object(); // create an object
boolean add = myset.add(o); // add object
boolean addagain myset.add(o); // add object again
Iterator iter = myset.iterator(); // extract the iterator
while(iter.hasNext()){
Object o = (Object)iter.next(); // Iterate over the collection
}
}
}
• In this example we are creating a hashset and adding an object to it.
• The value of add will be true and the value of addagain will be false as the same object is already
available in the set
• The iterator iterates over all the available objects in the set
• The equality of the objects is bounded by the equals(Object) and the hashcode() contract.
Map
• The Map Interface has two concrete implementations of
HashMap, LinkedHashMap, TreeMap and Hashtable.
• The Map takes in a key and value pair as input for storage.
• The value can be retrieved if the key is known.
• The Map does not maintain order and should have unique
keys.
• This is an efficient use as the complexity is O(1).
• Hashtable is a synchronized version of HashMap.
ConcurrentHashMap is used as a substitute to Hashtable
• TreeMap stores key, values in the sorted order based on
custom ordering
Map
Class myMap{
public static void main(String args[]){
Map maps = new HashMap();
Object o = new Object();
maps.put(“myobj”,o);
Object o = (Object) maps.get(“myobj”);
}
}
• In this example we are creating a HashMap() and adding an object
with key as myobj and value as the object.
• We can retrieve the object using the key and the get method.
• If you override the hashcode and equals method, then pay special
attention to the contract else the implementations can lead to weird
results
Further Reading
• http://docs.oracle.com/javase/tutorial/collecti
ons/interfaces/index.html
• http://java.sun.com/developer/
onlineTraining/collections/Collection.html

More Related Content

Similar to Java Collection slide ppt presentation.. (20)

PPT
JavaCollections.ppt
boopathirajaraja1
 
PPTX
Java Hands-On Workshop
Arpit Poladia
 
PPTX
collectionsframework210616084411 (1).pptx
ArunPatrick2
 
PPT
java collections
javeed_mhd
 
PPTX
Collections Training
Ramindu Deshapriya
 
PPT
Best core & advanced java classes in mumbai
Vibrant Technologies & Computers
 
PDF
Java Collection framework
ankitgarg_er
 
PPTX
Java collections
anshkhurana7
 
PPTX
Java collections
Amar Kutwal
 
PDF
Collections and generics
Muthukumaran Subramanian
 
ODP
Java Collections
parag
 
PPTX
oop lecture framework,list,maps,collection
ssuseredfbe9
 
PPT
collections
javeed_mhd
 
PPT
sets and maps
Rajkattamuri
 
PPTX
LJ_JAVA_FS_Collection.pptx
Raneez2
 
PPTX
JAVA(UNIT 4)
Dr. SURBHI SAROHA
 
PPTX
Java collections
Sujit Kumar
 
PPTX
Pptchdtdtfygugyxthgihhihigugufydtdfzrzrzrtdyfyfy
dnthulk
 
PPTX
22CS305-UNIT-1.pptx ADVANCE JAVA PROGRAMMING
logesswarisrinivasan
 
JavaCollections.ppt
boopathirajaraja1
 
Java Hands-On Workshop
Arpit Poladia
 
collectionsframework210616084411 (1).pptx
ArunPatrick2
 
java collections
javeed_mhd
 
Collections Training
Ramindu Deshapriya
 
Best core & advanced java classes in mumbai
Vibrant Technologies & Computers
 
Java Collection framework
ankitgarg_er
 
Java collections
anshkhurana7
 
Java collections
Amar Kutwal
 
Collections and generics
Muthukumaran Subramanian
 
Java Collections
parag
 
oop lecture framework,list,maps,collection
ssuseredfbe9
 
collections
javeed_mhd
 
sets and maps
Rajkattamuri
 
LJ_JAVA_FS_Collection.pptx
Raneez2
 
JAVA(UNIT 4)
Dr. SURBHI SAROHA
 
Java collections
Sujit Kumar
 
Pptchdtdtfygugyxthgihhihigugufydtdfzrzrzrtdyfyfy
dnthulk
 
22CS305-UNIT-1.pptx ADVANCE JAVA PROGRAMMING
logesswarisrinivasan
 

Recently uploaded (20)

PPTX
Presentationexpressions You are student leader and have just come from a stud...
BENSTARBEATZ
 
PPTX
Speech Act, types of Speech Act in Pragmatics
gracehananatalias
 
PPTX
AI presentation for everyone in every fields
dodinhkhai1
 
PPTX
Creative perspective presentation copy.pptx
dreamsteel
 
PPTX
BARRIERS TO EFFECTIVE COMMUNICATION.pptx
shraddham25
 
PPTX
Blended Family Future, the Mayflower and You
UCG NWA
 
PPTX
A brief History of counseling in Social Work.pptx
Josaya Injesi
 
PDF
Cloud Computing Service Availability.pdf
chakrirocky1
 
PPTX
2025-07-13 Abraham 07 (shared slides).pptx
Dale Wells
 
PPTX
English_Book_1 part 1 LET Reviewers NEw-
2022mimiacadserver
 
PDF
Leveraging the Power of Jira Dashboard.pdf
siddharthshukla742740
 
PPTX
Sample pitch deck: know what to keep in your pitch deck (for competitions only)
Ujjwaal G
 
PDF
Medical Technology Corporation: Supply Chain Strategy
daretruong
 
PPTX
Pastor Bob Stewart Acts 21 07 09 2025.pptx
FamilyWorshipCenterD
 
PPTX
Bob Stewart Humble Obedience 07-13-2025.pptx
FamilyWorshipCenterD
 
PDF
What should be in a Leadership and Motivation Plan?
Writegenic AI
 
PPTX
Presentation for a short film .pptx.pptx
madisoncosta17
 
PDF
481771880-Fundamentals-of-Public-Speaking.pdf
crisjseit1211
 
PDF
Mining RACE Newsletter 10 - first half of 2025
Mining RACE
 
PDF
Generalization predition MOOCs - Conference presentation - eMOOCs 2025
pmmorenom01
 
Presentationexpressions You are student leader and have just come from a stud...
BENSTARBEATZ
 
Speech Act, types of Speech Act in Pragmatics
gracehananatalias
 
AI presentation for everyone in every fields
dodinhkhai1
 
Creative perspective presentation copy.pptx
dreamsteel
 
BARRIERS TO EFFECTIVE COMMUNICATION.pptx
shraddham25
 
Blended Family Future, the Mayflower and You
UCG NWA
 
A brief History of counseling in Social Work.pptx
Josaya Injesi
 
Cloud Computing Service Availability.pdf
chakrirocky1
 
2025-07-13 Abraham 07 (shared slides).pptx
Dale Wells
 
English_Book_1 part 1 LET Reviewers NEw-
2022mimiacadserver
 
Leveraging the Power of Jira Dashboard.pdf
siddharthshukla742740
 
Sample pitch deck: know what to keep in your pitch deck (for competitions only)
Ujjwaal G
 
Medical Technology Corporation: Supply Chain Strategy
daretruong
 
Pastor Bob Stewart Acts 21 07 09 2025.pptx
FamilyWorshipCenterD
 
Bob Stewart Humble Obedience 07-13-2025.pptx
FamilyWorshipCenterD
 
What should be in a Leadership and Motivation Plan?
Writegenic AI
 
Presentation for a short film .pptx.pptx
madisoncosta17
 
481771880-Fundamentals-of-Public-Speaking.pdf
crisjseit1211
 
Mining RACE Newsletter 10 - first half of 2025
Mining RACE
 
Generalization predition MOOCs - Conference presentation - eMOOCs 2025
pmmorenom01
 
Ad

Java Collection slide ppt presentation..

  • 2. Why collections? • Collections are used to hold a collection of objects. • List holds objects based on order of insertion and can hold non unique collection • Set holds objects without any order and are unique • Maps holds objects based on a key and value pair, unique key and no specific order
  • 3. Different Collections Interfaces • List • Set • Maps
  • 5. Contract • If two objects are equal according to the equals(Object) method, then calling the hashCode() method on each of the two objects must produce the same integer result. • If two objects are unequal according to the equals(Object) method, there's no requirement about hashcode(). • If calling hashcode() on two objects produce different integer result, then both of them must be unequal according to the equals(Object).
  • 6. List • List is an interface with ArrayList, Vector and LinkedList as concrete implementations. • List hold objects based on the insertion order and are non unique. • Vector is deprecated • Depending on the requirement ArrayList or LinkedList is used. • Arraylist is used when more retrievals and less removals • Linkedlist is used when more removals and less retrievals
  • 7. List example class mylist{ public static void main(String args[]){ List l = new ArrayList(); Object o = new Object(); l.add(o);// add o to list Object x =(Object)l.get(0); // get the object in 0 position System.out.println(l.size()); // Print the size } } • This example creates an arrayList and adds an object ‘o’ to it. • We get the object back and print the size of the list. • Calling the get method does not remove the object from the list. • You can use a list when the ordering is of prime importance.
  • 8. Set • A set interface has concrete implementation of HashSet, LinkedHashSet and TreeSet. • The HashSet holds objects in no specific order (random order) and holds a unique set of objects. • There is no get method in the Set interface. You will need to iterate over the set to get your object. • Hence Set is used only as a collection of objects. • A TreeSet stores objects in a sorted order. Every object to be inserted in the TreeSet has to implement the Comparable or Comparator interface else would result in a classcastexception.
  • 9. Set example class mysetExample{ public static void main(String args[]){ Set myset = new HashSet(); // create a hashset Object o = new Object(); // create an object boolean add = myset.add(o); // add object boolean addagain myset.add(o); // add object again Iterator iter = myset.iterator(); // extract the iterator while(iter.hasNext()){ Object o = (Object)iter.next(); // Iterate over the collection } } } • In this example we are creating a hashset and adding an object to it. • The value of add will be true and the value of addagain will be false as the same object is already available in the set • The iterator iterates over all the available objects in the set • The equality of the objects is bounded by the equals(Object) and the hashcode() contract.
  • 10. Map • The Map Interface has two concrete implementations of HashMap, LinkedHashMap, TreeMap and Hashtable. • The Map takes in a key and value pair as input for storage. • The value can be retrieved if the key is known. • The Map does not maintain order and should have unique keys. • This is an efficient use as the complexity is O(1). • Hashtable is a synchronized version of HashMap. ConcurrentHashMap is used as a substitute to Hashtable • TreeMap stores key, values in the sorted order based on custom ordering
  • 11. Map Class myMap{ public static void main(String args[]){ Map maps = new HashMap(); Object o = new Object(); maps.put(“myobj”,o); Object o = (Object) maps.get(“myobj”); } } • In this example we are creating a HashMap() and adding an object with key as myobj and value as the object. • We can retrieve the object using the key and the get method. • If you override the hashcode and equals method, then pay special attention to the contract else the implementations can lead to weird results
  • 12. Further Reading • http://docs.oracle.com/javase/tutorial/collecti ons/interfaces/index.html • http://java.sun.com/developer/ onlineTraining/collections/Collection.html