61
Chapter 11. Collections and Maps Exam Objectives hashCode() ! equals()" Supplementary Objectives #$ % Collection& Set& SortedSet& List& Map& SortedMap ’$addAll()& removeAll()& retainAll() Collection ()* #$ % HashSet& TreeSet& LinkedHashSet& ArrayList& Vector& LinkedList& HashMap& Hashtable& TreeMap& LinkedHashMap + && ’$Comparable $Comparator ,UnsupportedOperationException $ #$Collections *$ ,-$Collections Page 1 of 61 6/16/2006 file://C:\Documents and Settings\cdot\Local Settings\Temp\~hhAFF6.htm

Khalid Mughal Chapter 11

Embed Size (px)

Citation preview

Page 1: Khalid Mughal Chapter 11

����������� �

Chapter 11. Collections and Maps Exam Objectives

� ������ �� ���������������������������������������������������������� ��������

�����������������������

� ��������������� ���������������������������� ����������������hashCode( ) �!����equal s( )"����������

Supplementary Objectives

� #������$����������������������������������������������������������������� �%�Col l ect i on&�Set &�Sor t edSet &�Li st &�Map&�Sor t edMap��

� '����������������������������� ���������������������� ���

� '��������������� ��������� ����������$�����addAl l ( )&�r emoveAl l ( )&�����r et ai nAl l ( ) ����������������Col l ect i on������������

� (������)���������������������*������������������������ ����������

� '������������ ������������� ��������� �����������������

� #������$������� ���������������������������������������������������������������

�����%�HashSet &�Tr eeSet &�Li nkedHashSet &�Ar r ayLi st &�Vect or&�Li nkedLi st &�HashMap&�Hasht abl e&�Tr eeMap&�Li nkedHashMap��

� +����������������������� ������������������

� '�������������������������������������������������������������

� ��������������� �������������&��������&�������������������������

� '������������������������������������������� ���������$�����Compar abl e������������������������������������ ������$�����Compar at or ������������

� ,�������������������������� ��������Unsuppor t edOper at i onExcept i on���$�����������������������

� #������$��������������������Col l ect i ons ��������������������������� ���������������������������*��������������������������$ �������������������

� ,�����������-�������������� ���������$�����Col l ect i ons��������

Page 1 of 61

6/16/2006file://C:\Documents and Settings\cdot\Local Settings\Temp\~hhAFF6.htm

Page 2: Khalid Mughal Chapter 11

� +������������� ������������ ��������$�������������������

���������� ����������� �

11.1 The Collections Framework

.���������������� �������� ������-�������������������������������������.�������$���-������������������&����������&��������� ����������������������������������

/��������������������������������������������� �������-��������������������������� ���� ��������������������������������$�������������������������������������������������� ������� ���������������j ava. ut i l � �������������� ����������������� ����%�

� ��������������������������� ����������������������� ����������� �������$����������

�� ������������!����0������11�1�����������11�1"�������������������������������������������������$��2���������$����������������������������������2���������� �����������������

��������������� ��������������

� .���������������� ������������!����&�����������������&�����������������11�1"����������� �������

�� �����������������������������������&� �������������������������������� ���������������������$��

� .���������������������������������������������������������� ���������������� ������������

�����������&������������������������������&��������������������)����������������

Core Interfaces

����Col l ect i on������������������������)����������������������������������������&�������������� ��������������������������������������$�������������������� �����0������11�1�������������������������������)������������11�1��

Page 2 of 61

6/16/2006file://C:\Documents and Settings\cdot\Local Settings\Temp\~hhAFF6.htm

Page 3: Khalid Mughal Chapter 11

������������������Set ���������������&��������&����� ���������������������������������������������������������������Li st ������������&������������������������������������������������������������ ���������������������

.�������������������0������11�1�&�����Map���������������������2���������Col l ect i on������������������������ �����$&����� ����������������������.��� ����������������������������#������������� ����!�����������������"���������������������-�������������������������-������.���$����&��������&��������������� ���������������.������������� ����&�����Sor t edMap������������2����������Map����������������������������� ������������������������

Implementations

����j ava. ut i l � ������� ���������� ������������������������������� ���*��� �����������������$ ��&�������������������������������0�������11�3�����11�4���� ���������������������������� ���� �������������������������������������� ��������� ��������������5���������������������� ����������������������������$����������Col l ect i on��������������������������������� �������������������� ���������������������������� ����������

��������������� ���� ������ ���������������������������������� ���

���������� ���������������������� ������ ��������� ���

���������� ��������� �� � �������

��������

Col l ect i on� .������������������������������������������ ������������������ �������������������-���������������������������������������������������

Set � ����Set ������������2����������Col l ect i on���������������� ��������������������������������%��������������������������

HashSet Li nkedHashSet

Sor t edSet � ����Sor t edSet ������������2����������Set �������������� ��������������������������������$�������������������������� ���������������������������������������������������

Tr eeSet �

Li st � ����Li st ������������2����������Col l ect i on�����������������������������������������������������������������������

Ar r ayLi st Vect or Li nkedLi st

Map� .������������������������������� ��������������������������� ����������$�������������

HashMap Hasht abl e Li nkedHashMap

Sor t edMap� 62����������Map����������������� ������������������������ �����������������$��������

Tr eeMap�

Page 3 of 61

6/16/2006file://C:\Documents and Settings\cdot\Local Settings\Temp\~hhAFF6.htm

Page 4: Khalid Mughal Chapter 11

��������������� ��������������������������������������� ���

$�����������&�������������������������� �������������������� ���������������������������������������������������������������������������������Col l ect i on���-���� ���������������������������� ��

Page 4 of 61

6/16/2006file://C:\Documents and Settings\cdot\Local Settings\Temp\~hhAFF6.htm

Page 5: Khalid Mughal Chapter 11

������ �������������������������������������������$������$� ���������������������������������������������������������� �����������������������������������$����������������� ����Map��� ����������������������������������� ���������������������������5����������������������!������� "����$������������������������-����&����������������������-������

��������������������� ��������������������&���������������������������������� �������������������������������������$ ��&��������������$������� �������������$ ����$������������������������ �������������������������-�������������&����������� ���������������������������������������$��

.��������������������������� �����0�������11�3�����11�4��� �����������Ser i al i zabl e���������Cl oneabl e�����������7����������&�������-������������������������������������)��������������������

.�������$��������������������� ��� �������������������������������11�3����������������������������� ������������������������������������������������������ ��������� ��������������������������������������� ��������������

��������� �����!� ��� ������ ���������������������� ���

� �������

� ������ �"����

���������� ����������� #������" ����� ���� ���

������� ��

$��������

����� ����������

��%�����

������������ ��

���&�����

HashSet � Set � '���������������

5�������� equal s( ) hashCode( )

8����������

Li nkedHashSet � Set � '���������������

#��������������� equal s( ) hashCode( )

8�������������������$*������������

Tr eeSet � Sor t edSet � '���������������

,������ equal s( ) compar eTo( )

�������������

Ar r ayLi st � Li st � .��� ��� #��������������� equal s( ) � (���)���������$�

Li nkedLi st � Li st � .��� ��� #��������������� equal s( ) � ������������

Vect or � Li st � .��� ��� #��������������� equal s( ) � (���)���������$�

HashMap� Map� '��������$��

5�������� equal s( ) hashCode( )

8����������

Li nkedHashMap� Map� '��������$��

9�$�����������������.�����������������������

equal s( ) hashCode( )

8�������������������$*������������

Hasht abl e� Map� '��������$��

5�������� equal s( ) hashCode( )

8����������

Tr eeMap� Sor t edMap� '��������$��

,�����������$�������

equal s( ) compar eTo( )

�������������

���������� ����������� �

11.2 Collections

Page 5 of 61

6/16/2006file://C:\Documents and Settings\cdot\Local Settings\Temp\~hhAFF6.htm

Page 6: Khalid Mughal Chapter 11

����Col l ect i on������������ ��������������������������������������������������� ��������,������������ ������������������������������� ������&�����������������������������$����������� ���������������� ������������������������ ����������������� �����Unsuppor t edOper at i onExcept i on� ������������������� �������������������������������������j ava. ut i l � ��������� ������������� �������� ����������������Col l ect i on�����������!����0������11�3�����������11�3"��

���$�������������������������bool ean������������������� ���������������������� �������������������������������� ���������

Basic Operations

����������� �������������������������$����������������������������������������� �����������������������������������������������������

i nt s i ze( ) bool ean i sEmpt y( ) bool ean cont ai ns( Obj ect el ement ) bool ean add( Obj ect el ement ) / / Opt i onal bool ean r emove( Obj ect el ement ) / / Opt i onal

����add( ) �����r emove( ) ����������������t r ue������������������� �������������������������������� ���������

$���������������������f al se&�����add( ) ���������������������������������������2��������� �������&�������������������������������$���������������-����������������������������-�����

����cont ai ns( ) ���������������������������� �������������������-������������������������������-�����������������$��

Bulk Operations

������� ��������� �����������������������������������������

bool ean cont ai nsAl l ( Col l ect i on c) bool ean addAl l ( Col l ect i on c) / / Opt i onal bool ean r emoveAl l ( Col l ect i on c) / / Opt i onal bool ean r et ai nAl l ( Col l ect i on c) / / Opt i onal voi d c l ear ( ) / / Opt i onal

������������ ������������������������ ��������������������������������������������������������!����-�����������"������cont ai nsAl l ( ) ����������������t r ue������������������������� �����������������������������������������������������������������

����addAl l ( )&�r emoveAl l ( )&�����r et ai nAl l ( ) ����������������������������������������������������������� ��������$����������������������������������� ��������� ����������$��������������������������)����$�:����������������0������11�;��

���������'�&����#������ ��� ��� ������ ���

Page 6 of 61

6/16/2006file://C:\Documents and Settings\cdot\Local Settings\Temp\~hhAFF6.htm

Page 7: Khalid Mughal Chapter 11

Array Operations

������� ������������������������������������$���

Obj ect [ ] t oAr r ay( ) Obj ect [ ] t oAr r ay( Obj ect a[ ] )

����������t oAr r ay( ) �����������������������$� ����������������������������������������������������������������������������������������������������������$������� ���������$ ���

#����������������$��������������&�������������������������������������$��#�������������������� ���������������$&��������&�����������������������$���������������������������������������������������������&������ ������������������� ����nul l �����������������������$��������������#����������$�������������&����� �����$����������������������$ ������� �� ��������)��������������#���������������$ ���������� ������������$������������ ���$ ������������������$ ���������$��������������������������&����Ar r aySt or eExcept i on�������� ���

Iterators

.������������ �������������������� ��������� �����������������������������������������������������.����������������������������$������������������ ������������������Col l ect i on����������%�

I t er at or i t er at or ( )

