Anda di halaman 1dari 3

Following are some of the best practices relating to Java

Collection:
1) Use ArrayList, HashMap etc. as opposed to Vector, Hashtable etc. ,
wherever possible to avoid synchronization overhead. Even better is to se
!st arrays where possible. "f mltiple threads concrrently access a collection
and at least one of the threads either adds or deletes an entry into
the collection , then the collection mst be e#ternally synchronized. $his is
achieved by
%ap my%ap&Collections.synchronized%ap'my%ap)(
)ist my)ist&Collections.synchronized)ist'my)ist)(
*) Set the initial capacity of a collection appropriately (e.g. ArrayList,
HashMap etc.) . $his is becase Collection classes li+e ,rray)ist, -ash%ap
etc. mst grow periodically to accommodate new elements. .t if yo have a
very large array and yo +now the size in advance then yo can speed things
p by setting the initial size appropriately.
e.g. -ash%aps/ -ashtables need to be created with s0ciently large
capacity to minimize rehashing ' which happens every time the table
grows) . -ash%ap has two parameters initial capacity and load factor
that a1ects its performance and space re2irements. -igher load
factor vales ' defalt load factor of 3.45 provides a good trade o1
bet6n performance and space) will redce the space cost bt will
increase the loo+p cost of my%ap.get'7.) and my%ap.pt'7.)
methods . 8hen the nmber of entries in the -ash%ap e#ceeds the
current capacity * load factor then the capacity of -ash%ap is
roghly dobled by calling the rehash fnction. "t is also very important
not to set the initial capacity too high or load factor too low if iteration
performance or redction in space is important.
9) rogra! in ter!s of interface not i!ple!entation: for e#ample yo
might decide a )in+ed)ist is the best choice for some application, bt then
later decide ,rray)ist might be a better choice for performance reason.
:se:
)ist list&new ,rray)ist'133)(
"nstead of
,rray)ist arr&new ,rray)ist')(
;) "eturn #ero length collections or arrays as opposed to returning null
: retrning nll instead of zero length collection ' se Collections.E%<$=>?E$,
Collections.E%<$=>)"?$,Collections.E%<$=>%,<) is more error@prone, since
the programmer writing the calling method might forget to handle a retrn
vale of nll.
5) $!!utable ob%ects should be used as &eys for the HashMap:
generally yo se a !ava.lang."nteger or !ava.lang.?tring class as the +ey,
which are immtable Java ob!ects. "f yo deAne yor own +ey class then it is a
best practice to ma+e the +ey class an immtable ob!ect ' i.e. do not provide
any BsetterC methods .
D) 'ncapsulate collections : in general collections are not immtable
obe!ects. ?o care shold be ta+en not to nintentionally e#pose the collection
Aelds to the caller.
A(oid
$he following code snippet e#poses the ?et BsetCarsC directly to the
caller. $his approach is ris+ier becase the variable BcarsC can be modiAed
nintentionally.
pblic class Car=ard
E
private ?etFCarG cars&new -ash?etFCarG')(
// e#poses the cars to the caller
pblic ?etFCarG getCars')
E
retrn cars(
H
// e#poses the cars to the caller
pblic void setCars'?etFCarG cars)
E
this.cars&cars(
H
H
)etter approach
$his approach prevents the caller from directly sing the nderlying
variable BcarsC.
pblic class Car=ard
E
private ?etFCarG cars&new -ash?etFCarG')(
pblic void addCar'Car car)
E
cars.add'car)(
H
pblic void removeCar'Car car)
E
cars.remove'car)(
H
pblic ?etFCarG getCars')
E
// se factory method from the Collections
retrn Collections.nmodiAable?et'cars)(
H
H

Anda mungkin juga menyukai