Java Reference
In-Depth Information
ation nodes) and disk sectors. And bitsets are useful in Huffman coding , a data-com-
pression algorithm for achieving lossless data compression.
Creating Your Own Collections
Arrays,theCollectionsFramework,andlegacyclassessuchas BitSet aresuitablefor
organizinggroupsofobjects(or,inthecaseof BitSet ,setsofbitsthatareinterpreted
asBooleantrue/falsevalues),andyoushouldusethemwhereverpossiblebeforecreat-
ing your own collection APIs. After all, why “reinvent the wheel?”
The Collections Framework supports lists, sets, queues, deques, and maps. If your
collectionrequirementcanfitintooneofthesecategories,thengowiththisframework.
Keepinmindthatyoucanalsotakeadvantageoftreesin TreeSet and TreeMap im-
plementation contexts, and stacks in deque contexts.
Perhaps you need a different implementation of one of the Collections Framework
core interfaces. If so, you can extend this framework by implementing the interface,
or by subclassing one of the more convenient “ Abstract ” classes, such as Ab-
stractQueue . For example, author Cay Horstmann demonstrates extending this
classtoimplementacirculararrayqueue(see http://www.java2s.com/Code/
Java/Collections-Data-Structure/Howtoextendthecollec-
tionsframework.htm ) .
OBEYING CONTRACTS
Whenyouimplementacoreinterfaceorextendoneofthe Abstract classes,you
should ensure that your implementation class doesn't deviate from the various con-
tracts described in the Java documentation for these interfaces. For example, List
places the following stipulation on the hashCode() method that it inherits from
Collection :
The hash code of a list is defined to be the result of the following calculation:
int hashCode = 1;
for (E e: list)
hashCode = 31*hashCode+(e==null ? 0 : e.hashCode());
This calculation ensures that list1.equals(list2) implies that
list1.hashCode() == list2.hashCode() for any two lists, list1 and
list2 , as required by the general contract of Object.hashCode() .
Search WWH ::




Custom Search