Professional Documents
Culture Documents
Collections Framework
Collections Framework
Ashwaq El-shair
advantages of a collections
framework
Reduces programming effort by
providing useful data structures and
algorithms so you don't have to write
them yourself.
Increases performance by
providing high-performance
implementations of useful data
structures and algorithms.
advantages of a collections
framework ,cont.
Provides interoperability between
unrelated APIs by establishing a common
language to pass collections back and forth.
Reduces the effort required to learn APIs
by eliminating the need to learn multiple ad hoc
collection APIs.
Reduces the effort required to design and
implement APIs by eliminating the need to
produce ad hoc collections APIs.
Fosters software reuse by providing a
standard interface for collections and algorithms
to manipulate them.
Collection Interfaces
The most basic interface is Collection.
These interfaces extend Collection:
Set, List, SortedSet, NavigableSet, Queue,
Deque, BlockingQueue and BlockingDeque.
The other collection interfaces, Map,
SortedMap, NavigableMap, ConcurrentMap
and ConcurrentNavigableMap do not extend
Collection, as they represent mappings
rather than true collections.
Collection Implementations
ArrayList
ArraylistisaclasswhichimplementsListinterfaceand
extendsAbstractListclass.
Theclassjava.util.ArrayListprovidesresizablearray,which
meansthatitemscanbeaddedandremovedfromthelist.
Itiswidelyusedbecauseofthefunctionalityandflexibilityit
offers.
MostofthedeveloperschooseArraylistoverArrayasitsa
verygoodalternativeoftraditionaljavaarrays.
AnArrayListisadynamicdatastructuresoitcanbeused
whenthereisnoupperboundonthenumberofelements.
DifferencebetweenArrayandArrayList
1. Resizable :
Array
ArrayList
Dynamic in size .
Each ArrayList object has instance variable
capacity which indicates the size of the ArrayList.
As elements are added to an ArrayList its capacity
grows automatically.
2.Primitives
Array
ArrayList
3. Length
Array
Each array object has the length variable which
returns the length of the array.
Arraylist
Length of the ArrayList is provided by the size()
method.
4. Addingelements
Array
ArrayList
5. Multidimensional
Array
ArrayList
Single dimensional
ArrayList Constructors
TheArrayListclasssupportsthreeconstructors:
1. Arraylist()
Thisconstructorbuildsanemptylist.
2. ArrayList(Collection c)
Thisconstructorbuildsanarraylistthatisinitializedwiththe
elementsofthecollectionc.
3. ArrayList(int capacity)
Thisconstructorbuildsanarraylistthathasthespecifiedinitial
capacity.Thecapacityisthesizeoftheunderlyingarraythatisusedto
storetheelements.Thecapacitygrowsautomaticallyaselementsare
addedtoanarraylist.
Forexample,ifyouwanttocreateanemptyarraylistof
Stringsthenyouwoulddothefollowing:
ArrayList<String>list=newArrayList<String>();
Ifyouwanttocreateanarraylistwithinitialcapacity,then
youshoulddothefollowing:
ArrayList<Integer>list=newArrayList<Integer>(7);
NotethatArrayListclasssupportsonlyobjecttypesand
notprimitivetypes.
Addsthespecifiedelementtotheendofthislist.
voidadd(intindex,Elemente)
Addsthespecifiedelementatthespecifiedpositioninthelist.
Removingelementsfromthelist
voidclear()
Removesalltheelementsfromthelist.
Eremove(intindex)
Removestheelementatthespecifiedpositioninthelist.
protectedvoidremoveRange(intstart,intend)
Removesfromthelistalltheelementsstartingfromindexstart
(included)untilindexend(notincluded).
Gettingelementsfromthelist
Eget(intindex)
Returnstheelementatthespecifiedposition.
Object[]toArray()
Returnsanarraycontainingalltheelementsofthelistinproper
sequence.
Settinganelement
Eset(intindex,Eelement)
Replacestheelementatthespecifiedpositionwiththespecified
element.
Searchingelements
booleancontains(Objecto)
Returnstrueifthespecifiedelementisfoundinthelist.
intindexOf(Objecto)
Returnstheindexofthefirstoccurrenceofthespecifiedelementinthe
list.Ifthiselementisnotinthelist,themethodreturns1.
intlastIndexOf(Objecto)
Returnstheindexofthelastoccurrenceofthespecifiedelementinthe
list.Ifthiselementisnotinthelist,themethodreturns1.
Iteratingthearraylist
Iteratoriterator()
Returnsaniteratorovertheelementsinthelist.
ListIteratorlistIterator()
Returnsalistiteratorovertheelementsinthislist.
Checkingifthelistisempty
booleanisEmpty()
Returnstrueifthelistdoesnotcontainanyelement .
Gettingthesizeofthelist
intsize()
Returnsthelengthofthelist(thenumberofelementscontainedinthe
list).