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

PPTX
Javasession7
PPT
sets and maps
PPTX
Collections Training
PPTX
Collections framework in java
PPTX
Java collections
PPTX
Java Collections Framework - Interfaces, Classes and Algorithms
PPT
Java Presentation
Javasession7
sets and maps
Collections Training
Collections framework in java
Java collections
Java Collections Framework - Interfaces, Classes and Algorithms
Java Presentation

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

PDF
Collections and generics
PPTX
Java 103 intro to java data structures
PPT
description of Collections, seaching & Sorting
PPTX
C# Non generics collection
PDF
Collections Api - Java
PPT
Set_TreeSet_etc comparison in java collection.ppt
PPTX
Java Collections.pptx
PPTX
Java util
PPTX
hashing in data strutures advanced in languae java
PDF
Lecture notesmap
PPTX
22.collections(1)
PPTX
Collection Framework-1.pptx
PPT
PDF
Java collections-interview-questions
PDF
Unit-9 Searching .pdf
PPTX
LJ_JAVA_FS_Collection.pptx
PPTX
GRAPHS, BREADTH FIRST TRAVERSAL AND DEPTH FIRST TRAVERSAL
PPTX
hashing in data structure for Btech .pptx
PPTX
hashing in data structure for engineering.pptx
PPTX
collectionsframework210616084411 (1).pptx
Collections and generics
Java 103 intro to java data structures
description of Collections, seaching & Sorting
C# Non generics collection
Collections Api - Java
Set_TreeSet_etc comparison in java collection.ppt
Java Collections.pptx
Java util
hashing in data strutures advanced in languae java
Lecture notesmap
22.collections(1)
Collection Framework-1.pptx
Java collections-interview-questions
Unit-9 Searching .pdf
LJ_JAVA_FS_Collection.pptx
GRAPHS, BREADTH FIRST TRAVERSAL AND DEPTH FIRST TRAVERSAL
hashing in data structure for Btech .pptx
hashing in data structure for engineering.pptx
collectionsframework210616084411 (1).pptx
Ad

Recently uploaded (20)

PPTX
fundraisepro pitch deck elegant and modern
PPT
First Aid Training Presentation Slides.ppt
PPTX
nose tajweed for the arabic alphabets for the responsive
PPTX
Introduction-to-Food-Packaging-and-packaging -materials.pptx
PPTX
Presentation for DGJV QMS (PQP)_12.03.2025.pptx
DOCX
"Project Management: Ultimate Guide to Tools, Techniques, and Strategies (2025)"
PPTX
Emphasizing It's Not The End 08 06 2025.pptx
PPTX
Introduction to Effective Communication.pptx
PPTX
Tour Presentation Educational Activity.pptx
PDF
Nykaa-Strategy-Case-Fixing-Retention-UX-and-D2C-Engagement (1).pdf
PPT
The Effect of Human Resource Management Practice on Organizational Performanc...
PPTX
worship songs, in any order, compilation
PPTX
chapter8-180915055454bycuufucdghrwtrt.pptx
PPTX
The Effect of Human Resource Management Practice on Organizational Performanc...
PPTX
The spiral of silence is a theory in communication and political science that...
PPTX
Tablets And Capsule Preformulation Of Paracetamol
PPTX
English-9-Q1-3-.pptxjkshbxnnxgchchxgxhxhx
PPTX
Non-Verbal-Communication .mh.pdf_110245_compressed.pptx
PPTX
Primary and secondary sources, and history
PPTX
Effective_Handling_Information_Presentation.pptx
fundraisepro pitch deck elegant and modern
First Aid Training Presentation Slides.ppt
nose tajweed for the arabic alphabets for the responsive
Introduction-to-Food-Packaging-and-packaging -materials.pptx
Presentation for DGJV QMS (PQP)_12.03.2025.pptx
"Project Management: Ultimate Guide to Tools, Techniques, and Strategies (2025)"
Emphasizing It's Not The End 08 06 2025.pptx
Introduction to Effective Communication.pptx
Tour Presentation Educational Activity.pptx
Nykaa-Strategy-Case-Fixing-Retention-UX-and-D2C-Engagement (1).pdf
The Effect of Human Resource Management Practice on Organizational Performanc...
worship songs, in any order, compilation
chapter8-180915055454bycuufucdghrwtrt.pptx
The Effect of Human Resource Management Practice on Organizational Performanc...
The spiral of silence is a theory in communication and political science that...
Tablets And Capsule Preformulation Of Paracetamol
English-9-Q1-3-.pptxjkshbxnnxgchchxgxhxhx
Non-Verbal-Communication .mh.pdf_110245_compressed.pptx
Primary and secondary sources, and history
Effective_Handling_Information_Presentation.pptx
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