Collections

M
Manav PrasadSolutions Architect
Collections
Overview
• A Collection is a container that groups similar
elements into an entity.
• Examples would include a list of bank accounts,
set of students, group of telephone numbers.
• The Collections framework in Java offers a
unified approach to store, retrieve and
manipulate a group of data
• Java’s Collection framework has an intuitive
approach when compared to other complex
frameworks such as C++
Benefits
• Helpful to developers by providing all standard data
structures algorithms to let the developer concentrate
more on functionality rather than the lower level
details
• Faster execution: with a range of options available for
choice of implementation. The framework aids in
improving the quality and performance of Java
applications.
• Code to interface: Since the collections framework is
coded on the principles of “Code to Interface”, it aids
in inter-operability between APIs that are not directly
related.
• Learning Curve: Easy to learn and start using
Collections due to the “Code to Interface” principles.
Core Collections Framework
• The Collection framework forms a suitable
hierarchy:
Collection
Set List Queue
SortedSet
Map
SortedMap
Core Collections Framework
(Cont’d)
• The core Collections framework is very generic in nature and
provides a standard interface to all operations that can be
performed.
• For example the root interface Collection is declared as public
interface Collection <E>
– E represents a generic object.
• Collection: This interface is pretty much used in declarations and
method signatures so any type of Collections can be passed
around.
• Set: A Collection interface that cannot take any duplicate
elements.
• List: An ordered Collection interface that allows duplicate
elements and provides object retrieval based on an integer
index.
• Queue: Apart from following the FIFO (First In First Out)
principles this Collection offers variety of implementation.
• Map: Supports Collection of key and value pairs. Design does not
allow duplicate keys.
• Sorted Set: Ordered version of the set interface.
• Sorted Map: Maintains ascending order of keys.
Operations Supported by
Collection <E> interface
• boolean isEmpty();
• boolean contains (Object element);
• boolean remove (Object element);
• Interface <E> iterator();
• boolean containsAll (Collection <> C);
• boolean addAll (Collection <? extends E> C);
• boolean removeAll (Collection() > C);
c.removeAll (Collections.Singleton(obj);
• boolean retainAll (Collection <?> C);
• void clear();
• Object[ ] toArray();
• <T> T[ ] toArray(T[ ]a);
Iteration
• Two ways to iterate over Collections:
– Iterator
static void loopThrough(Collection col){
for (Iterator <E> iter = col.iterator();iter.hasnext()) {
Object obj=iter.next();
}
}
– For-each
static void loopThrough(Collection col){
for (Object obj: col) {
//access object
}
}
Set
• Set Interface: It offers inherited services from
the root Collection interface with the added
functionality of restricting duplicate
elements.
• Implementations:
Set
HashSet TreeSet LinkedHashSet
Example Code
• Adding unique objects to a collection
Collection <String> uniqueString (Collection<String> c)
{
Set<String> uniqueSetStr = new HashSet<String>();
for (String str: c)
{
if(!uniqueStrSet.add(str)){
System.out.println(“Duplicate deleted:”+ str);
}
}
return uniqueStrSet;
Set Implementation
Comparisons
• Idioms for using bulk operations on sets:
• Copy a set when doing bulk operations to retain the
original set.
• Example:
Set <String> unionSet = new TreeSet<String>(set1);
unionSet.addAll(set2);
HashSet TreeSet Linked HashSet
Storage Type Hash Table Red-Black Tree Hash Table with
a Linked List
Performance Best
performance
Slower than
HashSet
Little costly than
HashSet
Order of
Iteration
No guarantee of
order of
iteration
Order based Orders elements
based on
insertion
List
• In addition to the core services inherited from the root
collection interface, the list interface offers
– Positional access
– Search
– Customized Iteration
– Range-view
• List Implementation
– ArrayList offers better performance compared to Linkedlist.
List
Arraylist Vector Linkedlist
Typical usage of List
• Bulk Operations
– listA.addAll(listB); // append elements.
• Positional access and search
– Eg: To swap elements in a list.
public static<String> void swap(List<String>strList, int k, int l)
{
String strTmp = strList.get(l);
strList.set(k,strList.get(l));
strList.set(l,strTmp);
}
List Iterator
• Customized Iteration: ListIterator
– Offers iteration in both directions, forward and
backward
– Example:
for(ListIterator<String>
lIter=strList.listIterator(strList.size());lIter.hasPrevious())
{
String str = lIter.previous();
}
List Operations
• Range-View:
– subList(int from, int to) returns a view portion
of the sublist starting from <from> to <to>
exclusive.
Algorithms available for List
• sort: Uses mergesort
• shuffle: Randomly shuffles elements
• reverse: Reverses order of elements
• rotate: Rotates list by specified number
• fill: Overwrites every element with specified element.
• copy: Copies source list into destination.
• binarySearch: Performs search usng binary search.
• indexOfSubList: Returns index of first subList found.
• lastIndexOfSubList: Returns index of the last subList
found.
Queue
• In addition to the inherited core services offered
by Collection, queue offers following methods in
two flavors:
Purpose Throw
Exception
Return Special
Value
Insert Inserts an elements
to the queue
add(obj) offer(obj)
Remove Remove head of the
queue and return it.
remove() poll()
Examine Return the head of
the queue
element() peek()
Map
• Basic operations:
– Val put (Object key);
– Val get (Object key);
– Val remove (Object key);
– boolean containsKey (Object key);
– boolean containsValue (Object value);
– int size();
– boolean isEmpty();
• Bulk services
– Void putAll(Map<? extends Key, ? extends Val> map);
– void clear();
Map (Cont’d)
• Views
public set<Key> keySet();
public collection<Val> values();
public set<Maps.Entry<Key,Val>> entrySet();
• Examples:
Iteration over a map
for(Map.Entry<?,vtype> elem: map.entrySet())
System.out.print(element.getKey()+”:”+element.getValue());
To find If a map contains another map
If(map1.entrySet().containsAll(Map2.entrySet()))
If two map objects contain same keys
If(map1.keyset().equals(map2.keyset()))
To find out common keys behave two map objects
Set<Ktype> common keys = new Hashset<Ktype>(map1.commonkeys.retainAll(map2.keyset());
Map Implementations
Performance Increases
Map
HashMap TreeMap Linkedlist HashTable
SortedSet
• SortedSet stores elements in ascending order as per the
natural ordering or as defined by a comparator provided at
instantiation time.
• Also the iterator and toArray methods iterate and return
array in sorted order.
• Additional Services provided:
– SortedSet <Element> subSet (Element fromElement, Element
toElement);
– SortedSet <Element> headSet (Element toElement);
– SortedSet <Element> tailSet (Element fromElement);
– element first();
– element last();
– Comparator <? Super Element> comparator();
SortedMap
• Organizes data in ascending order based on
natural ordering of the keys or a comparator
supplied at instantiation time.
• Services provided in addition to Map interface:
– Comparator <? super Key> comparator();
– SortedMap<Key, Value> SubMap(Key from, key to);
– SortedMap<Key, Value> headMap (key to);
– SortedMap<Key, Value> tailMap(key from);
– Key firstKey();
– Key lastKey();
Map: Pitfalls
• ThreadSafety
– The bulk operations like keySet(), values() and
entrySet() return collections that can be
modified automatically when the underlying
collection changes.
ThreadSafety
• Collections by default are not threadsafe
• In order to achieve thread-safety the following utility methods
from <collection> can be employed.
• Encapsulation
– Implementation style, where the access to the collection is only from a
thread-safe client
• Synchronization
– Collections.synchronizedList (new LinkedList(…));
• Unmodifiable
– Collections.unmodifiableList(new LinkedList(…));
• Fail-safe iterator
– Using fail-safe iterators makes sure the collection is not modified as it is
iterated.
Bibliography
• http://java.sun.com/docs/books/tutorial/
• http://www.purpletech.com/talks/Collect
1 von 24

Recomendados

CollectionsCollections
CollectionsRajkattamuri
231 views23 Folien
collectionscollections
collectionsjaveed_mhd
365 views23 Folien
Collections in JavaCollections in Java
Collections in JavaKhasim Cise
814 views23 Folien
 sets and maps sets and maps
sets and mapsRajkattamuri
871 views23 Folien
java collectionsjava collections
java collectionsjaveed_mhd
904 views30 Folien

Más contenido relacionado

Was ist angesagt?(20)

Java - Collections frameworkJava - Collections framework
Java - Collections framework
Riccardo Cardin5.3K views
Collections and genericsCollections and generics
Collections and generics
Muthukumaran Subramanian688 views
07 java collection07 java collection
07 java collection
Abhishek Khune4.8K views
Collections - Maps Collections - Maps
Collections - Maps
Hitesh-Java1.4K views
Collections Java e Google CollectionsCollections Java e Google Collections
Collections Java e Google Collections
André Faria Gomes1.2K views
Java collectionJava collection
Java collection
Arati Gadgil2.5K views
Java Collections FrameworkJava Collections Framework
Java Collections Framework
Sony India Software Center4.5K views
Collections Api - JavaCollections Api - Java
Collections Api - Java
Drishti Bhalla1.2K views
Collections - Lists, Sets Collections - Lists, Sets
Collections - Lists, Sets
Hitesh-Java606 views
Java Collections TutorialsJava Collections Tutorials
Java Collections Tutorials
Prof. Erwin Globio2.8K views
Ist year Msc,2nd sem module1Ist year Msc,2nd sem module1
Ist year Msc,2nd sem module1
blessyboban9285 views
Java Collection frameworkJava Collection framework
Java Collection framework
ankitgarg_er14.3K views
Java CollectionsJava Collections
Java Collections
parag8.6K views
stacks and queues for publicstacks and queues for public
stacks and queues for public
iqbalphy1225 views
Java collections conceptJava collections concept
Java collections concept
kumar gaurav7.7K views
Priority QueuePriority Queue
Priority Queue
Joyjit Choudhury776 views
5 collection framework5 collection framework
5 collection framework
Minal Maniar2.6K views
Collection advanceCollection advance
Collection advance
Aakash Jain54 views
Collections in Java NotesCollections in Java Notes
Collections in Java Notes
Shalabh Chaudhary2.1K views

Similar a Collections(20)

Best core & advanced java classes in mumbaiBest core & advanced java classes in mumbai
Best core & advanced java classes in mumbai
Vibrant Technologies & Computers410 views
CollectionsCollections
Collections
Gujarat Technological University965 views
standard template library(STL) in C++standard template library(STL) in C++
standard template library(STL) in C++
•sreejith •sree15.3K views
JavaCollections.pptJavaCollections.ppt
JavaCollections.ppt
boopathirajaraja12 views
JavaCollections.pptJavaCollections.ppt
JavaCollections.ppt
Irfanhabeeb183 views
ListsLists
Lists
Sumit Tambe397 views
Collection frameworkCollection framework
Collection framework
Jainul Musani953 views
Java collectionsJava collections
Java collections
anshkhurana7303 views
Java  Collections Java  Collections
Java Collections
Kongu Engineering College, Perundurai, Erode848 views
JAVA PROGRAMMING - The Collections Framework JAVA PROGRAMMING - The Collections Framework
JAVA PROGRAMMING - The Collections Framework
Jyothishmathi Institute of Technology and Science Karimnagar79 views
Collections TrainingCollections Training
Collections Training
Ramindu Deshapriya582 views
Collections lecture 35 40Collections lecture 35 40
Collections lecture 35 40
bhawna sharma139 views
Collections - Array List Collections - Array List
Collections - Array List
Hitesh-Java943 views
2 b queues2 b queues
2 b queues
Nguync9136861 views
Data structuresData structures
Data structures
Manaswi Sharma5.5K views
Standard Template LibraryStandard Template Library
Standard Template Library
GauravPatil318251 views
collection framework in javacollection framework in java
collection framework in java
MANOJ KUMAR422 views

Más de Manav Prasad

MulesoftconnectorsMulesoftconnectors
MulesoftconnectorsManav Prasad
117 views17 Folien
Mulesoft cloudhubMulesoft cloudhub
Mulesoft cloudhubManav Prasad
383 views9 Folien
Perl tutorialPerl tutorial
Perl tutorialManav Prasad
1.1K views42 Folien

Más de Manav Prasad(20)

Experience with mulesoftExperience with mulesoft
Experience with mulesoft
Manav Prasad257 views
MulesoftconnectorsMulesoftconnectors
Mulesoftconnectors
Manav Prasad117 views
Mule and web servicesMule and web services
Mule and web services
Manav Prasad450 views
Mulesoft cloudhubMulesoft cloudhub
Mulesoft cloudhub
Manav Prasad383 views
Perl tutorialPerl tutorial
Perl tutorial
Manav Prasad1.1K views
Hibernate presentationHibernate presentation
Hibernate presentation
Manav Prasad1.4K views
JpaJpa
Jpa
Manav Prasad481 views
Spring introductionSpring introduction
Spring introduction
Manav Prasad426 views
Json Json
Json
Manav Prasad463 views
The spring frameworkThe spring framework
The spring framework
Manav Prasad274 views
Rest introductionRest introduction
Rest introduction
Manav Prasad235 views
Exceptions in javaExceptions in java
Exceptions in java
Manav Prasad311 views
JunitJunit
Junit
Manav Prasad2.4K views
Xml parsersXml parsers
Xml parsers
Manav Prasad3.9K views
XpathXpath
Xpath
Manav Prasad2.1K views
XsltXslt
Xslt
Manav Prasad1.3K views
XhtmlXhtml
Xhtml
Manav Prasad2.5K views
CssCss
Css
Manav Prasad1.2K views
Introduction to html5Introduction to html5
Introduction to html5
Manav Prasad518 views
AjaxAjax
Ajax
Manav Prasad414 views

Collections

  • 2. Overview • A Collection is a container that groups similar elements into an entity. • Examples would include a list of bank accounts, set of students, group of telephone numbers. • The Collections framework in Java offers a unified approach to store, retrieve and manipulate a group of data • Java’s Collection framework has an intuitive approach when compared to other complex frameworks such as C++
  • 3. Benefits • Helpful to developers by providing all standard data structures algorithms to let the developer concentrate more on functionality rather than the lower level details • Faster execution: with a range of options available for choice of implementation. The framework aids in improving the quality and performance of Java applications. • Code to interface: Since the collections framework is coded on the principles of “Code to Interface”, it aids in inter-operability between APIs that are not directly related. • Learning Curve: Easy to learn and start using Collections due to the “Code to Interface” principles.
  • 4. Core Collections Framework • The Collection framework forms a suitable hierarchy: Collection Set List Queue SortedSet Map SortedMap
  • 5. Core Collections Framework (Cont’d) • The core Collections framework is very generic in nature and provides a standard interface to all operations that can be performed. • For example the root interface Collection is declared as public interface Collection <E> – E represents a generic object. • Collection: This interface is pretty much used in declarations and method signatures so any type of Collections can be passed around. • Set: A Collection interface that cannot take any duplicate elements. • List: An ordered Collection interface that allows duplicate elements and provides object retrieval based on an integer index. • Queue: Apart from following the FIFO (First In First Out) principles this Collection offers variety of implementation. • Map: Supports Collection of key and value pairs. Design does not allow duplicate keys. • Sorted Set: Ordered version of the set interface. • Sorted Map: Maintains ascending order of keys.
  • 6. Operations Supported by Collection <E> interface • boolean isEmpty(); • boolean contains (Object element); • boolean remove (Object element); • Interface <E> iterator(); • boolean containsAll (Collection <> C); • boolean addAll (Collection <? extends E> C); • boolean removeAll (Collection() > C); c.removeAll (Collections.Singleton(obj); • boolean retainAll (Collection <?> C); • void clear(); • Object[ ] toArray(); • <T> T[ ] toArray(T[ ]a);
  • 7. Iteration • Two ways to iterate over Collections: – Iterator static void loopThrough(Collection col){ for (Iterator <E> iter = col.iterator();iter.hasnext()) { Object obj=iter.next(); } } – For-each static void loopThrough(Collection col){ for (Object obj: col) { //access object } }
  • 8. Set • Set Interface: It offers inherited services from the root Collection interface with the added functionality of restricting duplicate elements. • Implementations: Set HashSet TreeSet LinkedHashSet
  • 9. Example Code • Adding unique objects to a collection Collection <String> uniqueString (Collection<String> c) { Set<String> uniqueSetStr = new HashSet<String>(); for (String str: c) { if(!uniqueStrSet.add(str)){ System.out.println(“Duplicate deleted:”+ str); } } return uniqueStrSet;
  • 10. Set Implementation Comparisons • Idioms for using bulk operations on sets: • Copy a set when doing bulk operations to retain the original set. • Example: Set <String> unionSet = new TreeSet<String>(set1); unionSet.addAll(set2); HashSet TreeSet Linked HashSet Storage Type Hash Table Red-Black Tree Hash Table with a Linked List Performance Best performance Slower than HashSet Little costly than HashSet Order of Iteration No guarantee of order of iteration Order based Orders elements based on insertion
  • 11. List • In addition to the core services inherited from the root collection interface, the list interface offers – Positional access – Search – Customized Iteration – Range-view • List Implementation – ArrayList offers better performance compared to Linkedlist. List Arraylist Vector Linkedlist
  • 12. Typical usage of List • Bulk Operations – listA.addAll(listB); // append elements. • Positional access and search – Eg: To swap elements in a list. public static<String> void swap(List<String>strList, int k, int l) { String strTmp = strList.get(l); strList.set(k,strList.get(l)); strList.set(l,strTmp); }
  • 13. List Iterator • Customized Iteration: ListIterator – Offers iteration in both directions, forward and backward – Example: for(ListIterator<String> lIter=strList.listIterator(strList.size());lIter.hasPrevious()) { String str = lIter.previous(); }
  • 14. List Operations • Range-View: – subList(int from, int to) returns a view portion of the sublist starting from <from> to <to> exclusive.
  • 15. Algorithms available for List • sort: Uses mergesort • shuffle: Randomly shuffles elements • reverse: Reverses order of elements • rotate: Rotates list by specified number • fill: Overwrites every element with specified element. • copy: Copies source list into destination. • binarySearch: Performs search usng binary search. • indexOfSubList: Returns index of first subList found. • lastIndexOfSubList: Returns index of the last subList found.
  • 16. Queue • In addition to the inherited core services offered by Collection, queue offers following methods in two flavors: Purpose Throw Exception Return Special Value Insert Inserts an elements to the queue add(obj) offer(obj) Remove Remove head of the queue and return it. remove() poll() Examine Return the head of the queue element() peek()
  • 17. Map • Basic operations: – Val put (Object key); – Val get (Object key); – Val remove (Object key); – boolean containsKey (Object key); – boolean containsValue (Object value); – int size(); – boolean isEmpty(); • Bulk services – Void putAll(Map<? extends Key, ? extends Val> map); – void clear();
  • 18. Map (Cont’d) • Views public set<Key> keySet(); public collection<Val> values(); public set<Maps.Entry<Key,Val>> entrySet(); • Examples: Iteration over a map for(Map.Entry<?,vtype> elem: map.entrySet()) System.out.print(element.getKey()+”:”+element.getValue()); To find If a map contains another map If(map1.entrySet().containsAll(Map2.entrySet())) If two map objects contain same keys If(map1.keyset().equals(map2.keyset())) To find out common keys behave two map objects Set<Ktype> common keys = new Hashset<Ktype>(map1.commonkeys.retainAll(map2.keyset());
  • 20. SortedSet • SortedSet stores elements in ascending order as per the natural ordering or as defined by a comparator provided at instantiation time. • Also the iterator and toArray methods iterate and return array in sorted order. • Additional Services provided: – SortedSet <Element> subSet (Element fromElement, Element toElement); – SortedSet <Element> headSet (Element toElement); – SortedSet <Element> tailSet (Element fromElement); – element first(); – element last(); – Comparator <? Super Element> comparator();
  • 21. SortedMap • Organizes data in ascending order based on natural ordering of the keys or a comparator supplied at instantiation time. • Services provided in addition to Map interface: – Comparator <? super Key> comparator(); – SortedMap<Key, Value> SubMap(Key from, key to); – SortedMap<Key, Value> headMap (key to); – SortedMap<Key, Value> tailMap(key from); – Key firstKey(); – Key lastKey();
  • 22. Map: Pitfalls • ThreadSafety – The bulk operations like keySet(), values() and entrySet() return collections that can be modified automatically when the underlying collection changes.
  • 23. ThreadSafety • Collections by default are not threadsafe • In order to achieve thread-safety the following utility methods from <collection> can be employed. • Encapsulation – Implementation style, where the access to the collection is only from a thread-safe client • Synchronization – Collections.synchronizedList (new LinkedList(…)); • Unmodifiable – Collections.unmodifiableList(new LinkedList(…)); • Fail-safe iterator – Using fail-safe iterators makes sure the collection is not modified as it is iterated.