(������������-���� ������� ������������I t er at or ������������

�����I t er at or ������������������������������ �%�

bool ean hasNext ( )

(�������t r ue��������������$��������������������������������������������������.��������������������next ( ) �������� �����������������2�������������������������������

Obj ect next ( )

����������������������������2�����������������������$��������������&����������������������������������#�������������������������������������������&�������� ����NoSuchEl ement Except i on��

Obj ect r emove( )

Page 7 of 61

6/16/2006file://C:\Documents and Settings\cdot\Local Settings\Temp\~hhAFF6.htm

Page 8: Khalid Mughal Chapter 11

(������������������������ �������������$����������������������next ( ) �������&����������������$����������������#����������������������������������I l l egal St at eExcept i on&��������next ( ) ����������������$��������������&���� ��������r emove( ) ������������������$����������������������������������������next ( ) ������������������������� ����������������������&��������&�������� �����Unsuppor t edOper at i onExcept i on���������������� ����������������� �������

�.��������������������������������������������&������������� ���������$�����I t er at or ���������������������������$�����������$������������������������������������$��������������������$������62�� ���11�1�������������������������������������

$(����������)�������������� ��

i mpor t j ava. ut i l . * ; publ i c c l ass I t er at or Usage { publ i c st at i c voi d mai n( St r i ng[ ] ar gs) { / / ( 1) Cr eat e a l i s t of I nt eger s. Col l ect i on i nt Li st = new Ar r ayLi s t ( ) ; i nt [ ] val ues = { 9, 11, - 4, 1, 13, 99, 1, 0 } ; f or ( i nt i = 0; i < val ues. l engt h; i ++) i nt Li st . add( new I nt eger ( val ues[ i ] ) ) ; Syst em. out . pr i nt l n( " Bef or e: " + i nt Li st ) ; / / ( 2) I t er at or i nt er at or = i nt Li st . i t er at or ( ) ; / / ( 3) Get an i t er at or . whi l e ( i nt er at or . hasNext ( ) ) { / / ( 4) Loop I nt eger el ement = ( I nt eger ) i nt er at or . next ( ) ; / / ( 5) The next el ement i nt val ue = el ement . i nt Val ue( ) ; i f ( val ue < 1 | | val ue > 10) / / ( 6) Remove t he el ement i f i nt er at or . r emove( ) ; / / i t s val ue i s not bet ween 1 and 10. } Syst em. out . pr i nt l n( " Af t er : " + i nt Li st ) ; / / ( 7) } }

<�� ������������ ������%�

Bef or e: [ 9, 11, - 4, 1, 13, 99, 1, 0] Af t er : [ 9, 1, 1]

62�� ���11�1���������������������������������������������������������������������������������������� ����1�����1=&�����������������2�� �����������Ar r ayLi st ���������������������������,�����i nt ������������������-����&����$������������������������������������������������&������i nt ��������������� �� ���������I nt eger ���-���&� ��������������������������������������

����Col l ect i on��������������� ����������$���-����������������������������������+������������������������&������������������� ���������������$ ��Obj ect ��<�����������&���������������������$������ �����������������������������������-������������������$ �*� �����������������

���������������������������������������������t oSt r i ng( ) ����������� �����������2������� ������������

Page 8 of 61

6/16/2006file://C:\Documents and Settings\cdot\Local Settings\Temp\~hhAFF6.htm

Page 9: Khalid Mughal Chapter 11

����������������������������������2������� �����������������������$�����t oSt r i ng( ) ����������������������������

�[ �������

�, ��������

�, . . . , ��������

�] �

����������������������������2������� �����������������������$�����t oSt r i ng( ) ���������������

���������������������������������������.��!3"��������!>"�����t oSt r i ng( ) ������������������������������������������ ������$�����������������2������� ��������������������������������

#��62�� ���11�1&����������������������������!4"�������������������� ����!;"��������������������������������������������������������������������������������2��������������� ���������!?"��#��������������������������������i nst anceof �� ���������������������$ �������������������-���������������� ���������� ���������������������+������������������������������ ����2�� ����������� ����� ����������������������������������������������$�I nt eger���

5������������������������������������������������&���������������$���������������������������������������������������������������������� ������������� &������� �����62�� ���11�1��

������-����$������������������ ���������������j ava. ut i l � ��������������������������������+��������������������������$��������������&������������$������$��������������$����������������$������������� �����������������������������,������������������������������� �������� ���Concur r ent Modi f i cat i onExcept i on������r emove( ) ���������������������������������$������������� �$�����������������������������������$�������������������������������� �����������������

������������� ������������������ ��������������������������������������$����������������� ������������������������������ �������$�����������������0����2�� ��&������������������������ �����������������������������������������������������$�����������������&� ���������������������������������������������������������$������������ ��������������.����������������������������������� ������������������������������������������������������������������������� �������������������������� �������������������������������������

����������� ����������� �

Review Questions

��� +���������������������������������������������������������� ���@�

,��������������������������� �����

� Set �

� Bag�

Page 9 of 61

6/16/2006file://C:\Documents and Settings\cdot\Local Settings\Temp\~hhAFF6.htm

Page 10: Khalid Mughal Chapter 11

� Li nkedLi st �

� Col l ect i on�

� Map�

��� +���������������� ����������������� ���������$�����j ava. ut i l � ������@�

,����������� ������������� �����

� HashLi st �

� HashMap�

� Ar r aySet �

� Ar r ayMap�

� Tr eeMap�

��� +������������������������������������������� ������������������������������������*������������������������@�

,������������������������� ����

� Col l ect i on�

� Set �

� Sor t edSet �

� Li st �

� Sequence�

���������� ����������� �

11.3 Sets

'�������������� ��������������������Col l ect i on����������&��� ��������������������Set ���������������������� ��� ��������������������������������������������������������������������nul l ������������Set �����������������������������$��� ��������&���������add( ) �����addAl l ( ) ��������� ���������������� ���������#����������������������������$�����������&�� ���������������������������add( ) ������������������������������ �����������������t r ue&������f al se��.�Set ���������������������������!����������11�4"&��������&���������������������������������������������-������

��������!����������"���������� ��� ���������������������������� �������������������Set �������������������������������������������������������������������������������������

Page 10 of 61

6/16/2006file://C:\Documents and Settings\cdot\Local Settings\Temp\~hhAFF6.htm

Page 11: Khalid Mughal Chapter 11

HashSet and LinkedHashSet

����HashSet ��������� ������������Set ����������. �,������������ �����������������������������&������������������������*����� ��������������������� ����������.�HashSet ����������������������$���������������������������8� ����&�����Li nkedHashSet ������������HashSet ���������������������*������������������������� �������Tr eeSet &� ������� ������������Sor t edSet ��������������������������������������� ��2��$�!����,�������11�A&� ��;?3"��

.�HashSet ����������������� �������������������hashCode( ) �����equal s( ) �������������������������!����,�������11�>&� ��;A1"������hashCode( ) ����������������������������������������&���������equal s( ) ������������������������� ����������������#������&������������$�����������������������HashSet �������������������������������������$���������������������������������������

.�������������������&�����Li nkedHashSet ��� ���������������������������������HashSet ��������#�� �������������$������HashSet &��2�� ������������ ���������������'��������HashSet &���Li nkedHashSet ������������������������������ ��������������������������������������&��������&����������������� ��������$� ����������������������Li nkedHashSet ��

����Li nkedHashSet ��������������������������������������������������������HashSet ���������������������� ������!����&����������������������������������������"��������������������!����&��������������������������������������������������������� ����$"�������������� ����������������������������������������������������� ���������� ����������������������������� ����������� ������ ������������

HashSet ( )

B�������������� &��� �$������

HashSet ( Col l ect i on c)

B�������������� ������������������������������������� �������������������������� ����� ������������������$��� ���������������������������������� �$������������� ���������������������������

HashSet ( i nt i ni t i al Capaci t y)

B�������������� &��� �$����� ��������� ������������������ ����$��

HashSet ( i nt i ni t i al Capaci t y, f l oat l oadFact or )

���������&����#������ ������� ���* ����

������� ���+����������������,� � ����� �������������������#������ ���

a. cont ai nsAl l ( b) � b� �a�!������"�

a. addAl l ( b) � a = a� �b�!�����"�

a. r emoveAl l ( b) � a = a – b�!����������"�

a. r et ai nAl l ( b) � a = a� �b�!������������"�

a. c l ear ( ) � a =�C�!�� �$����"�

Page 11 of 61

6/16/2006file://C:\Documents and Settings\cdot\Local Settings\Temp\~hhAFF6.htm

Page 12: Khalid Mughal Chapter 11

B�������������� &��� �$����� ��������� ������������������ ����$���������� ���������������������

�$(����������)����� ����

i mpor t j ava. ut i l . * ; publ i c c l ass Char act er Set s { publ i c st at i c voi d mai n( St r i ng[ ] ar gs) { i nt numAr gs = ar gs. l engt h; / / A set keepi ng t r ack of al l char act er s pr evi ousl y encount er ed. Set encount er ed = new HashSet ( ) ; / / ( 1) / / For each pr ogr am ar gument i n t he command l i ne . . . f or ( i nt i =0; i <numAr gs; i ++) { / / Conver t t he cur r ent ar gument t o a set of char act er s. St r i ng ar gument = ar gs[ i ] ; Set char act er s = new HashSet ( ) ; / / ( 2) i nt s i ze = ar gument . l engt h( ) ; / / For each char act er i n t he ar gument . . . f or ( i nt j =0; j <si ze; j ++) / / add char act er t o t he char act er s set . char act er s. add( new Char act er ( ar gument . char At ( j ) ) ) ; / / ( 3) / / Det er mi ne whet her a common subset exi st s. ( 4) Set commonSubset = new HashSet ( encount er ed) ; commonSubset . r et ai nAl l ( char act er s) ; bool ean ar eDi s j unct = commonSubset . s i ze( ) ==0; i f ( ar eDi s j unct ) Syst em. out . pr i nt l n( char act er s + " and " + encount er ed + " ar e di s j unct . " ) ; el se { / / Det er mi ne super set and subset r el at i ons. ( 5) bool ean i sSubset = encount er ed. cont ai nsAl l ( char act er s) ; bool ean i sSuper set = char act er s. cont ai nsAl l ( encount er ed) ; i f ( i sSubset && i sSuper set ) Syst em. out . pr i nt l n( char act er s + " i s equi val ent t o " + encount er ed) ; el se i f ( i sSubset ) Syst em. out . pr i nt l n( char act er s + " i s a subset of " + encount er ed) ; el se i f ( i sSuper set ) Syst em. out . pr i nt l n( char act er s + " i s a super set of " + encount er ed) ; el se Syst em. out . pr i nt l n( char act er s + " and " + encount er ed + " have " + commonSubset + " i n common. " ) ; } / / Updat e t he set of char act er s encount er ed so f ar . encount er ed. addAl l ( char act er s) ; / / ( 6) } } }

Page 12 of 61

6/16/2006file://C:\Documents and Settings\cdot\Local Settings\Temp\~hhAFF6.htm

Page 13: Khalid Mughal Chapter 11

(����������� ������� ������������� �������������%�

j ava Char act er Set s i sai d i am mai ds

�������������������� ������� ��%�

[ i ] and [ ] ar e di s j unct . [ d, a, s, i ] i s a super set of [ i ] [ i ] i s a subset of [ d, a, s, i ] [ a, m] and [ d, a, s, i ] have [ a] i n common. [ d, a, s, m, i ] i s equi val ent t o [ d, a, s , m, i ]

62�� ���11�3������������������� ����������#�������������������������� ���� ����� ��������������������%�

� ����������$��������-����&��������&�������������������������

� ����������$�����������������������&��������&����������������

� ������������������������������������

� ������������������ �������������������

� ����������$����������������������

D��������������� �������� ����������������&������������������������������������������������������������������ ����� ������������������������������������������������������ ���������������������������������������������������

����������� �������������������������������������������� ����������������������������&�����������encount er ed�������������������������!1"��0����������������&�������������������������������&������� �����!3"����������&��������char act er s&���� � ������� ������������������������������������������&������� �����!4"������ ������������������������������������������������������������� ������&������� �����!;"%�

/ / Det er mi ne whet her a common subset exi s t s. ( 4) Set commonSubset = new HashSet ( encount er ed) ; commonSubset . r et ai nAl l ( char act er s) ; bool ean ar eDi s j unct = commonSubset . s i ze( ) ==0;

5�������������r et ai nAl l ( ) �� ������������������������������������!;"���������������������encount er ed���������char act er s�������#��������)��������������������������)���&�����������������-����7������ ���&���������������� �������������� ����� �������������������� ����������������������������������!?"�����������cont ai nsAl l ( ) ���������

/ / Det er mi ne super set and subset r el at i ons. ( 5) bool ean i sSubset = encount er ed. cont ai nsAl l ( char act er s) ; bool ean i sSuper set = char act er s. cont ai nsAl l ( encount er ed) ;

������������������������������������ ����������������������������#�����$���������������&��������&���������������� ����������������� ��2����&����$����$�������������������������������������������������������������������������������������������������������������� ��������$������������������addAl l ( ) ������������������������� �� ���&������� �����!A"%�

Page 13 of 61

6/16/2006file://C:\Documents and Settings\cdot\Local Settings\Temp\~hhAFF6.htm

Page 14: Khalid Mughal Chapter 11

encount er ed. addAl l ( char act er s) ; / / ( 6)

��2������� �������������������������� ������$�����������������������t oSt r i ng( ) ��������Abst r act Set ���������� ���������HashSet ��� ���������������������!����0������11�3"��

���������� ����������� �

11.4 Lists

���������������������������������������������������������&������������������� �������������������������������������������6�����������&����������&������� ���������������������.�)���*����������2������������������������������������������ ��������������������$���������2������������ ������������������������������������������������������������������������������������

#������������������� ����������������������������Col l ect i on����������&�����Li st ������������������������� �������������� ����� ���������$���������%� �������*���������������������������������&��������������������&��������������������)������������&������ ������������ ���������������!�������� �������������� ��������"������������������������������$���� ���������$���������� �������������������Li st ����������%�

/ / El ement Access by I ndex Obj ect get ( i nt i ndex)

(��������������������������� ������������2��

�Obj ect set ( i nt i ndex, Obj ect el ement ) �����������< �������

(� �������������������������� ������������2� ��������� �����������������#�������������� ������������������������ ������������2��

�voi d add( i nt i ndex, Obj ect el ement ) �������������< �������

#������������ ������������������������ ������������2��#����������$&����������������������� ��������$�������������2�������$������������������������� ���������� ����������������������������������������������add( Obj ect ) ����������Col l ect i on����������� ����� ��������� ����������������������������������������

�Obj ect r emove( i nt i ndex) �������������������������< �������

���������������������������������������� ������������2&��������������������������������$�����������������������r emove( Obj ect ) ����������Col l ect i on����������� ��������������������������������������������������������������

�bool ean addAl l ( i nt i ndex, Col l ect i on c) ����������< �������

#������������������������������ ��������������������������� ������������2&���������������������������� ���������������������������������������t r ue������$���������� �����������

Page 14 of 61

6/16/2006file://C:\Documents and Settings\cdot\Local Settings\Temp\~hhAFF6.htm

Page 15: Khalid Mughal Chapter 11

#�������*�� �$�����&�����������������������������2�0����������������������������si ze( ) - 1��.������������2 �����&����������������� ����I ndexOut Of BoundsExcept i on�������������������2����� ���������

/ / El ement Sear ch i nt i ndexOf ( Obj ect o) i nt l ast I ndexOf ( Obj ect o)

����������������� �������$����������������2����������������������������������������������������������������������������������������7������ ���&�����������E1��������������

/ / Li st I t er at or s Li st I t er at or l i s t I t er at or ( ) Li st I t er at or l i s t I t er at or ( i nt i ndex)

����������������������������������������������������������������������$&���������� ���������������������������������&� ��������������������������������������������������������������������������������������������������$������ ������������2��

i nt er f ace Li st I t er at or ext ends I t er at or { bool ean hasNext ( ) ; bool ean hasPr evi ous( ) ; Obj ect next ( ) ; / / El ement af t er t he cur sor Obj ect pr evi ous( ) ; / / El ement bef or e t he cur sor i nt next I ndex( ) ; / / I ndex of el ement af t er t he cur sor i nt pr evi ousI ndex( ) ; / / I ndex of el ement bef or e t he cur sor voi d r emove( ) ; / / Opt i onal voi d set ( Obj ect o) ; / / Opt i onal voi d add( Obj ect o) ; / / Opt i onal }

����Li st I t er at or ��������������������������������������������������#���2����������I t er at or ������������������� ������������������������������������������������+��������������������&�������������� ������������������������������� ����������� ������������������������� ��������������������������next ( ) ���������pr evi ous( ) �������&���� �������$������������������������������ ������������������������+��������r emove( ) �����������������&������������������ �������������������������������������

/ / Open Range- Vi ew Li st subLi st ( i nt f r omI ndex, i nt t oI ndex)

��������������������������������������&� ����������������������������������������������������������2�f r omI ndex ������������2�t oI ndex- 1��.���� ����� ����������������� ���������������������$������������������� ��������.�$������������������� ����������������������������$��������&�����������������:�� ����������������� �������� ������������� �������������������������

�ArrayList, LinkedList, and Vector

�������� ��������������������Li st ��������������� ���������������j ava. ut i l � ������%�Ar r ayLi st &�

Page 15 of 61

6/16/2006file://C:\Documents and Settings\cdot\Local Settings\Temp\~hhAFF6.htm

Page 16: Khalid Mughal Chapter 11

Li nkedLi st &�����Vect or ��

����Ar r ayLi st ��������� ������������Li st ����������������Vect or �����������������$�������������������������������������� �����������Li st ����������������Vect or �����Ar r ayLi st ��������������� ����������������$��������$�����)���������$�&� ���������������������������������������������������F���$���������������������������$�����$��'����������Ar r ayLi st ������&�����Vect or ����������������*����&��������������������������������������������� ����������� �������������������$��

����Li nkedLi st ��� ������������������������$*�������������#����������������������������������$*�������������������$����������F������������������������&���������������������������$������Li nkedLi st ������� ���������2������������������� �������� �����������������&����&����������������������������������������Li nkedLi st %�

voi d addFi r st ( Obj ect obj ) voi d addLast ( Obj ect obj ) Obj ect get Fi r st ( ) Obj ect get Last ( ) Obj ect r emoveFi r st ( ) Obj ect r emoveLast ( )

�����Ar r ayLi st �����Vect or ������������������ ������� ����������&�����Vect or ���-��������������������� ����������� �����$���������$�������)�������/�������*�������������������������*����� �������������������Ar r ayLi st �����Vect or ����������8� ����&� �������*�������������������������������������Li nkedLi st , �� ���������������������������$*�������������+���������������������������������������������������������&���Li nkedLi st �������� ������������������#������������&�����Ar r ayLi st ��� �����������������������*���������������������� �����������������

����Ar r ayLi st ������� ����������������� ����������������%�

Ar r ayLi st ( )

B�������������� &��� �$�Ar r ayLi st ��.��������������������������� ���������$�����Li nkedLi st �����Vect or ����������

Ar r ayLi st ( Col l ect i on c)

B�������������� �Ar r ayLi st ��������������������������������� �������������������������� �Ar r ayLi st � �������������$��� �����������������������������Ar r ayLi st � �������������������$�������������������������������������������������������� �������������������.��������������������������� ���������$�����Li nkedLi st �����Vect or ����������

Ar r ayLi st ( i nt i ni t i al Capaci t y)

B�������������� &��� �$�Ar r ayLi st � ��������� ������������������ ����$��.��������������������������� ���������$�����Vect or ��������

�$(����������)�����*�����

i mpor t j ava. ut i l . * ;

Page 16 of 61

6/16/2006file://C:\Documents and Settings\cdot\Local Settings\Temp\~hhAFF6.htm

Page 17: Khalid Mughal Chapter 11

publ i c c l ass TakeAGuess { f i nal st at i c i nt NUM_DI GI TS = 5; publ i c st at i c voi d mai n( St r i ng[ ] ar gs) { / / Sani t y check on t he gi ven dat a. i f ( ar gs. l engt h ! = NUM_DI GI TS) { Syst em. er r . pr i nt l n( " Guess " + NUM_DI GI TS + " di gi t s . " ) ; r et ur n; } / * I ni t i al i ze t he sol ut i on l i s t . Thi s pr ogr am has a f i xed sol ut i on. * / Li st secr et Sol ut i on = new Ar r ayLi st ( ) ; / / ( 1) secr et Sol ut i on. add( " 5" ) ; secr et Sol ut i on. add( " 3" ) ; secr et Sol ut i on. add( " 2" ) ; secr et Sol ut i on. add( " 7" ) ; secr et Sol ut i on. add( " 2" ) ; / / Conver t t he user ' s guess f r om st r i ng ar r ay t o l i s t . ( 2) Li st guess = new Ar r ayLi st ( ) ; f or ( i nt i =0; i <NUM_DI GI TS; i ++) guess. add( ar gs[ i ] ) ; / / Fi nd t he number of di gi t s t hat wer e cor r ect l y i nc l uded. ( 3) Li st dupl i cat e = new Ar r ayLi st ( secr et Sol ut i on) ; i nt numI ncl uded = 0; f or ( i nt i =0; i <NUM_DI GI TS; i ++) i f ( dupl i cat e. r emove( guess. get ( i ) ) ) numI ncl uded++; / * Fi nd t he number of cor r ect l y pl aced di gi t s by compar i ng t he t wo l i s t s, el ement by el ement , count i ng each cor r ect pl acement . * / / / Need t wo i t er at or s t o t r aver se t hr ough guess and sol ut i on. ( 4) Li st I t er at or cor r ect = secr et Sol ut i on. l i s t I t er at or ( ) ; Li st I t er at or at t empt = guess. l i s t I t er at or ( ) ; i nt numPl aced = 0; whi l e ( cor r ect . hasNext ( ) ) i f ( cor r ect . next ( ) . equal s( at t empt . next ( ) ) ) numPl aced++; / / Pr i nt t he r esul t s. Syst em. out . pr i nt l n( numI ncl uded + " di gi t ( s) cor r ect l y i nc l uded. " ) ; Syst em. out . pr i nt l n( numPl aced + " di gi t ( s) cor r ect l y pl aced. " ) ; } }

(����������� ������� ������������� �������������%�

j ava TakeAGuess 3 2 2 2 7

��������������� ������� ��%�

4 di gi t ( s) cor r ect l y i nc l uded. 1 di gi t ( s) cor r ect l y pl aced.

62�� ���11�4������������������������� ������������������������������������������������������������*��������������������������������* �������������2�� �����������������������������&� �������������������� ���������������������St r i ng���-���������secr et Sol ut i on��������������������!1"����� � �����������������add( ) �������������������� ������������������������������� ������������� ����������&�

Page 17 of 61

6/16/2006file://C:\Documents and Settings\cdot\Local Settings\Temp\~hhAFF6.htm

Page 18: Khalid Mughal Chapter 11

�������guess&����!3"��

��������������������������������������$��������������������������!4"�������������������������� ���������������������������������������������������������� ������������������������������������������������ ��������������������������������������������guess�������.������������ ��������������2��������������������������������$�����get ( ) ������������r emove( ) ����������������t r ue���������� ������������ �����������&��������&�������������������������� �������������������������������� ������������������

/ / Fi nd t he number of di gi t s t hat wer e cor r ect l y i nc l uded. ( 3) Li st dupl i cat e = new Ar r ayLi st ( secr et Sol ut i on) ; i nt numI ncl uded = 0; f or ( i nt i =0; i <NUM_DI GI TS; i ++) i f ( dupl i cat e. r emove( guess. get ( i ) ) ) numI ncl uded++;

0���������������������������������������������$� �������������������$�������� ����������������&� ��������� �������������������� ���������������guess���������secr et Sol ut i on���������������� ����%�

/ / Need t wo i t er at or s t o t r aver se t hr ough guess and sol ut i on. ( 4) Li st I t er at or cor r ect = secr et Sol ut i on. l i s t I t er at or ( ) ; Li st I t er at or at t empt = guess. l i s t I t er at or ( ) ; i nt numPl aced = 0; whi l e ( cor r ect . hasNext ( ) ) i f ( cor r ect . next ( ) . equal s( at t empt . next ( ) ) ) numPl aced++;

���������� ����������� �

Review Questions

��' +������������������������������������������@�

,����������� ������������� �����

� ,����� ���������������������������$����� ����Unsuppor t edOper at i onExcept i on��

� ����������������� �������� ����������������������������������������������Unsuppor t edOper at i onExcept i on������������������������t hr ows���������

� .�Li st ������������ �����������������

� .��Ar r ayLi st ��������$�����������������2�����������������������

� ����Col l ect i on�����������������������������������get ��

��- +���� �������������������������� ����������� ��������������������� ���� ������@�

Page 18 of 61

6/16/2006file://C:\Documents and Settings\cdot\Local Settings\Temp\~hhAFF6.htm

Page 19: Khalid Mughal Chapter 11

i mpor t j ava. ut i l . * ; publ i c c l ass Set s { publ i c st at i c voi d mai n( St r i ng[ ] ar gs) { HashSet set 1 = new HashSet ( ) ; addRange( set 1, 1) ; Ar r ayLi st l i s t 1 = new Ar r ayLi st ( ) ; addRange( l i s t 1, 2) ; Tr eeSet set 2 = new Tr eeSet ( ) ; addRange( set 2, 3) ; Li nkedLi st l i s t 2 = new Li nkedLi st ( ) ; addRange( l i s t 2, 5) ; set 1. r emoveAl l ( l i s t 1) ; l i s t 1. addAl l ( set 2) ; l i s t 2. addAl l ( l i s t 1) ; set 1. r emoveAl l ( l i s t 2) ; Syst em. out . pr i nt l n( set 1) ; } s t at i c voi d addRange( Col l ect i on col , i nt st ep) { f or ( i nt i = st ep* 2; i <=25; i +=st ep) col . add( new I nt eger ( i ) ) ; } }

,������������������������� ����

� ���� ������� ��������������� ����������� ������������� ����������������� �������������������� ��������������

� ���� ������� ��������������� ��������������Tr eeSet ����������$�set 2����������������������Compar at or �������� ��������������������������

� ���� ������� ������� ���� ������������&����� �������� ����Unsuppor t edOper at i onExcept i on� ���������

� ���� ������� ������� ���� ����������������� ���� ��������� ���������� �3?� ���������

� ���� ������� ������� ���� ����������������� ���� ������������������������������������ ���������

��. +�����������������������������������������Col l ect i on����������@�

,��������������������������� �����

� add( Obj ect o) �

� r et ai nAl l ( Col l ect i on c) �

� get ( i nt i ndex) �

Page 19 of 61

6/16/2006file://C:\Documents and Settings\cdot\Local Settings\Temp\~hhAFF6.htm

Page 20: Khalid Mughal Chapter 11

� i t er at or ( ) �

� i ndexOf ( Obj ect o) �

��/ +���� �������������� ����������������� ���� ������@�

i mpor t j ava. ut i l . * ; publ i c c l ass I t er at e { publ i c st at i c voi d mai n( St r i ng[ ] ar gs) { Li st l = new Ar r ayLi st ( ) ; l . add( " A" ) ; l . add( " B" ) ; l . add( " C" ) ; l . add( " D" ) ; l . add( " E" ) ; Li st I t er at or i = l . l i s t I t er at or ( ) ; i . next ( ) ; i . next ( ) ; i . next ( ) ; i . next ( ) ; i . r emove( ) ; i . pr evi ous( ) ; i . pr evi ous( ) ; i . r emove( ) ; Syst em. out . pr i nt l n( l ) ; } ; } ;

,������������������������� ����

� #�� ���� �����[ A, B, C, D, E] . �

� #�� ���� �����[ A, C, E] . �

� #�� ���� �����[ B, D, E] . �

� #�� ���� �����[ A, B, D] . �

� #�� ���� �����[ B, C, E] . �

� #�� ���� ��������� ���NoSuchEl ement Except i on��

��0 +�������������������������������Col l ect i on����������� ���������������������t r ue������������������� ������������������������ �������@�

,����������� ������������� �����

� cont ai ns( ) �

� add( ) �

� cont ai nsAl l ( ) �

� r et ai nAl l ( ) �

� cl ear ( ) �

���������� ����������� �

Page 20 of 61

6/16/2006file://C:\Documents and Settings\cdot\Local Settings\Temp\~hhAFF6.htm

Page 21: Khalid Mughal Chapter 11

11.5 Maps

.�Map����������� ������������$�����������������G����������H� �����������������������.��� �������������� ��� ���������$�&���������� ����&�������$��������������6������$��� ��������������������������&��� ���������� ������������������������������� ������&������������������������������������ ������$��������������0����2�� ��&�������������*�������� &���������!�����"��������� ������������$����������!��$�"&�������������������������$������������

����������$���������������������������-���������������������� ������������������������ �� ��������������� ������� �� �����-����&�������$����������� ���������� ��

.��� �����������������������������Map���������������������2���������Col l ect i on������������8� ����&������� ��������������� ������������������������������ �$�%�����$����&�������������������&�����������$�������������������������� �������������$������������������������� ��

����Map������������ �������������� ����������������#� ������������������������ ����Unsuppor t edOper at i onExcept i on�������$���������� ������������� ��������������� ������������������ �����������j ava. ut i l � ��������� ������������� �������� ����������������Map�����������!����������11�3�����0������11�4"��

Basic Operations

������� ������������������������������������������$� ���������$����� ��

#�����������Gkey&�val ueH�����$������������ ��#�������������������� ��������$������������ ��������� ����������$&������$��<���� ���&����������������nul l ��������

Obj ect get ( Obj ect key)

(�������������������� ���������� ����������$������ ��&����nul l �����������$�����������

����r emove( ) ������������������������$���������� ����������$��#�������������������� ��������$������������ ��������� ����������$&������$��<���� ���&����������������nul l ��������

bool ean cont ai nsKey( Obj ect key)

(�������t r ue��������� ����������$������ ����������������������� ��

bool ean cont ai nsVal ue( Obj ect val ue)

(�������t r ue�����������2�������������������$������������� ����������� ���������������

i nt s i ze( ) bool ean i sEmpt y( )

�������������������������������������������!����&��������������������$����������� "�����

Obj ect put ( Obj ect key, Obj ect val ue) � < �������

Obj ect r emove( Obj ect key) � < �������

Page 21 of 61

6/16/2006file://C:\Documents and Settings\cdot\Local Settings\Temp\~hhAFF6.htm

Page 22: Khalid Mughal Chapter 11

������������� ������ �$���������

Bulk Operations

������������������� �������������������������� ���������� ������������������ &������������������������������������������������������������������ ��

Collection Views

Set keySet ( ) Col l ect i on val ues( ) Set ent r ySet ( )

�������������� �������������������� ��������� ��B���������������� ������������������������� &����������������������������������������������� ������$�&����������������� ������������������������ ����G����������H��������&���� �������$��5�������������Col l ect i on�����������$�����val ues( ) �����������������Set &��������������$�������� ������������������&��������&��� ����������������������������������������������������������6����G����������H������������$�������� ������ ����������$������-������ ���������������������Map. Ent r y ������������.������$������������$�������� ������������ ��������$����������������������������������&� �������������*�2 �������$%�

i nt er f ace Ent r y { Obj ect get Key( ) ; Obj ect get Val ue( ) ; Obj ect set Val ue( Obj ect val ue) ; }

�HashMap, LinkedHashMap, and Hashtable

0������11�4���� ��������� ��������������������Map������������������j ava. ut i l � ������%�HashMap&�Li nkedHashMap&�Tr eeMap&�����Hasht abl e��

������������HashMap�����Hasht abl e��� ������������������� �������������Li nkedHashMap��� ������������������ �&� ����������������������� ������������Tr eeMap��� ����������������� ��!����,�������11�A&� ��;?3"��

+���������HashMap��������������������*��������� �����������nul l ���$&�����Hasht abl e����������������*��������� ����������*nul l ���$����������������$������������*�����$�����Hasht abl e������� �������������� ����������� �����$��������*�������������� ���������� ���������$��������������������Col l ect i ons�������!����,�������11�I&� ��;I1"�����������Vect or ������&�����Hasht abl e����������������������$�������������������������������������� �����������Map������������

voi d put Al l ( Map t ) � < �������

voi d c l ear ( ) � < �������

Page 22 of 61

6/16/2006file://C:\Documents and Settings\cdot\Local Settings\Temp\~hhAFF6.htm

Page 23: Khalid Mughal Chapter 11

�������� ��� �����������������������������������������������< ���������������� ���������$��������hashCode( ) �����equal s( ) ������������������$���-�����!����,�������11�>&� ��;A1"��

����Li nkedHashMap��� ���������������������������������HashMap����������������������� ���� ���������� ���������Li nkedHashMap�����HashMap�������������������������������� ���� ����������������� ����������������Li nkedHashSet �����HashSet ��6�������������HashMap�!������HashSet "����������������������������������Li nkedHashMap�!������Li nkedHashSet "��������������$��������&������������������Li nkedHashMap�����������������������&��������&�������������� �����������$������������������������ ������������������������������������$������*��������&�������������� �����$���������������������$J������$�������$��2������������������������������������Li nkedHashSet ��������!�������"������������������8� ����&���Li nkedHashMap�����������������������������������!�������"���������&��������&�������������� �����������������������������&�����������*�������$�����������������*�������$�������������������������������������������� ������������������������������������������Li nkedHashMap��������

��������HashMap�����Li nkedHashMap��������� ���������� ������� ����������&���������HashMap����������������������������������������������������������< �����������������������&���������&�������������������$��������������$������������������&���������������������$��< ���������������������������������$� ������ ����������������������������������&�������������������������������������������������

.�����&���������&��������������������������Li nkedHashMap���������������$���� �������������HashMap&��������������������$*��������������������������������������������������� ���������������������������������*��� ���0������������$����Li nkedHashMap&����������������������� �� ������������������)����������� F�������������������� ����$��8� ����&��������������$����HashMap&������� �� ������������������ ����$���������� ��

��������������� ����������������������t oSt r i ng( ) ������������������������2������� �����������������������$�����t oSt r i ng( ) ���������������� ����

�{���

�=�����

�, ����

�=�����

�, . . . , ����

�=�����

�} �

�����������������������������

�����������2������� �����������������������$�����t oSt r i ng( ) �

���������������������������$�������������-�������������� &���� �������$��

.�� ������������ ���������������&��� �������������������� ����������������������������������������������� ��� �$��� &������������������������������������ ��� ��������������2������������������������������������������ ��� �$��� � ����������������� ����$�������������������������HashMap������� ����������������� ����������������%�

HashMap( ) HashMap( i nt i ni t i al Capaci t y) HashMap( i nt i ni t i al Capaci t y, f l oat l oadFact or )

B�������������� &��� �$�HashMap&��������������� ����������������������������� ����$������������������

HashMap( Map ot her Map)

B�������������� ��� ��������������������������������� ���������� ��

Page 23 of 61

6/16/2006file://C:\Documents and Settings\cdot\Local Settings\Temp\~hhAFF6.htm

Page 24: Khalid Mughal Chapter 11

�����Li nkedHashMap�����Hasht abl e����������������������������������������������������������������������HashMap��������#����������&�����Li nkedHashMap������� ���������������������� ������������������������������������ �������%�

Li nkedHashMap( i nt i ni t i al Capaci t y, f l oat l oadFact or , bool ean accessOr der )

B�������������� &��� �$�Li nkedHashMap� ��������� ������������������ ����$&������ �������������������&���������� ��������������������������������������������t r ue������������������f al se����������������������

�$(��������'�)����������

i mpor t j ava. ut i l . * ; publ i c c l ass Wei ght Gr oups { publ i c st at i c voi d mai n( St r i ng[ ] ar gs) { / / Cr eat e a map t o st or e t he f r equency f or each gr oup. Map gr oupFr eqDat a = new HashMap( ) ; i nt numAr gs = ar gs. l engt h; f or ( i nt i =0; i <numAr gs; i ++) { / / Get t he val ue f r om an ar gument and gr oup i nt o i nt er val s of 5. ( 1) doubl e wei ght = Doubl e. par seDoubl e( ar gs[ i ] ) ; I nt eger wei ght Gr oup = new I nt eger ( ( i nt ) Mat h. r ound( wei ght / 5) * 5) ; / / I ncr ement count , set t o 1 i f i t ' s t he f i r s t val ue of gr oup. ( 2) I nt eger ol dCount = ( I nt eger ) gr oupFr eqDat a. get ( wei ght Gr oup) ; I nt eger newCount = ( ol dCount ==nul l ) ? new I nt eger ( 1) : new I nt eger ( ol dCount . i nt Val ue( ) +1) ; gr oupFr eqDat a. put ( wei ght Gr oup, newCount ) ; / / ( 3) } / * Pr i nt by t r aver si ng a sor t ed l i s t of gr oups ( keys) , and ext r act i ng count ( val ues) f r om t he gr oupFr eqDat a map. * / / * Cr eat e a l i s t of gr oups ( keys) , and use t he sor t al gor i t hm f r om t he Col l ect i ons c l ass t o sor t t he keys. * / Li st keys = new Ar r ayLi st ( gr oupFr eqDat a. keySet ( ) ) ; / / ( 4) Col l ect i ons. sor t ( keys) ; / / ( 5) / * Cr eat e an i t er at or on t he sor t ed keys. Tr aver se t he keys, l ooki ng up t he f r equency f r om t he f r equency map. * / Li st I t er at or keyI t er at or = keys. l i s t I t er at or ( ) ; / / ( 6) whi l e ( keyI t er at or . hasNext ( ) ) { / / Cur r ent key ( gr oup) . ( 7) I nt eger gr oup = ( I nt eger ) keyI t er at or . next ( ) ; / / Ext r act count ( val ue) f r om t he map. I nt eger count = ( I nt eger ) gr oupFr eqDat a. get ( gr oup) ; / / ( 8) i nt i nt Count = count . i nt Val ue( ) ; / * Use t he f i l l ( ) met hod f r om t he Ar r ays c l ass t o cr eat e a s t r i ng consi st i ng of i nt Count number of ' * ' . * / char [ ] bar = new char [ i nt Count ] ; Ar r ays. f i l l ( bar , ' * ' ) ; / / ( 9)

Page 24 of 61

6/16/2006file://C:\Documents and Settings\cdot\Local Settings\Temp\~hhAFF6.htm

Page 25: Khalid Mughal Chapter 11

Syst em. out . pr i nt l n( gr oup + " : \ t " + new St r i ng( bar ) ) ; } / / end whi l e } / / end mai n( ) } / / end of c l ass

(����������� ������� ������������� �������������%�

>j ava Wei ght Gr oups 74 75 93 75 93 82 61 92 10 185

��������������� ������� ��%�

10: * 60: * 75: * * * 80: * 90: * 95: * * 185: *

62�� ���11�;� ����������2�������������������������������$���� ������������������������ ���������� &� ������� ���������� ���������������������������������������������� ������������������������� �������� �����������������������2�� ���������������������������� �&���������������$���� �&������������������������������������������������ ������ ������� ���������������� �%�

� #������������ ����������������&����������������� ������������������� ������� ���������� ������ �������������������$�������� ���������� %�

���� ���������� ������������������!1"��������������������������&������������$&������� �����!3"&���������������������������� &������� �����!4"��,�������$�������������������� &���$� ������������$�������� ��������

/ / I ncr ement count , set t o 1 i f i t ' s t he f i r s t val ue of gr oup. ( 2) I nt eger ol dCount = ( I nt eger ) gr oupFr eqDat a. get ( wei ght Gr oup) ; I nt eger newCount = ( ol dCount ==nul l ) ? new I nt eger ( 1) : new I nt eger ( ol dCount . i nt Val ue( ) +1) ; gr oupFr eqDat a. put ( wei ght Gr oup, newCount ) ; / / ( 3)

� #����������������������$��! ��������� ���������� �"����������gr oupFr eqDat a��� ���������������������keySet ( ) ������������������������� ������$�&� ���������������������������&������� �����!;"��������$�����������������$���������������sor t ( ) ����������Col l ect i ons������&������� �����!?"��

Li st keys = new Ar r ayLi st ( gr oupFr eqDat a. keySet ( ) ) ; / / ( 4) Col l ect i ons. sor t ( keys) ; / / ( 5)

� #�������������������������������������$�&���������� �������������$��������gr oupFr eqDat a��� ��.��� ��������$������������������������������������ ���

��������������������������������!A"��

Li st I t er at or keyI t er at or = keys. l i s t I t er at or ( ) ; / / ( 6)

0����������$&����������� �������������!����&���������$������"�������������&������� �����!>"�

Page 25 of 61

6/16/2006file://C:\Documents and Settings\cdot\Local Settings\Temp\~hhAFF6.htm

Page 26: Khalid Mughal Chapter 11

����!I"��

/ / Cur r ent key ( gr oup) . ( 7) I nt eger gr oup = ( I nt eger ) keyI t er at or . next ( ) ; / / Ext r act count ( val ue) f r om t he map. I nt eger count = ( I nt eger ) gr oupFr eqDat a. get ( gr oup) ; / / ( 8)

.���������������������$����������������������f i l l ( ) �����������������Ar r ays������&������� �����!K"��

���������� ����������� �

11.6 Sorted Sets and Sorted Maps

,���������� �������� ����������������&��������Sor t edSet �����Sor t edMap&������� �������������������������������������������� �������������!����0�������11�3�����11�4"��<�-���������� ����$�������������������$��� ��������������Compar abl e����������&�����������������������������$������ �������-����������� ������������Compar at or ������������

+�J���������������������� �������������Compar abl e�����Compar at or&������������������������������������� ���

The Comparator Interface

/��������������������������������������������$�������������������)������ �������������� �������� ���������������������������������������.������ ���������� �����������Compar at or ����������&� ������������������ �����������������%�

i nt compar e( Obj ect o1, Obj ect o2)

����compar e( ) ����������������������������������&�)���&������ �������������������������������-����������������&���������&������������������������������-���&�������������������������������,����������������������������������$&��������������$������������������������ �����������������������������������������������������equal s( ) ��������!����,�������11�>"��

The Comparable Interface

.������������������������������������������������������$��� ��������������Compar abl e���������������$��������������������������������L����./#&������������� �� ����������&�St r i ng&�Dat e&�����Fi l e&��� ���������������������������j ava. l ang. Compar abl e������������ �����������������������%�

i nt compar eTo( Obj ect o)

��������������������������������������&�)���&������ ���������������������������������-����������������&���������&���������������������� ����������-���&�����������������������������#������ ����Cl assCast Except i on������������������������ ���������������������������������������������$ �������������������-�����,����������������������������������$&������������������������������ �����������������������������������������������������equal s( ) ��������!����,�������11�>"��

Page 26 of 61

6/16/2006file://C:\Documents and Settings\cdot\Local Settings\Temp\~hhAFF6.htm

Page 27: Khalid Mughal Chapter 11

�<�-������� ����������������������������������������

� �������������������������

� ��$���������������� �

� �����������������������������������������$�����������Col l ect i ons. sor t ( ) ��������

5�������������������������������St r i ng���-�����!����Char act er ���-����"������2������ ������������!����,�������1=�?&� ��;13"��,������� ���������2������ ������$������������������������������������������������$���������������� �����������������������������.�������������������������������$��������������� ���������������������2������ �������������

������������������������-�������������������� �� ����$ ������������������������������������������������ ����������������� ����������$ ��!����,�������1=�4&� ��4KA"��.��������������������������&���������$���������������� &�������-����� ���������������������������������������

.������������������������������������������������������������ ���������$� ���������Compar at or �������������������� ������������������������ �����������������Col l ect i ons�����Ar r ays��������� �������������$��������������������&� �����������������Compar at or �!����,�������11�I&� ��;I3"��

62�� ���11�?�������������������������������������� ������������������������� �������������������� �$����������������������Tr eeSet �������!�������������������������������"��6���� ����������������������������������������������������� ����!3"��.���2������� �������������������������������������� ����������!4"��������� ������ ����������������������� ��������������������������������������������������������������������������������������������������

����St r i ng��������� ������������Compar abl e����������&� �������������� �������������������compar eTo( ) �������������compar eTo( ) ���������������������������������������������&� ����������2������ ������������������������������������������������� ��������������������������2������ ������$� ����������������������!1�"����������#�� �� ���������������������������������������������������&� ���������� ����������������)������ ��������

����St r i ng������� �����������������������!CASE_I NSENSI TI VE_ORDER"������������������� ���������-���� ������compar e( ) ������������������������������ ������� ����������������2������ ������$������� ���������������������������������������������� ������������������������ ����������������������!1�"����������������� ���������� ������������������������������������������������� ������ ���� ������������������������������������������������$�����������������&� ��������������*�������������������

+������������������������� ������������������������������������������������#����$����������&�� ����������������� ������$��2�������������������� �������������������������� ���������������� ���������&���������� ������������������������������ ���������0�������������������������������� ��������������� ����&�������������������������������� �������&������������0����2�� ��&������������ ����������" r epor t " �����" cour t "&����������� �����������������������������������������������B�������������� ������������ ���������&���������������' o' ������������������������������������������������' u' �����������������������.������������������$����������&������������" r epor t " �������������������������" cour t " ��

B�� ������� �����������������������������$��������������������������������������������������������� �����������������������������2������ ������$��#�� ��������������� ���������&�" r epor t " �����" cour t "&���������������������" t r oper " ������2������ ������$�������������������������������" t r uoc" ��

Page 27 of 61

6/16/2006file://C:\Documents and Settings\cdot\Local Settings\Temp\~hhAFF6.htm

Page 28: Khalid Mughal Chapter 11

.���$�������������� ������������ ����������$�����Rhymi ngSt r i ngCompar at or ����������62�� ���11�?������compar e( ) �����������!;"������������������������������������������������ ��������������������.��������������������������������������������������������������&� ����������������������������������������������������������&������� �����!?"������compar e( ) �����������������������compar eTo( ) �����������!A"������� ������������������������&����������2������ ������������������������������������������������������������$������������������������������������������ ���������������������������������������������� ������������������������ ����������������������!1�"����������������� ���������������� ������������������������������������������������� ������ ���� ������������������������������������������������$�����������������&� ����������$������������

$(��������-�1�������#��������� ����#�����

i mpor t j ava. ut i l . * ; publ i c c l ass Compar at or Usage { publ i c st at i c voi d mai n( St r i ng[ ] ar gs) { / / Choi ce of compar at or . / / Set st r Set = new Tr eeSet ( ) ; / / ( 1a) / / Set st r Set = new Tr eeSet ( St r i ng. CASE_I NSENSI TI VE_ORDER) ; / / ( 1b) Set st r Set = new Tr eeSet ( new Rhymi ngSt r i ngCompar at or ( ) ) ; / / ( 1c) / / Add each command l i ne ar gument t o t he set . f or ( i nt i =0; i < ar gs. l engt h; i ++) { / / ( 2) s t r Set . add( ar gs[ i ] ) ; } Syst em. out . pr i nt l n( st r Set ) ; / / ( 3) } } c l ass Rhymi ngSt r i ngCompar at or i mpl ement s Compar at or { publ i c i nt compar e( Obj ect obj 1, Obj ect obj 2) { / / ( 4) / / ( 5) Cr eat e r ever sed ver s i ons of t he st r i ngs. St r i ng r ever seSt r 1 = new St r i ngBuf f er ( ( St r i ng) obj 1) . r ever se( ) . t oSt r i ng( ) ; St r i ng r ever seSt r 2 = new St r i ngBuf f er ( ( St r i ng) obj 2) . r ever se( ) . t oSt r i ng( ) ; / / Compar e t he r ever sed st r i ngs l exi cogr aphi cal l y . r et ur n r ever seSt r 1. compar eTo( r ever seSt r 2) ; / / ( 6) } }

���� �������������� ������������� ���� ������������������������������������%�

>j ava Compar at or Usage cour t St uar t r epor t Resor t assor t suppor t t r anspor t di st or t

<�� ������������ �������������������������������!1�"%�

[ Resor t , St uar t , assor t , cour t , di st or t , r epor t , suppor t , t r anspor t ]

<�� ������������ ����������������������������������������!1�"%�

[ assor t , cour t , di st or t , r epor t , Resor t , St uar t , suppor t , t r anspor t ]

<�� ������������ �������������������$�����������!1�"%�

Page 28 of 61

6/16/2006file://C:\Documents and Settings\cdot\Local Settings\Temp\~hhAFF6.htm

Page 29: Khalid Mughal Chapter 11

[ St uar t , r epor t , suppor t , t r anspor t , Resor t , assor t , di st or t , cour t ]

The SortedSet Interface

����Sor t edSet ������������2����������Set �������������� �����������������������$���������������������������

/ / Range- vi ew oper at i ons Sor t edSet headSet ( Obj ect t oEl ement ) Sor t edSet t ai l Set ( Obj ect f r omEl ement ) Sor t edSet subSet ( Obj ect f r omEl ement , Obj ect t oEl ement )

����headSet ( ) ��������������������� ������ �������������������������&� �������������������������$���������������� �����������������,�������$&�����t ai l Set ( ) ��������������������� �������� �������������������������&� ������������������������������������������������ ���������������������subSet ( ) ��������������������� �������� �������������������������&� �������������������������f r omEl ement &����������&����t oEl ement &��2���������5���������������� �� ����������������������������������������������������������$������������� ��

/ / Fi r st - l ast el ement s Obj ect f i r s t ( ) Obj ect l ast ( )

����f i r s t ( ) ������������������������������������������$�������������������&���������l ast ( ) �����������������������������������������$����������������������������� ���NoSuchEl ement Except i on������������������������ �$��

/ / Compar at or access Compar at or compar at or ( )

��������������������������� ������������������ �������������������&����nul l ���������������������������������������������������������� ������&�����������&����������$��������� �������������������������������&��������������� ������ $�������������������� ��������������

The SortedMap Interface

����Sor t edMap������������2����������Map�������������� �����������������������$������� ������������ �� ��������������#���� ���������������������������������������Sor t edSet ����������&�� ���������� ��������$������������������������������������

/ / Range- vi ew oper at i ons Sor t edMap headMap( Obj ect t oKey) Sor t edMap t ai l Map( Obj ect f r omKey) Sor t edMap subMap( Obj ect f r omKey, Obj ect t oKey) / / Fi r st - l ast keys Obj ect f i r s t Key( ) Obj ect l ast Key( ) / / Compar at or access Compar at or compar at or ( )

Page 29 of 61

6/16/2006file://C:\Documents and Settings\cdot\Local Settings\Temp\~hhAFF6.htm

Page 30: Khalid Mughal Chapter 11

�TreeSet and TreeMap

����Tr eeSet �����Tr eeMap����������� �����������Sor t edSet �����Sor t edMap�����������&���� �������$��$��������&�� ����������������������������� �����$����������������������������������������������$�&���� �������$��8� ����&�������������������������� ���������$� ����������������)������ ���������������������������

�������� �������������������������������&� ��������������2�������� �������������������� ����������,��������������HashSet ����HashMap�������������������������Tr eeSet ����Tr eeMap&�����������������������������$�������������� ����������������������������������������������������������

6���������� �������������������������%�

Tr eeSet ( ) Tr eeMap( )

.������������������������������������ ��� �$����������������� &���������������������������������������������������������$�&���� �������$��

Tr eeSet ( Compar at or c) Tr eeMap( Compar at or c)

.����������������������������2 ��������� ������������������������������������������$���

Tr eeSet ( Col l ect i on c) Tr eeMap( Map m)

.�������������������������������������������������������� ������������������������������ &���������������������������������������������������������$�&���� �������$��

Tr eeSet ( Sor t edSet s) Tr eeMap( Sor t edMap m)

.������������������������������ ���������� ������������������������������������������������� ������������������������������� &� �����������������������

�$(��������.�)����� ���������

i mpor t j ava. ut i l . * ; publ i c c l ass Wei ght Gr oups2 { publ i c st at i c voi d mai n( St r i ng[ ] ar gs) { / / Cr eat e a map t o st or e t he f r equency f or each gr oup. Map gr oupFr eqDat a = new HashMap( ) ; i nt numAr gs = ar gs. l engt h; f or ( i nt i =0; i <numAr gs; i ++) { / / Get t he val ue f r om ar gument and gr oup i nt o i nt er val s of 5 ( 1) doubl e wei ght = Doubl e. par seDoubl e( ar gs[ i ] ) ; I nt eger wei ght Gr oup = new I nt eger ( ( i nt ) Mat h. r ound( wei ght / 5) * 5) ;

Page 30 of 61

6/16/2006file://C:\Documents and Settings\cdot\Local Settings\Temp\~hhAFF6.htm

Page 31: Khalid Mughal Chapter 11

/ / I ncr ement count , set t o 1 i f i t ' s t he f i r s t val ue of gr oup. ( 2) I nt eger ol dCount = ( I nt eger ) gr oupFr eqDat a. get ( wei ght Gr oup) ; I nt eger newCount = ( ol dCount ==nul l ) ? new I nt eger ( 1) : new I nt eger ( ol dCount . i nt Val ue( ) +1) ; gr oupFr eqDat a. put ( wei ght Gr oup, newCount ) ; / / ( 3) } / * Onl y t he hi st ogr am f or t he wei ght gr oups bet ween 50 and 150 i s of i nt er est . Pr i nt f r equency f or t hese gr oups i n a sor t ed or der . * / / / Tr ansf er t he dat a t o a sor t ed map. Sor t edMap sor t edGr oupFr eqDat a = new Tr eeMap( gr oupFr eqDat a) ; / / ( 4) / / Sel ect t he r el evant sub- map. Sor t edMap sel ect edGr oupFr eqDat a = / / ( 5) sor t edGr oupFr eqDat a. subMap( new I nt eger ( 50) , new I nt eger ( 150) ) ; / * * Pr i nt by t r aver si ng t he sor t ed ent r i es of wei ght gr oups ( key) and count ( val ue) . * / I t er at or ent r yI t er at or = sel ect edGr oupFr eqDat a. ent r ySet ( ) . i t er at or ( ) ; / / ( 6) whi l e ( ent r yI t er at or . hasNext ( ) ) { Map. Ent r y ent r y = ( Map. Ent r y) ent r yI t er at or . next ( ) ; / / ( 7) / / Ext r act gr oups ( key) and count ( val ue) f r om ent r y. ( 8) I nt eger gr oup = ( I nt eger ) ent r y. get Key( ) ; I nt eger count = ( I nt eger ) ent r y. get Val ue( ) ; i nt i nt Count = count . i nt Val ue( ) ; / * Use t he f i l l ( ) met hod f r om t he Ar r ays c l ass t o cr eat e a s t r i ng consi st i ng of i nt Count number of ' * ' . * / char [ ] bar = new char [ i nt Count ] ; Ar r ays. f i l l ( bar , ' * ' ) ; / / ( 9) Syst em. out . pr i nt l n( gr oup + " : \ t " + new St r i ng( bar ) ) ; } / / end whi l e } / / end mai n( ) } / / end of c l ass

(����������� ������� ������������� ������������%�

j ava Wei ght Gr oups2 74 75 93 75 93 82 61 92 10 185

��������������� ������� ��%�

60: * 75: * * * 80: * 90: * 95: * *

62�� ���11�A���������������������� ���#������� ����������2�������������������������������62�� ���11�;&������� ���������������������������������������� ���������� ������� ������������������������ ������� �%�

� (�������� ����������������&����������������� ������������������� ������� ���������� ������ �������������������$�������� ���������� ���������������������� �������62�� ���11�;��

Page 31 of 61

6/16/2006file://C:\Documents and Settings\cdot\Local Settings\Temp\~hhAFF6.htm

Page 32: Khalid Mughal Chapter 11

� �������������������������������� &������� �����!;"��

Sor t edMap sor t edGr oupFr eqDat a = new Tr eeMap( gr oupFr eqDat a) ; / / ( 4)

� B�������������������� ���� �������������������������� ���������� �&������� �����!?"��

Sor t edMap sel ect edGr oupFr eqDat a = / / ( 5) sor t edGr oupFr eqDat a. subMap( new I nt eger ( 50) , new I nt eger ( 150) ) ;

' ,���� ������������������������� ������������������������ &������� �����!A"��0����&���������� �������������������������������������ent r ySet ( ) ����������������������!����&������������"���������������� � �������������&�����������������$��������� ������������.����������������������������������������

I t er at or ent r yI t er at or = sel ect edGr oupFr eqDat a. ent r ySet ( ) . i t er at or ( ) ; / / ( 6)

- '��������������������������������������$���������6����������������������������������$��������������$���������������� ��6��������$�����������������Map. Ent r y����������&� ��������� ��������$����������������������2�������&������� �����!I"��

/ / Ext r act gr oups ( key) and count ( val ue) f r om ent r y. ( 8) I nt eger gr oup = ( I nt eger ) ent r y. get Key( ) ; I nt eger count = ( I nt eger ) ent r y. get Val ue( ) ; i nt i nt Count = count . i nt Val ue( ) ;

���������� ����������� �

Review Questions

��2 +�����������������������������������������-������� ��������������Map����������@�

,����������� ������������� �����

� cont ai ns( Obj ect o) �

� addAl l ( Col l ect i on c) �

� r emove( Obj ect o) �

� val ues( ) �

Page 32 of 61

6/16/2006file://C:\Documents and Settings\cdot\Local Settings\Temp\~hhAFF6.htm

Page 33: Khalid Mughal Chapter 11

� t oAr r ay( ) �

���3 +��������������������������������� �@�

,����������� ������������� �����

� ������������$ ���������val ues( ) �����������Set ��

� B�������������������������� �����������$�keySet ( ) � ����������������������������������� ��

� ����Map������������2����������Col l ect i on������������

� .�����$��������� �������������

� .���Map��� ���������������� �������$����������

���� +������������������������ ������������� ���� ������� ����@�

i mpor t j ava. ut i l . * ; publ i c c l ass Li st s { publ i c st at i c voi d mai n( St r i ng[ ] ar gs) { Li st l i s t = new Ar r ayLi st ( ) ; l i s t . add( " 1" ) ; l i s t . add( " 2" ) ; l i s t . add( 1, " 3" ) ; Li st l i s t 2 = new Li nkedLi st ( l i s t ) ; l i s t . addAl l ( l i s t 2) ; l i s t 2 = l i s t . subLi st ( 2, 5) ; l i s t 2. c l ear ( ) ; Syst em. out . pr i nt l n( l i s t ) ; } }

,������������������������� ����

� [ 1, 3, 2] �

� [ 1, 3, 3, 2] �

� [ 1, 3, 2, 1, 3, 2] �

� [ 3, 1, 2] �

� [ 3, 1, 1, 2] �

� 5������������������

���� +�����������������������������compar at or ( ) �������@�

,����������� ������������� �����

Page 33 of 61

6/16/2006file://C:\Documents and Settings\cdot\Local Settings\Temp\~hhAFF6.htm

Page 34: Khalid Mughal Chapter 11

� Ar r ayLi st �

� HashMap�

� Tr eeSet �

� HashSet �

� Tr eeMap�

���� +������������ �����$ ��������������������������������j ava. ut i l . Map. Ent r y@�

,����������� ������������� �����

� Obj ect get Key( ) �

� Obj ect set Key( Obj ect val ue) �

� voi d r emove( ) �

� Obj ect get Val ue( ) �

� voi d set Val ue( Obj ect val ue) �

���������� ����������� �

11.7 Implementing the equals(), hashCode(), and compareTo() Methods

������-����$�����������*f i nal ����������������Obj ect ��������������������������������������$� �������������������������������-����&� ������������������������������������������������������

#������� ����������������������� ����� �$�����������������������������equal s( ) �����hashCode( ) ����������#� �������������������compar eTo( ) ���������������Compar abl e��������������������$����������������������� �����������

<�-����������������������������������equal s( ) �������������������������������������������������#�����$��������������hashCode( ) �������&����$�����������������������������������HashSet ����������$�������HashMap��#� ��������������Compar abl e��������������� ����������������������������������������������������������������� ���������11�3��������)����������������������-������������ ����������������-����������������������������������������������� ���

.�������������2�� ��&� �� ������ ������������������������������������������������������.����������������!:5<"����������� ���� ���������� ������������ ��������������������%�

� �����������������

Page 34 of 61

6/16/2006file://C:\Documents and Settings\cdot\Local Settings\Temp\~hhAFF6.htm

Page 35: Khalid Mughal Chapter 11

� ������������������

� �� ������������

����������������������������������� ������$��������(�������������� �������������������$��������������&�������������������$����������� �������������������+��������$������������������������������������������������������������������������������������������������������������&��������� ����������������������������������������������������������������������� ��������������� ��$��� �������������������������������������������$��

The equals() Method

#������$���-������������������������������&������������������������$�����������������equal s( ) ���������������Obj ect ���������������������� ����������-���������������������$��#���� ����������������������������������������������������� �������������-������6��������������������������������$������������������

����������Si mpl eVNO����62�� ���11�>�����������������������equal s( ) ���������������Obj ect ��������#�����$���������������t oSt r i ng( ) �����������������������������������2������� ����������������������������������

$(��������/�4� ������������� ��5���� ��1������

publ i c c l ass Si mpl eVNO { / / Does not over r i de equal s( ) or hashCode( ) . pr i vat e i nt r el ease; pr i vat e i nt r evi s i on; pr i vat e i nt pat ch; publ i c Si mpl eVNO( i nt r el ease, i nt r ev i s i on, i nt pat ch) { t hi s. r el ease = r el ease; t hi s. r evi s i on = r evi s i on; t hi s. pat ch = pat ch; } publ i c St r i ng t oSt r i ng( ) { r et ur n " ( " + r el ease + " . " + r evi s i on + " . " + pat ch + " ) " ; } }

����������Test Ver si onSi mpl e����62�� ���11�I��������-������������������Si mpl eVNO��#��������������������������������������������������������Si mpl eVNO���-����&������� �����!1"&�!3"&�����!4"&���� �������$��#����������������������$����Si mpl eVNO���-����&��������ver si ons&������� �����!;"�������$ ���������������������-����� ������������������������������2�� ���&���� �������������$������� ��� ������������������������������������������.�����������2�� ���� ������������t est ( ) �������������������62�� ���11�I��

+����������������������������������downl oads�����$������������!?"&��� ����������������������� ������������������� ���������������������������� ������� ���������������ver si ons�����$��

62�� ���11�I�����������������������Si mpl eVNO���-���������������&�������������������Si mpl eVNO�����������������������equal s( ) ����������� ���������$�����������������������������������-����

Page 35 of 61

6/16/2006file://C:\Documents and Settings\cdot\Local Settings\Temp\~hhAFF6.htm

Page 36: Khalid Mughal Chapter 11

���������$���������������l at est ������� ����� ����������-�������������$���������������i nShops����� ����������-�������������$���������������ol der&������� �����!A"&�!>"&�!I"&�����!K"��������� ������������ ���������� ���������������������f al se�������-���������������������$���������������-�����������������$�������������������������������������-����&����������������-�������������$���������������l at est �����������-�������������$���������������i nShops������������������������

5������������������equal s( ) ��������� �� ������$������������ ����������������������Si mpl eVNO���-������������$�&������������&������ ���,������������������� ����$�������� $���-���&��������������������&� ��������������� ����� ������-������������������������,���������Si mpl eVNO���-�����������������&�����equal s( ) �������� ������ �$���������f al se&��������������� �������-���������� ����� �����������������$���-�����.����� ���$�������� ��������62�� ���11�I&����������������������������������( 9. 1. 1) ��������ver si ons�����$� ������ �$��������

����ver si ons�����$�������������������Li st ����!1;"&����������$���������������vnoLi st &���������cont ai ns( ) ���������������������!1?"��������������������������$���������������������cont ai ns( ) �������������Li st ���������������equal s( ) �������� ���������$����������������������������&�����2 �����&�f al se��

.�HashMap� ����Si mpl eVNO���-����������$������I nt eger ���-��������������&���������������!1>"&������������������������������$��ver si ons�����downl oads��8������������������������ ���$������ ����������!1K"&������������������������������������$���� ����������!3="��,���������hashCode( ) ��������������������������������&������� �������������������Obj ect ������������ �������������������������������������������������������-������

�������������� ����������������������������������������� ������������������������� ����!33"�����!34"&���� �������$������������Si mpl eVNO��������������� �����������compar eTo( ) ���������������Compar abl e����������&��������� ��������������� �������&������������������������-�������������������������������� ��!����,�������11�A&� ��;?3"��#��������2�� ��&����� ���������� ������ �����������2�� ������������ ����������Si mpl eVNO���-��������������������������������8� ����&����������������� ����������� ������-����������������������������������������������������������������������� ���

+�� ������������t est ( ) �����������62�� ���11�I������������������������������������������������������������� ��������������������

$(��������0���������� ��� ��1 ��#6�������������equals()����� ��

i mpor t j ava. ut i l . * ; publ i c c l ass Test Ver si onSi mpl e { publ i c st at i c voi d mai n( St r i ng[ ] ar gs) { ( new Test Ver si onSi mpl e( ) ) . t est ( ) ; } pr ot ect ed Obj ect makeVer si on( i nt a, i nt b, i nt c) { r et ur n new Si mpl eVNO( a, b, c) ; } pr ot ect ed voi d t est ( ) { / / Thr ee i ndi v i dual ver s i on number s. Obj ect l at est = makeVer si on( 9, 1, 1) ; / / ( 1) Obj ect i nShops = makeVer si on( 9, 1, 1) ; / / ( 2) Obj ect ol der = makeVer si on( 6, 6, 6) ; / / ( 3) / / An ar r ay of ver s i on number s.

Page 36 of 61

6/16/2006file://C:\Documents and Settings\cdot\Local Settings\Temp\~hhAFF6.htm

Page 37: Khalid Mughal Chapter 11

Obj ect [ ] ver s i ons = { / / ( 4) makeVer si on( 3, 49, 1) , makeVer si on( 8, 19, 81) , makeVer si on( 2, 48, 28) , makeVer si on( 10, 23, 78) , makeVer si on( 9, 1, 1) } ; / / An ar r ay of downl oads. I nt eger [ ] downl oads = { / / ( 5) new I nt eger ( 245) , new I nt eger ( 786) , new I nt eger ( 54) , new I nt eger ( 1010) , new I nt eger ( 123) } ; / / Var i ous t est s. Syst em. out . pr i nt l n( " Test obj ect r ef er ence and val ue equal i t y : " ) ; Syst em. out . pr i nt l n( " l at est : " + l at est + " , i nShops: " + i nShops + " , ol der : " + ol der ) ; Syst em. out . pr i nt l n( " l at est == i nShops: " + ( l at est == i nShops) ) ; / / ( 6) Syst em. out . pr i nt l n( " l at est . equal s( i nShops) : " + ( l at est . equal s( i nShops) ) ) ; / / ( 7) Syst em. out . pr i nt l n( " l at est == ol der : " + ( l at est == ol der ) ) ; / / ( 8) Syst em. out . pr i nt l n( " l at est . equal s( ol der ) : " + ( l at est . equal s( ol der ) ) ) ; / / ( 9) Obj ect sear chKey = i nShops; Syst em. out . pr i nt l n( " Sear ch key: " + sear chKey) ; / / ( 10) Syst em. out . pr i nt ( " Ar r ay: " ) ; f or ( i nt i = 0; i < ver s i ons. l engt h; i ++) / / ( 11) Syst em. out . pr i nt ( ver s i ons[ i ] + " " ) ; bool ean f ound = f al se; f or ( i nt i = 0; i < ver s i ons. l engt h && ! f ound; i ++) f ound = sear chKey. equal s( ver s i ons[ i ] ) ; / / ( 12) Syst em. out . pr i nt l n( " \ n Sear ch key f ound i n ar r ay: " + f ound) ; / / ( 13) Li st vnoLi st = Ar r ays. asLi st ( ver s i ons) ; / / ( 14) Syst em. out . pr i nt l n( " Li st : " + vnoLi st ) ; Syst em. out . pr i nt l n( " Sear ch key cont ai ned i n l i s t : " + vnoLi st . cont ai ns( sear chKey) ) ; / / ( 15) Map ver s i onSt at i s t i cs = new HashMap( ) ; / / ( 16) f or ( i nt i = 0; i < ver s i ons. l engt h; i ++) / / ( 17) ver s i onSt at i s t i cs. put ( ver s i ons[ i ] , downl oads[ i ] ) ; Syst em. out . pr i nt l n( " Map: " + ver s i onSt at i s t i cs) ; / / ( 18) Syst em. out . pr i nt l n( " Hash code f or keys i n t he map: " ) ; f or ( i nt i = 0; i < ver s i ons. l engt h; i ++) / / ( 19) Syst em. out . pr i nt l n( " " + ver s i ons[ i ] + " : " + ver s i ons[ i ] . hashCode( ) ) ; Syst em. out . pr i nt l n( " Sear ch key " + sear chKey + " has hash code: " + sear chKey. hashCode( ) ) ; / / ( 20) Syst em. out . pr i nt l n( " Map cont ai ns sear ch key: " + ver s i onSt at i s t i cs. cont ai nsKey( sear chKey) ) ; / / ( 21) Syst em. out . pr i nt l n( " Sor t ed l i s t : \ n\ t " + ( new Tr eeSet ( vnoLi st ) ) ) ; / / ( 22) Syst em. out . pr i nt l n( " Sor t ed map: \ n\ t " + ( new Tr eeMap( ver si onSt at i s t i cs) ) ) ; / / ( 23) Syst em. out . pr i nt l n( " Li st bef or e sor t i ng: " + vnoLi st ) ; / / ( 24) Col l ect i ons. sor t ( vnoLi st ) ;

Page 37 of 61

6/16/2006file://C:\Documents and Settings\cdot\Local Settings\Temp\~hhAFF6.htm

Page 38: Khalid Mughal Chapter 11

Syst em. out . pr i nt l n( " Li st af t er sor t i ng: " + vnoLi st ) ; Syst em. out . pr i nt l n( " Bi nar y sear ch i n l i s t : " ) ; / / ( 25) i nt r esul t I ndex = Col l ect i ons. bi nar ySear ch( vnoLi st , sear chKey) ; Syst em. out . pr i nt l n( " \ t Key: " + sear chKey + " \ t Key i ndex: " + r esul t I ndex) ; } }

<�� ������������ ������%�

Test obj ect r ef er ence and val ue equal i t y : l at est : ( 9. 1. 1) , i nShops: ( 9. 1. 1) , ol der : ( 6. 6. 6) l at est == i nShops: f al se l at est . equal s( i nShops) : f al se l at est == ol der : f al se l at est . equal s( ol der ) : f al se Sear ch key: ( 9. 1. 1) Ar r ay: ( 3. 49. 1) ( 8. 19. 81) ( 2. 48. 28) ( 10. 23. 78) ( 9. 1. 1) Sear ch key f ound i n ar r ay: f al se Li st : [ ( 3. 49. 1) , ( 8. 19. 81) , ( 2. 48. 28) , ( 10. 23. 78) , ( 9. 1. 1) ] Sear ch key cont ai ned i n l i s t : f al se Map: { ( 9. 1. 1) =123, ( 10. 23. 78) =1010, ( 8. 19. 81) =786, ( 3. 49. 1) =245, ( 2. 48. 28) =54} Hash code f or keys i n t he map: ( 3. 49. 1) : 13288040 ( 8. 19. 81) : 27355241 ( 2. 48. 28) : 30269696 ( 10. 23. 78) : 24052850 ( 9. 1. 1) : 26022015 Sear ch key ( 9. 1. 1) has hash code: 20392474 Map cont ai ns sear ch key: f al se Except i on i n t hr ead " mai n" j ava. l ang. Cl assCast Except i on . . .

.���� �������������������equal s( ) �������������������$����� �� ��������������������������������%�

� ���������%�0�����$�����������sel f &�sel f . equal s( sel f ) ������ �$��t r ue��

� �������%�0�����$������������x�����y&�x. equal s( y) ����t r ue�����������$����y. equal s( x) ����t r ue��

� !�������%�0�����$������������x&�y�����z&���������x. equal s( y) �����y. equal s( z) �����t r ue&������x. equal s( z) ����t r ue��

� "�������%�0�����$������������x�����y&������ ������������������x. equal s( y) ��� �$������������������������&� ��������������-��������������$����������������������������������������������������������������� ��������

� nul l ���� ����%�0�����$����*nul l �����������obj &�obj . equal s( nul l ) ������ �$��f al se��

����������������������������equal s( ) ���������������������� ������#��������������������'������������������������������� ����������� ����������� �� ����� �������������

Reflexivity

Page 38 of 61

6/16/2006file://C:\Documents and Settings\cdot\Local Settings\Temp\~hhAFF6.htm

Page 39: Khalid Mughal Chapter 11

������������� �$������������������-����������������������&����������������� �����������������#��������$����������$%�������-���� ������������������������������������-����������� �����������-���������������������$�!=="%�

i f ( t hi s == ar gument Obj ) r et ur n t r ue;

Symmetry

�����2 ��������x. equal s( y) �������������equal s( ) �����������������-�������������$���������������x&� ������������2 ��������y. equal s( x) �������������equal s( ) �����������������-�������������$���������������y�����������������������������������������������

#������equal s( ) �����������������������������������������&����������������������������$������� ���������������-�����������������������#�������� ����&��$�����$������������������������equal s( ) ���������������������������������������������������� ���������������� ���������� ������ ���&�������������������������������� �������������.�������������� ��������$� ����������!���*�������"��������� ������ ��������������equal s( ) ������������������$��������������

Transitivity

#��� ���������&�A�����B&���������������������������������������-����������������&�������������������������������������������&���$�B&���������������������������������� ������������������C��������� ���.��������������������������������������$��������$����������������������������

.��$ ����� ��������������������������������������$���� ��������equal s( ) ��������������������������������equal s( ) ����������������� �������&���� ��������������������� ������������equal s( ) ������������������������������$��������������������������������� ��������%�

r et ur n super . equal s( ar gument Obj ) && compar eSubcl assSpeci f i cAspect s( ) ;

������������������ �������$�������������*� ��������� ���������������������equal s( ) �������&�������������������������� ��������equal s( ) ��������������� ������������ �������*� ��������� ������8� ����&������� ��������������������� �����2������������������� ��������������������������������������������������������������������$���� ���������� �������������������������equal s( ) ����������#���������������equal s( ) ����������������������� ������ ������ ����������-����&��$�����$���������$���������#���������������equal s( ) ������������������� ������ ������ ����������-����&������������$���������$���������

#�������� �������������������&��������������������� ��������equal s( ) �������� ����� �������������������� ����������-����������������������equal s( ) ���������������������#����������&������� ��������equal s( ) ��������������������������������$��$���$���������������������������������������������equal s( ) ������������������������������ ������� ��������equal s( ) �������������������#�����������$������������ ��������equal s( ) �������������� ���������� �������*� ��������� �������������������-������

Consistency

������������������������� ����-��������������������!������*�����"��������������!������*�����"���������������$�������������������0�������������-����&����������������������������� �������������������������!�������"����������������� ������������������������8� ����&�����������������-����&������������������� �$�������������������equal s( ) ��������������������������������������� �����������

Page 39 of 61

6/16/2006file://C:\Documents and Settings\cdot\Local Settings\Temp\~hhAFF6.htm

Page 40: Khalid Mughal Chapter 11

�������� ��������������������-����&�������������������������������$�����������������������

null comparison

���������������������������-����������������nul l �����������������������������equal s( ) ������������������f al se�������������������������� �����2�� ����7������ ��������������������������������.��������������������������������$���������� ��������������$ �����$&��������������������� ����������������������2 ������$���� ����� ��������nul l ������%�

i f ( ar gument Obj == nul l ) r et ur n f al se;

#�����$������&������� ���������������������i nst anceof �� ��������#���� �$����������f al se�������������� ���������nul l %�

i f ( ! ( ar gument Obj i nst anceof MyRef Type) ) r et ur n f al se;

�������������������������������������������������������������&������������������������������������$��� �������

$(��������2������������������equals()����� ��

publ i c c l ass Usabl eVNO { / / Over r i des equal s( ) , but not hashCode( ) . pr i vat e i nt r el ease; pr i vat e i nt r evi s i on; pr i vat e i nt pat ch; publ i c Usabl eVNO( i nt r el ease, i nt r ev i s i on, i nt pat ch) { t hi s. r el ease = r el ease; t hi s. r evi s i on = r evi s i on; t hi s. pat ch = pat ch; } publ i c St r i ng t oSt r i ng( ) { r et ur n " ( " + r el ease + " . " + r evi s i on + " . " + pat ch + " ) " ; } publ i c bool ean equal s( Obj ect obj ) { / / ( 1) i f ( obj == t hi s) / / ( 2) r et ur n t r ue; i f ( ! ( obj i nst anceof Usabl eVNO) ) / / ( 3) r et ur n f al se; Usabl eVNO vno = ( Usabl eVNO) obj ; / / ( 4) r et ur n vno. pat ch == t hi s. pat ch && / / ( 5) vno. r evi s i on == t hi s. r evi s i on && vno. r el ease == t hi s. r el ease; } }

62�� ���11�K���� ������� �������������������equal s( ) �����������������������������5�2�&� �� ������������������������� ��������������equal s( ) ���������

Method overriding signature

Page 40 of 61

6/16/2006file://C:\Documents and Settings\cdot\Local Settings\Temp\~hhAFF6.htm

Page 41: Khalid Mughal Chapter 11

����������� �����$ �����

publ i c bool ean equal s( Obj ect obj ) / / ( 1)

������������������������������������������������������� �����������������$ ��Obj ect ����������� ����������� �����������������������&����������������%�

publ i c bool ean equal s( MyRef Type obj ) / / Over l oaded.

������� ����� ����������� ������B���������������������������������������������� ��������&��� ���������������$ �������������������B��������������������������������������������������&��� ���������������$ ������������������-�������������$���������������B�� ������������-������������������MyRef Type��������������������equal s( ) �����������������������&������������������������������%�

MyRef Type r ef 1 = new MyRef Type( ) ; MyRef Type r ef 2 = new MyRef Type( ) ; Obj ect r ef 3 = r ef 2; bool ean b1 = r ef 1. equal s( r ef 2) ; / / Tr ue. Cal l s equal s( ) i n MyRef Type. bool ean b2 = r ef 1. equal s( r ef 3) ; / / Al ways f al se. Cal l s equal s( ) i n Obj ect .

8� ����&��������equal s( ) ������������������������������$&����$��������������������������MyRef Type������������.����������� �������������� ������������&���������equal s( ) �����������������������������

Reflexivity test

��������������$���������������� ����������������equal s( ) �������&��������������������� �����������������������t r ue������equal s( ) �����������62�� ���11�K�������������������!3"��

Correct argument type

����equal s( ) �����������62�� ���11�K�������������$ ��������������������-�������!4"&�����������i nst anceof �� ������%�

i f ( ! ( obj i nst anceof Usabl eVNO) ) / / ( 3) r et ur n f al se;

������������������������nul l ���� ���������������$&�����������f al se�����������������obj ���������������nul l ��

����i nst anceof �� ������� ����������������t r ue�����������������obj ����������������������-�����������������Usabl eVNO��#���������������f i nal &��������������������������F������������������������-������������������!4"��������������� �������$���������� ����������������������2������������������-����&����������������������-����%�

i f ( ( obj == nul l ) | | ( obj . get Cl ass( ) ! = t hi s. get Cl ass( ) ) ) / / ( 3a) r et ur n f al se;

������������!4�"������� ������������nul l ���� ��������2 ������$�������2 ��������( obj . get Cl ass( ) ! = t hi s. get Cl ass( ) ) ������������ ��������������������������� ����-�����������������������������-������ ����������������#������������������&�������-��������������������������������������

Page 41 of 61

6/16/2006file://C:\Documents and Settings\cdot\Local Settings\Temp\~hhAFF6.htm

Page 42: Khalid Mughal Chapter 11

Argument casting

�������������������$����������������������������������� �����������������������i nst anceof �� ��������������������������$������������&������������62�� ���11�K��������������������������!;"�������� ����������*� ���������������� �������%�

Usabl eVNO vno = ( Usabl eVNO) obj ; / / ( 4)

Field comparisons

6�������������� ������������������� ���������������������������������-�������������������������������������������������0���������������������� ���������������$ ��&������� �������������������������� ������#����������������������Usabl eVNO����62�� ���11�K���������$����������� ���������������$ ����:��������������� ��������������������� ������������������������$���� ����� ��Usabl eVNO���-����%�

r et ur n vno. pat ch == t hi s. pat ch && / / ( 5) vno. r evi s i on == t hi s. r evi s i on && vno. r el ease == t hi s. r el ease;

#��������������� ��������������������t r ue&�����equal s( ) ����������������t r ue��

0�����������������������������&�������-��������������$�������������������������� ������0����2�� ��&��������Usabl eVNO�������������������������������pr oduct I nf o&� �������������������&���������� ����������������������%�

( vno. pr oduct I nf o == t hi s. pr oduct I nf o | | ( t hi s. pr oduct I nf o ! = nul l && t hi s. pr oduct I nf o. equal s( vno. pr oduct I nf o) ) )

�����2 ��������vno. pr oduct I nf o == t hi s. pr oduct I nf o���������������� ���������$����������� ����-�������������� ���������������������-�������������$������pr oduct I nf o�������������#�������������������Nul l Poi nt er Except i on����������� �&�����equal s( ) ������������������������������t hi s. pr oduct I nf o��������������nul l ��

62������� ������������������* ��������������������������������������$��������������&��������������������������������������������������� ��������!�������������������Fl oat . f l oat ToI nt Bi t s( ) �����Doubl e. doubl eToLongBi t s( )"���������������������������������������������������������* �������� �����������������������5.5���������������������)����!�������������equal s( ) �����������Fl oat �����Doubl e��������"��

<��$������������������������������������������������������������������������������������������������&� �������� ������������� ��������������������������������������-���&������������������������������&�������$��������������������������������������� ��������B�� ������������������������������������������������������&��������equal s( ) ��������������������� ���������������������������&������������ ������������

������������� ������������ ������������������������������������������ ���������������������������� ��������0�������������������������$������������������������ ������������$���� �������������������������*��������������� ���������#�������2�� ��&� ������������������������������������������������&� ����&��������&��������������������������������������������������������������������r et ur n��������������!?"����62�� ���11�K��

.��������&������ �������������������equal s( ) �����������������������������������������������������

Page 42 of 61

6/16/2006file://C:\Documents and Settings\cdot\Local Settings\Temp\~hhAFF6.htm

Page 43: Khalid Mughal Chapter 11

�����������

62�� ���11�1=���������������������������������Usabl eVNO������62�� ���11�K���������������������������������������������������62�� ���11�I�����������������������������������Usabl eVNO���������������equal s( ) ���������

$(���������3���������� ��� ��#6�������������equals()����� ��

publ i c c l ass Test Ver si onUsabl e ext ends Test Ver si onSi mpl e { publ i c st at i c voi d mai n( St r i ng[ ] ar gs) { ( new Test Ver si onUsabl e( ) ) . t est ( ) ; } pr ot ect ed Obj ect makeVer si on( i nt a, i nt b, i nt c) { r et ur n new Usabl eVNO( a, b, c) ; } }

<�� ������������ ������%�

Test obj ect r ef er ence and val ue equal i t y : l at est : ( 9. 1. 1) , i nShops: ( 9. 1. 1) , ol der : ( 6. 6. 6) l at est == i nShops: f al se l at est . equal s( i nShops) : t r ue l at est == ol der : f al se l at est . equal s( ol der ) : f al se Sear ch key: ( 9. 1. 1) Ar r ay: ( 3. 49. 1) ( 8. 19. 81) ( 2. 48. 28) ( 10. 23. 78) ( 9. 1. 1) Sear ch key f ound i n ar r ay: t r ue Li st : [ ( 3. 49. 1) , ( 8. 19. 81) , ( 2. 48. 28) , ( 10. 23. 78) , ( 9. 1. 1) ] Sear ch key cont ai ned i n l i s t : t r ue Map: { ( 10. 23. 78) =1010, ( 2. 48. 28) =54, ( 3. 49. 1) =245, ( 9. 1. 1) =123, ( 8. 19. 81) =786} Hash code f or keys i n t he map: ( 3. 49. 1) : 27355241 ( 8. 19. 81) : 30269696 ( 2. 48. 28) : 24052850 ( 10. 23. 78) : 26022015 ( 9. 1. 1) : 3541984 Sear ch key ( 9. 1. 1) has hash code: 11352996 Map cont ai ns sear ch key: f al se Except i on i n t hr ead " mai n" j ava. l ang. Cl assCast Except i on . . .

������� ������������ ���������� ���������-�����������������$������� �������������$��<�-�����������������$������ ��������������������������&�������������$�����equal s( ) ���������

������������������Usabl eVNO���-��������������$��������������Usabl eVNO���-���������� �����������&������������������ ���������������������������������������-����&������������������������������������

8� ����&������������������ �������������������������������&������������������������0������������������HashMap&� ��������������������������������� ���� ��������equal s( ) ���������hashCode( ) ����������0������������������������������������������� �&� �� ���� ������������ �������������������compar eTo( ) ���������

The hashCode() Method

Page 43 of 61

6/16/2006file://C:\Documents and Settings\cdot\Local Settings\Temp\~hhAFF6.htm

Page 44: Khalid Mughal Chapter 11

$�����������������������������������������������������������������.�����������������������������������$&� ����������������������������������������������$�����������������������������< ���������������������������������������� �������������$�����2���������������B�����������������������������$�����2����������$�������������������������$�����2�����������$������������������������������������������������������������������������������������ ������������������

,��������������������������������� ������� �%�

� 8�����������������������������������������

� #������������������������������������$��������������&�����������������������������

5�������������� �������������������������(�������������������������������������������������$��� �������������������$�����������#������������������������� �*��� � ������%�

� 8�������������$��������������������������

� #��������$������������������������������&�����������������������������������������

�������������������������������������������&���������������������������������������������������������������������������������������������������������������������������������������$������������������������������������������������������

������������������$������������������������0���������������������������������������������&������������������������$���������������� �����������������������������������������*�������������������������&����������&� �������� ���������������������%����������������������$�����������

���� ����������������������������������������$�����������$��� � ������������������������������������������������������������������������������������.�������������������������������������� ������$� �����������������������.���������������������� ����������������������������������������������������������������������������������� ���������������������,���������������������������������$�����������������0���������$&��������2���������������������������������������������������������

.��������������������������������������������&����������������������������$����������$����� ���������������������� �������������������������������$� ��������$�����������$&�����������������������

#����-��������������������������������������������*������������������������ ���������j ava. ut i l � �������!����������11�3"&���������������� �������� �� �������� ������������������������� ���������������������Obj ect ������%�

� ��hashCode( ) ������������� ������������������������������-�����

� ���equal s( ) ���������������������-����������������$�

.����������������������� �����������������������&�����������������������equal s( ) �����������������������hashCode( ) ���������B���������������������������������������������$�����������Usabl eVNO����62�� ���11�K��6������������������������������������$�����62�� ���11�1=��������� ������������ ���������� ����������� � ������������� ����������������������%�

Map: { ( 10. 23. 78) =1010, ( 2. 48. 28) =54, ( 3. 49. 1) =245, ( 9. 1. 1) =123, ( 8. 19. 81) =786}

Page 44 of 61

6/16/2006file://C:\Documents and Settings\cdot\Local Settings\Temp\~hhAFF6.htm

Page 45: Khalid Mughal Chapter 11

����hashCode( ) �����������������Obj ect ��������������������������$�����Usabl eVNO�������������&����������&������������ �����������������������������$���-������������� ������������ ���������� ����������������������������$����������������������$����������� %�

Hash code f or keys i n t he map: ( 3. 49. 1) : 27355241 ( 8. 19. 81) : 30269696 ( 2. 48. 28) : 24052850 ( 10. 23. 78) : 26022015 ( 9. 1. 1) : 3541984

.���� ��������������������������$�!K�1�1"������������� ����������������%�

Sear ch key ( 9. 1. 1) has hash code: 11352996 Map cont ai ns sear ch key: f al se

�������������������� ����-����&� ��������������������������������equal s( ) �������&��������������������������������hashCode( ) ���������������Obj ect �����������������&�������$���-����!K�1�1"������������$�GK�1�1&�134H���������� ����������������������������������������������$���-����!K�1�1"����������-����������������������������������������� ������������������$���-����������������������������������������������������$�GK�1�1&�134H&� ���������������������������� �����$�������������������<��$����������������equal s( ) ���������������������������������Usabl eVNO����������������$��������������hashCode( ) ���������%���������#�������� ����������������������

General Contract of the hashCode() method

����������������������������hashCode( ) ����������� ������%�

� "������������������������%������ ����������������������hashCode( ) ����������������-��������������������$���������������������������������������2��������������� ��������&� ��������������-���������������������������������������������������$�����equal s( ) ��������������������������������������������������������������������2����������������� ���������������������������������� �������������������������������� �����������������������������������������$��

� %�#��������������������� ����������������������%�#��� ����-��������������������������������equal s( ) �������&����������hashCode( ) ������������� ��������������������������������������-������������������������� ��������������������������������equal s( ) ���������

� %�#��������������������� ���������������������������������%�#��� ����-�����������������

�����������������equal s( ) �������&����������hashCode( ) ����������������� ���������������������������������������-������#������������$����������������������hashCode( ) �������� ����������������������������������������-������

5�������������������������������������� �$��������-����� ��������������������������������5��� ������������������������������������������-����������������������������������� ����������&��������������-����� �����������������������������

Heuristics for implementing the hashCode() method

#��62�� ���11�11&�������� ���������������������������������hashCode( ) ���������������Rel i abl eVNO������������������������������������ ������������$������������������������������������������������� ��������������������������� �����������%�

Page 45 of 61

6/16/2006file://C:\Documents and Settings\cdot\Local Settings\Temp\~hhAFF6.htm

Page 46: Khalid Mughal Chapter 11

hashVal ue = 11 * 313 + r el ease * 312 + r evi s i on * 311 + pat ch

����������������������$�������������������!����,�������D�4&� ��?KA"��6�������������������������������������������� ���������<��$�������������������������������������equal s( ) ������������������������������������������-�������������������������������������equal s( ) �������&����������������������������������������������hashCode( ) ���������

$(����������������������������hashCode()����� ��

publ i c c l ass Rel i abl eVNO { / / Over r i des bot h equal s( ) and hashCode( ) . pr i vat e i nt r el ease; pr i vat e i nt r evi s i on; pr i vat e i nt pat ch; publ i c Rel i abl eVNO( i nt r el ease, i nt r evi s i on, i nt pat ch) { t hi s. r el ease = r el ease; t hi s. r evi s i on = r evi s i on; t hi s. pat ch = pat ch; } publ i c St r i ng t oSt r i ng( ) { r et ur n " ( " + r el ease + " . " + r evi s i on + " . " + pat ch + " ) " ; } publ i c bool ean equal s( Obj ect obj ) { / / ( 1) i f ( obj == t hi s) / / ( 2) r et ur n t r ue; i f ( ! ( obj i nst anceof Rel i abl eVNO) ) / / ( 3) r et ur n f al se; Rel i abl eVNO vno = ( Rel i abl eVNO) obj ; / / ( 4) r et ur n vno. pat ch == t hi s. pat ch && / / ( 5) vno. r evi s i on == t hi s. r evi s i on && vno. r el ease == t hi s. r el ease; } publ i c i nt hashCode( ) { / / ( 6) i nt hashVal ue = 11; hashVal ue = 31 * hashVal ue + r el ease; hashVal ue = 31 * hashVal ue + r evi s i on; hashVal ue = 31 * hashVal ue + pat ch; r et ur n hashVal ue; } }

������������������������ �������i nt ������������sf Val ����������������������������sf &������������������������������������������ �����!1"����������� �������%�

publ i c i nt hashCode( ) { i nt s f Val ; i nt hashVal ue = 11; . . . s f Val = . . . / / Comput e hash val ue f or each s i gni f i cant f i el d sf . hashVal ue = 31 * hashVal ue + sf Val ; / / ( 1) . . . r et ur n hashVal ue;

Page 46 of 61

6/16/2006file://C:\Documents and Settings\cdot\Local Settings\Temp\~hhAFF6.htm

Page 47: Khalid Mughal Chapter 11

}

��������� ����������������������������������� �����������������������������������������������������������������������2���������������

B��������������������������sf Val �������������������������sf ��� �������������$ ��������������%�

� 0�����sf ����bool ean%�sf Val = sf ? 0 : 1�

� 0�����sf ����byt e, char , shor t , or i nt %�sf Val = ( i nt ) sf �

� 0�����sf ����l ong%�sf Val = ( i nt ) ( sf ^ ( sf >>> 32) ) �

� 0�����sf ����f l oat %�sf Val = Fl oat . f l oat ToI nt ( sf ) �

� 0�����sf ����doubl e%�l ong sf Val Temp = Doubl e. doubl eToLong( sf ) ; �

sf Val = ( i nt ) ( sf Val Temp ^ ( sf Val Temp >>> 32) )

� 0�����sf ����������������������������������-������$ �����$&�����hashCode( ) �����������������������������$��������equal s( ) �����������������������������$%�

sf Val = ( sf == nul l ? 0 : s f . hashCode( ) )

� 0�����sf �����������$��B����������������������������������������������������$�������������

������������� ������������������������� ����������������������������� �������� ������������������������������0������ ��������������������������������������������������2�������������������� ���������������������������������� �����������������������������������������������$������ �����������������������������0���������������������������������������equal s( ) �����������������2������7������ ���&�����hashCode( ) ������������������� �������������������equal s( ) ���������.�� ��������equal s( ) �������&��������������������������������!����&���� ����������"���������������������

.���������������������������������������������������$������������� �� �����������������������������������2�� �����������������������������������������������

publ i c i nt hashCode( ) { r et ur n 1949; }

.�����-������������������������������������������������������������������������������������������������������0������������������������$&���������������������������������� ����������������������������������������-������

0����������� �� ����$ ��&�����hashCode( ) ��� �����������������������i nt ��� ������������������� ��������������&���������������� ���������������������i nt &������������$������Bool ean���-�������������bool ean����������t r ue�����f al se������� ������������������&� �������������������$�����hashCode( ) ���������

����hashCode( ) ���������������St r i ng��������������������������������������������������� ��$������� ����������������������������41&��������������������������������������������������&�������������������

Page 47 of 61

6/16/2006file://C:\Documents and Settings\cdot\Local Settings\Temp\~hhAFF6.htm

Page 48: Khalid Mughal Chapter 11

�����������������������������0����2�� ��&������������������������������" abc" ������� ������������� �%�

hashVal ue = ' a' * 312 + ' b' * 311 + ' c ' * 310 = 97 * 31 * 31 + 98 * 31 + 99 = 96354

0���������������-����&����������������������������&��������&������������������������������ ������������hashCode( ) �������������������

��������������62�� ���11�13��������-������������������Rel i abl eVNO����62�� ���11�11��������������� �����������������������$���������� ��<�� ������������ ���������� �������������$���-����!K�1�1"������������$�GK�1�1&�134H���������� �����������������������������������������$���-����!K�1�1"������������������������������������-���������������������������������������&����������������������$���-���������� ��������������������������#����������������$�GK�1�1&�134H�����������equal s( ) ���������$������������$���������������������$���� �����������������$���-����!K�1�1"�����������$���-����!K�1�1"�������������$��

$(�������������������� ��� ��#6�������������hashCode()����� ��

publ i c c l ass Test Ver si onRel i abl e ext ends Test Ver si onSi mpl e { publ i c st at i c voi d mai n( St r i ng[ ] ar gs) { ( new Test Ver si onRel i abl e( ) ) . t est ( ) ; } pr ot ect ed Obj ect makeVer si on( i nt a, i nt b, i nt c) { r et ur n new Rel i abl eVNO( a, b, c) ; } }

<�� ������������ ������%�

. . . Map: { ( 10. 23. 78) =1010, ( 2. 48. 28) =54, ( 3. 49. 1) =245, ( 8. 19. 81) =786, ( 9. 1. 1) =123} Hash code f or keys i n t he map: ( 3. 49. 1) : 332104 ( 8. 19. 81) : 336059 ( 2. 48. 28) : 331139 ( 10. 23. 78) : 338102 ( 9. 1. 1) : 336382 Sear ch key ( 9. 1. 1) has hash code: 336382 Map cont ai ns sear ch key: t r ue Except i on i n t hr ead " mai n" j ava. l ang. Cl assCast Except i on . . .

The compareTo() Method

����Compar abl e���������������������������,�������11�A���� ����;?3��#������������������ ������������ ������ ��������������$�������%�����compar eTo( ) ���������������������������������������������������������������������������������������� ������������Compar abl e������������<�-������� ����������Compar abl e������������������������������������������������ ���

+���� ���������������������������������compar eTo( ) �������������,�������11�A���� ����;?3�%�

i nt compar eTo( Obj ect o)

#����������������������������&�)���&������ ���������������������������������-����������������&�

Page 48 of 61

6/16/2006file://C:\Documents and Settings\cdot\Local Settings\Temp\~hhAFF6.htm

Page 49: Khalid Mughal Chapter 11

��������&���������������������� ����������-���&�����������������������������#������ ����Cl assCast Except i on������������������������ ���������������������������������������������$ �������������������-�����

�.���� �������������������compar eTo( ) ������������������-������������������������������������� ������������%�

� 0�����$�� ����-�����������������&����������������-��������������������������������������������

���������-���&�������������������-���������������������������������������������������������-���&���� �������$��

� .������������������ �����������������!�����������������������������"�����������������compar eTo( ) ��������������������������0����2�� ��&����obj 1. compar eTo( obj 2) > 0�����obj 2. compar eTo( obj 3) > 0&������obj 1. compar eTo( obj 3) > 0��

� 0�����$�� ����-�����������������&� �������� ������������&�����compar eTo( ) ����������������������������������������������� ����-������������ ����� ������$���������-�����

� ����compar eTo( ) �����������������������������������&��������&�( obj 1. compar eTo( obj 2) == 0) == ( obj 1. equal s( obj 2) ) �������������������������������-����� ��������������������������������������������� ���

��������������������*)���������������������$�������������������������7����������������������������������������� ������������������������������������compar eTo( ) ���������������������������������������������equal s( ) �������&� �������������������������������������� �������-�����������������$�������������compar eTo( ) ��������������������� �������������������������equal s( ) ���������.�compar eTo( ) �������������������� ������������������ ������ ������-�����������������������

#� ��������������compar eTo( ) ������������������������������������� ��������������equal s( ) ���������#������&����������������������������$��������equal s( ) ���������������������������������������$��������compar eTo( ) �������&�����equal s( ) ��� �������������������������compar eTo( ) ����������������������������������� ���������������� �$������������� ����������������

publ i c bool ean equal s( Obj ect ot her ) { / / . . . r et ur n compar eTo( ot her ) == 0; }

.���� �������������������compar eTo( ) ����������������������������������������������� �����62�� ���11�14�������� ������������������������������� ������������0���� ������������������������� ����� ���&�����������������������f i nal �����������������������2��������

$(����������������������������compareTo()����� �� ������Comparable�

����������

publ i c f i nal c l ass Ver si onNumber i mpl ement s Compar abl e { pr i vat e f i nal i nt r el ease; pr i vat e f i nal i nt r evi s i on; pr i vat e f i nal i nt pat ch;

Page 49 of 61

6/16/2006file://C:\Documents and Settings\cdot\Local Settings\Temp\~hhAFF6.htm

Page 50: Khalid Mughal Chapter 11

publ i c Ver si onNumber ( i nt r el ease, i nt r evi s i on, i nt pat ch) { t hi s. r el ease = r el ease; t hi s. r evi s i on = r evi s i on; t hi s. pat ch = pat ch; } publ i c St r i ng t oSt r i ng( ) { r et ur n " ( " + r el ease + " . " + r evi s i on + " . " + pat ch + " ) " ; } publ i c bool ean equal s( Obj ect obj ) { / / ( 1) i f ( obj == t hi s) / / ( 2) r et ur n t r ue; i f ( ! ( obj i nst anceof Ver si onNumber ) ) / / ( 3) r et ur n f al se; Ver si onNumber vno = ( Ver si onNumber ) obj ; / / ( 4) r et ur n vno. pat ch == t hi s. pat ch && / / ( 5) vno. r evi s i on == t hi s. r evi s i on && vno. r el ease == t hi s. r el ease; } publ i c i nt hashCode( ) { / / ( 6) i nt hashVal ue = 11; hashVal ue = 31 * hashVal ue + r el ease; hashVal ue = 31 * hashVal ue + r evi s i on; hashVal ue = 31 * hashVal ue + pat ch; r et ur n hashVal ue; } publ i c i nt compar eTo( Obj ect obj ) { / / ( 7) Ver si onNumber vno = ( Ver si onNumber ) obj ; / / ( 8) / / Compar e t he r el ease number s. ( 9) i f ( r el ease < vno. r el ease) r et ur n - 1; i f ( r el ease > vno. r el ease) r et ur n 1; / / Rel ease number s ar e equal , ( 10) / / must compar e r evi s i on number s. i f ( r evi s i on < vno. r evi s i on) r et ur n - 1; i f ( r evi s i on > vno. r evi s i on) r et ur n 1; / / Rel ease and r evi s i on number s ar e equal , ( 11) / / must compar e pat ch number s. i f ( pat ch < vno. pat ch) r et ur n - 1; i f ( pat ch > vno. pat ch) r et ur n 1; / / Al l f i el ds ar e equal . ( 12) r et ur n 0; } }

����compar eTo( ) ���������������������������������������������$ ���������������������Cl assCast Except i on&�������nul l �����������������������������Nul l Poi nt er Except i on������������

Page 50 of 61

6/16/2006file://C:\Documents and Settings\cdot\Local Settings\Temp\~hhAFF6.htm

Page 51: Khalid Mughal Chapter 11

�����������������������������������!I"����62�� ���11�14��

������������������ ����� ���������������������������������������������������������������������������#���������������������������������&���������������������������� ����������&������ ����$���������������������&� �������� ���������������������� �����������5���������������2����������������������������������$���� ��������������� �������������� ����������������������������������$��������������$��#��������$���� ���������� �������������������������������*���������������� ���������#����������������������������������&���)����������������������� ������������ ����������� �������������������compar eTo( ) �����������!K"���������!13"����62�� ���11�14��

B�� ������������������������������������������� ����)����0����2�� ��&����������������� �����������������������������!K"����62�� ���11�14%�

i f ( r el ease < vno. r el ease) r et ur n - 1; i f ( r el ease > vno. r el ease) r et ur n 1; / / Next f i el d compar i son

��������� �������$���������� �������������������������� ������&� ��������������������������������� ��������������%�

i nt r el easeDi f f = r el ease - vno. r el ease; i f ( r el easeDi f f ! = 0) r et ur n r el easeDi f f ; / / Next f i el d compar i son

������������������������������������������������������������������������������i nt ��$ ���

,������������������ �������*bool ean� ���������������������������$���� ��������������������������� ��������<�����>��0������ ������������������������������������������������-����&����������� ������������������������compar eTo( ) ������������������������������� ��������

62�� ���11�1;���������������������������������Ver si onNumber ������62�� ���11�14��������������������������������������������������������62�� ���11�I�����������������������������������Ver si onNumber ��������������������equal s( ) �����hashCode( ) ��������&������� ������������compar eTo( ) ���������#����������&�����compar eTo( ) ���������������������� ������������.������������������������ ������2 �����'������ ������������� ��&�Ver si onNumber ���-����������� ������������������������������������ ���

Syst em. out . pr i nt l n( " Sor t ed l i s t : \ n\ t " + ( new Tr eeSet ( vnoLi st ) ) ) ; / / ( 22) Syst em. out . pr i nt l n( " Sor t ed map: \ n\ t " + ( new Tr eeMap( ver si onSt at i s t i cs) ) ) ; / / ( 23)

$��������&�����������Tr eeSet ����������������������������� �����������compar eTo( ) ���������������� ������������ ����������62�� ���11�1;���� �����������Tr eeSet &������������!33"&��������������������������������������������������������������$�����compar eTo( ) ���������.���������$&�������� ������������ ����������62�� ���11�1;���� �����������Tr eeMap&������������!34"&���������������������������������������$�&� ����������������������������������������$�����compar eTo( ) ���������

$(���������'���������� ��� �������������������compareTo()����� ��

Page 51 of 61

6/16/2006file://C:\Documents and Settings\cdot\Local Settings\Temp\~hhAFF6.htm

Page 52: Khalid Mughal Chapter 11

publ i c c l ass Test Ver si on ext ends Test Ver s i onSi mpl e { publ i c st at i c voi d mai n( St r i ng[ ] ar gs) { ( new Test Ver si on( ) ) . t est ( ) ; } pr ot ect ed Obj ect makeVer si on( i nt a, i nt b, i nt c) { r et ur n new Ver si onNumber ( a, b, c) ; } }

<�� ������������ ������%�

. . . Sor t ed l i s t : [ ( 2. 48. 28) , ( 3. 49. 1) , ( 8. 19. 81) , ( 9. 1. 1) , ( 10. 23. 78) ] Sor t ed map: { ( 2. 48. 28) =54, ( 3. 49. 1) =245, ( 8. 19. 81) =786, ( 9. 1. 1) =123, ( 10. 23. 78) =1010} . . .

+����������������������������������������������������������������'�����$��������� ���������$�����Col l ect i ons�����Ar r ays����������������j ava. ut i l � ������������������������,�������11�I����������� ��������������������������������������������� ��������������!1;"����62�� ���11�1;&��������������$���������������vnoLi st %�

Syst em. out . pr i nt l n( " Li st bef or e sor t i ng: " + vnoLi st ) ; / / ( 24) Col l ect i ons. sor t ( vnoLi st ) ; Syst em. out . pr i nt l n( " Li st af t er sor t i ng: " + vnoLi st ) ;

������� ���������2��������������������� �������������������������������������� �������%�

Li st bef or e sor t i ng: [ ( 3. 49. 1) , ( 8. 19. 81) , ( 2. 48. 28) , ( 10. 23. 78) , ( 9. 1. 1) ] Li st af t er sor t i ng: [ ( 2. 48. 28) , ( 3. 49. 1) , ( 8. 19. 81) , ( 9. 1. 1) , ( 10. 23. 78) ]

.������$�������������������������������������������������������2�����������������������!K�1�1"&����������$���������������sear chKey����62�� ���11�1;%�

i nt r esul t I ndex = Col l ect i ons. bi nar ySear ch( vnoLi st , sear chKey) ; Syst em. out . pr i nt l n( " \ t Key: " + sear chKey + " \ t Key i ndex: " + r esul t I ndex) ;

62����������������� ����������������������2�����������������$������������%�

Key: ( 9. 1. 1) Key i ndex: 3

����������� ����������� �

11.8 Working with Collections

����������������� ������������������������������ ������������� ����������������$%�

� ������*�����$�

� ����������������������$�

Page 52 of 61

6/16/2006file://C:\Documents and Settings\cdot\Local Settings\Temp\~hhAFF6.htm

Page 53: Khalid Mughal Chapter 11

����������������� �������������������&��2�� ������Vect or �����Hasht abl e&���������������*����&��������&���������������$��������-�� ����)����$��������������������.�������������������������������������������������������������L���� ��������������������������������������������������������������.�������������-������ �������������������&������$������������������������������������

#���������� ��������� �����������������������&�L���� ����������������������������������������� �� ������$���������������������������������#�������������&������������������������� ���������������� �������������#������������������������ ������$�����������������$��������&�����Col l ect i ons������� ������������������������������������������������ ��������*�����������������������

Synchronized Collection Decorators

��������� �����������������$������������������Col l ect i ons �������������������)����������������������������� �������������*�����$����������������%�

st at i c Col l ect i on synchr oni zedCol l ect i on( Col l ect i on c) st at i c Li st synchr oni zedLi st ( Li st l i s t ) s t at i c Map synchr oni zedMap( Map m) st at i c Set synchr oni zedSet ( Set s) st at i c Sor t edMap synchr oni zedSor t edMap( Sor t edMap m) st at i c Sor t edSet synchr oni zedSor t edSet ( Sor t edSet s)

�.���������������������������������$����������������������������$�������)������ &������ ���&����*�������������������������$��������

/ / Cr eat e a synchr oni zed decor at or . Col l ect i on syncDecor at or = Col l ect i ons. synchr oni zedCol l ect i on( nonsyncCol l ect i on) ;

#����������&�������������������$�������)�������������&�������������������������������������������������$�������)�������������������%�

/ / Each t hr ead can onl y t r aver se when synchr oni zed on t he decor at or . synchr oni zed( syncDecor at or ) { f or ( I t er at or i t er at or = syncDecor at or . i t er at or ( ) ; i t er at or . hasNext ( ) ; ) doSomet hi ng( i t er at or . next ( ) ) ; }

Unmodifiable Collection Decorators

��������� �����������������$������������������Col l ect i ons ������������������������ �����������*���$���������������������$��������������%�

st at i c Col l ect i on unmodi f i abl eCol l ect i on( Col l ect i on c) st at i c Li st unmodi f i abl eLi st ( Li st l i s t ) s t at i c Map unmodi f i abl eMap( Map m) st at i c Set unmodi f i abl eSet ( Set s) st at i c Sor t edMap unmodi f i abl eSor t edMap( Sor t edMap m) st at i c Sor t edSet unmodi f i abl eSor t edSet ( Sor t edSet s)

Page 53 of 61

6/16/2006file://C:\Documents and Settings\cdot\Local Settings\Temp\~hhAFF6.htm

Page 54: Khalid Mughal Chapter 11

�������������������� ��������� ��������������������������$�����������$��������������&��������� ����Unsuppor t edOper at i onExcept i on��#��������� ����������������&������������������������ �����������$������������������������������#������������������ &����������������������� ����������$��������������������������������� ��

Sorting Collections

����Col l ect i ons������� ��������� ������������������������������������

st at i c voi d sor t ( Li st l i s t ) s t at i c voi d sor t ( Li st l i s t , Compar at or comp)

������������������������������������������������������������������������������������������������������������������������������������������������������ ���������$�������� �������!����,�������11�A"��

Searching in Collections

����Col l ect i ons������� ��������� ������������������������������������������������

st at i c i nt bi nar ySear ch( Li st sor t edLi st , Obj ect obj ) st at i c i nt bi nar ySear ch( Li st sor t edLi st , Obj ect obj , Compar at or comp)

�����������������������$������������������������2��������obj ����������������� �������������������������������������������������������������������������������������������������&� ����������������������������������������������������������������������������������������������$�������� ��������

���������� �����������������������������������2�����������������������������%�

st at i c Obj ect max( Col l ect i on c) st at i c Obj ect max( Col l ect i on c, Compar at or comp) st at i c Obj ect mi n( Col l ect i on c) st at i c Obj ect mi n( Col l ect i on c, Compar at or comp)

�������*������������������������������������������������������������������������������������������������������������������������������������������������$�������� ��������

��������������������������� �� ������������������)�������������������������������������� �������������$�����������&�����������������$�����������

B���������$��������������� ��������� �$��������������� �����������������������NoSuchEl ement Except i on��

Singleton Collections

.�������������&����������� �!����&������������������������������ ���������������$����������������

Page 54 of 61

6/16/2006file://C:\Documents and Settings\cdot\Local Settings\Temp\~hhAFF6.htm

Page 55: Khalid Mughal Chapter 11

��������$&���� �������$"�����������������$������������������ �����������������$����������������Col l ect i ons������&���� �������$%�

st at i c Set s i ngl et on( Obj ect o) st at i c Li st s i ngl et onLi st ( Obj ect o) st at i c Map s i ngl et onMap( Obj ect key, Obj ect val ue)

�0����2�� ��&���������������������������������������������$���������������������������������%�

/ / Cr eat e a s i ngl et on set wi t h t he el ement t o r emove. Set f i shBone = Col l ect i ons. s i ngl et on( bone) ; / / bone i s a f i sh par t . / / Remove t he el ement f i sh. r emoveAl l ( f i shBone) ; / / f i sh i s a set of f i sh par t s.

������ �$����&������� �$�����&����������� �$��� �����������������$���������� �������������%�

Col l ect i ons. EMPTY_SET Col l ect i ons. EMPTY_LI ST Col l ect i ons. EMPTY_MAP

�����������������������������$� ���������������������� �������������������������� � ��������

Other Utility Methods in the Collections Class

����������������� ����Li st &� ��������� �� �����������������$�Col l ect i on���-������/���������$���$�� ���������������������������������������������������

st at i c voi d copy( Li st dst , Li st sr c)

.����������������������������������������������������

st at i c voi d f i l l ( Li st l i s t , Obj ect o)

(� �������������������������������������� ��������� �����������������

st at i c Li st nCopi es( i nt n, Obj ect o)

B������������������������� ����n��� ������������ ����������-�����

st at i c voi d r ever se( Li st l i s t )

(�����������������������������������������������

st at i c Compar at or r ever seOr der ( )

(������������ ����������������������������������������������������������'������������������������-���������������*��������������������������������������������$���

Page 55 of 61

6/16/2006file://C:\Documents and Settings\cdot\Local Settings\Temp\~hhAFF6.htm

Page 56: Khalid Mughal Chapter 11

���������� ������������-������ ���������������������������)��� ����KK�nul l ���������%�

Li st i t emsLi st = new Ar r ayLi st ( Col l ect i ons. nCopi es( 99, nul l ) ) ;

���������� ��������������������I nt eger�������������$����������������������������������������������*��2������ �����������&���� �������$%�

Col l ect i ons. sor t ( i nt Li st , Col l ect i ons. r ever seOr der ( ) ) ; Ar r ays. sor t ( st r Ar r ay, Col l ect i ons. r ever seOr der ( ) ) ;

������������������������� �������� ���������������������������������������������%�

Col l ect i on i nt Set = new Tr eeSet ( Col l ect i ons. r ever seOr der ( ) ) ; i nt Set . add( new I nt eger ( 9) ) ; i nt Set . add( new I nt eger ( 11) ) ; i nt Set . add( new I nt eger ( - 4) ) ; i nt Set . add( new I nt eger ( 1) ) ; Syst em. out . pr i nt l n( i nt Set ) ; / / [ 11, 9, 1, - 4]

st at i c voi d shuf f l e( Li st l i s t )

(������$� ����������������&��������&����������������������

bool ean r epl aceAl l ( Li st l i s t , Obj ect ol dVal , Obj ect newVal )

(� ����������������������������ol dVal � ����newVal ������������7���������t r ue������������� �������������

st at i c voi d r ot at e( Li st l i s t , i nt di st ance)

(���������������������� ��������������������������$������ ������������������.���������������� ������������� ���������������������������

st at i c voi d swap( Li st l i s t , i nt i , i nt j )

, � ��������������������������i �����j ��

���������������������������$������������������������������������&��������&��������������������������������� �����������������������������������������������������5������ �������������������������������� ������������������������������������

/ / i nt Li st denot es t he f ol l owi ng l i s t : [ 9, 11, - 4, 1, 7] Col l ect i ons. r ot at e( i nt Li st , 2) ; / / Two t o t he r i ght . [ 1, 7, 9, 11, - 4] Col l ect i ons. r ot at e( i nt Li st , - 2) ; / / Two t o t he l ef t . [ 9, 11, - 4, 1, 7] Li st i nt Subl i s t = i nt Li st . subLi st ( 1, 4) ; / / Subl i s t : [ 11, - 4, 1] Col l ect i ons. r ot at e( i nt Subl i s t , - 1) ; / / One t o t he l ef t . [ - 4, 1, 11] / / i nt Li st i s now: [ 9, - 4, 1, 11, 7]

Utility Methods in the Arrays Class

����Ar r ays������� ���������������������$��������������� �������������$�%������$�������&��������&�����$���� ������&�����$����������

Page 56 of 61

6/16/2006file://C:\Documents and Settings\cdot\Local Settings\Temp\~hhAFF6.htm

Page 57: Khalid Mughal Chapter 11

����Ar r ays������������ �������������������asLi st ( ) �������&� ���������������������������Li st ���� ���������$���B��������������Li st ���� ��������������������$&���������������������Li st �����������������������$���������$������Li st ���)����������������������$�����������������������������������asLi st ( ) ���������������Ar r ays���������������t oAr r ay( ) ���������������Col l ect i on����������� ����������������������������������� ��������$�������������������

st at i c Li st asLi st ( Obj ect [ ] backi ngAr r ay)

�St r i ng[ ] j i veAr r ay = new St r i ng[ ] { " j ava" , " j i ve" , " j ava" , " j i ve" } ; Set j i veSet = new HashSet ( Ar r ays. asLi st ( j i vear r ay) ) ; / / ( 1) St r i ng[ ] uni queJi veAr r ay = ( St r i ng[ ] ) j i veSet . t oAr r ay( new St r i ng[ 0] ) ; / / ( 2)

.��!1"&�����j i veAr r ay���������������������Li st &� ����&��������&���������������������Set ��.��!3"���������������������t oAr r ay( ) ��������� �������������$ �������������$�������������������������������������������$�uni queJi veAr r ay�������������������� ���������

Abstract Implementations

�������������������������� ��������������������j ava. ut i l � ����������������������������� ������������!����0�������11�3�����11�4"��0����2�� ��&�����HashSet ��� ����������������������������Abst r act Set ��� �����������&� ����&��������&��2����������Abst r act Col l ect i on��� �������������������abst r act ���������������$� ���������������������������$��$��� ��������������������������������������������&����������2����������������� ������������ �����������������)����������������

����������� ����������� �

Review Questions

���' D����������������-��������������$����� �����������������������equal s( ) ���������hashCode( )

��������� �� ������$&� ����������������������� ���������������������� ����������@�

St r i ng f unc( Obj ect x, Obj ect y) { r et ur n ( x == y) + " " + x. equal s( y) + " " + ( x. hashCode( ) == y. hashCode( ) ) ;}

,����������� ������������� �����

� " f al se f al se t r ue" �

� " f al se t r ue f al se" �

Page 57 of 61

6/16/2006file://C:\Documents and Settings\cdot\Local Settings\Temp\~hhAFF6.htm

Page 58: Khalid Mughal Chapter 11

� " f al se t r ue t r ue" �

� " t r ue f al se f al se" �

� " t r ue f al se t r ue" �

���- #��������������������equal sI mpl ( ) �������������������� ������������������� �������������������equal s( ) ���������

publ i c c l ass Pai r { i nt a, b; publ i c Pai r ( i nt a, i nt b) { t hi s. a = a; t hi s. b = b; } publ i c bool ean equal s( Obj ect o) { r et ur n ( t hi s == o) | | ( o i nst anceof Pai r ) && equal sI mpl ( ( Pai r ) o) ; } pr i vat e bool ean equal sI mpl ( Pai r o) { / / . . . PROVI DE I MPLEMENTATI ON HERE . . . } }

,��������������������������� �����

� r et ur n a == o. a | | b == o. b; �

� r et ur n f al se; �

� r et ur n a >= o. a; �

� r et ur n a == o. a; �

� r et ur n a == o. a && b == o. b; �

���. +������������������ ���������������������*����@�

,������������������������� ����

� Ar r ayLi st �

� HashSet �

� Vect or �

� Tr eeSet �

� Li nkedLi st �

Page 58 of 61

6/16/2006file://C:\Documents and Settings\cdot\Local Settings\Temp\~hhAFF6.htm

Page 59: Khalid Mughal Chapter 11

���/ +���������� �������������������� �������������������hashCode( ) �������������������� ���� ������@�

i mpor t j ava. ut i l . * ; publ i c c l ass Measur ement { i nt count ; i nt accumul at ed; publ i c Measur ement ( ) { } publ i c voi d r ecor d( i nt v) { count ++; accumul at ed += v; } publ i c i nt aver age( ) { r et ur n accumul at ed/ count ; } publ i c bool ean equal s( Obj ect ot her ) { i f ( t hi s == ot her ) r et ur n t r ue; i f ( ! ( ot her i nst anceof Measur ement ) ) r et ur n f al se; Measur ement o = ( Measur ement ) ot her ; i f ( count ! = 0 && o. count ! = 0) r et ur n aver age( ) == o. aver age( ) ; r et ur n count == o. count ; } publ i c i nt hashCode( ) { / / . . . PROVI DE I MPLEMENTATI ON HERE . . . } }

,����������� ������������� �����

� r et ur n 31337; �

� r et ur n accumul at ed / count ; �

� r et ur n ( count << 16) ^ accumul at ed; �

� r et ur n ~accumul at ed; �

� r et ur n count == 0 ? 0 : aver age( ) ; �

���������� ����������� �

Chapter Summary

Page 59 of 61

6/16/2006file://C:\Documents and Settings\cdot\Local Settings\Temp\~hhAFF6.htm

Page 60: Khalid Mughal Chapter 11

��������� ���������������� ����������������������� ���%�

� ���������� ������������������������� �����������j ava. ut i l � ������%����������������������������� ��������������

� ������������������������������$�� ���������$�����Col l ect i on������������������������������������������������ �����

� ������������������&��� �������������������$�������������$�����Set ����������������� ����������$�HashSet �����Li nkedHashSet ��

� �������������������&��� �������������������$�������������$�����Li st ����������������� ����������$�Ar r ayLi st &�Vect or&�����Li nkedLi st ��

� ���������������� �&��� �������������������$�������������$�����Map����������������� ����������$�HashMap&�Li nkedHashMap&�����Hasht abl e��

� ����������������Compar at or �����Compar abl e��������������������������������������

� ��������������������������������������� �&��� �������������������$�������������$�����Sor t edSet �����Sor t edMap������������������ ����������$�Tr eeSet �����Tr eeMap��

� �������)���������������������$�������)��������������������������$��

� ���������� ����������$����������������������Col l ect i ons�����Ar r ays������������������ ����������� �

Programming Exercises

��� +���������������������������������������������������������������������������������������������#������2 �������������������� ����������������������������������$���� �����������������������������������,������������������� �����������������������������&������������������������������������&��������� ��������������������������������� ��������$������������&���� ������� �$�����������������������������'��������������������� �� ������ �� �������

��� +������� ������� ���������������������������������������������������������������!����&� ���������������������� ����������������"��(��������������������������������������

(����������� ������%�

>j ava Concor dance Hel l o Wor l d { d=[ 9] , o=[ 4, 6] , r =[ 7] , W=[ 5] , H=[ 0] , l =[ 2, 3, 8] , e=[ 1] }

Page 60 of 61

6/16/2006file://C:\Documents and Settings\cdot\Local Settings\Temp\~hhAFF6.htm

Page 61: Khalid Mughal Chapter 11

���������� �

Page 61 of 61

6/16/2006file://C:\Documents and Settings\cdot\Local Settings\Temp\~hhAFF6.htm