1 L 41 Collections 1 2 OBJECTIVES What

1 L 41 Collections (1)

2 OBJECTIVES § What collections are. § To use class Arrays for array manipulations. § To use the collections framework (prepackaged data structure) implementations.

3 19. 1 Introduction • Java collections framework – Contain prepackaged data structures, interfaces, algorithms – Use generics – Use existing data structures • Example of code reuse – Provides reusable componentry

4 19. 2 Collections Overview • Collection – Data structure (object) that can hold references to other objects • Collections framework – Interfaces declare operations for various collection types – Provide high-performance, high-quality implementations of common data structures – Enable software reuse – Enhanced with generics capabilities in J 2 SE 5. 0 • Compile-time type checking

5 Fig. 19. 1 | Some collection framework interfaces.

6 19. 3 Class Arrays • Class Arrays – Provides static methods for manipulating arrays – Provides “high-level” methods • • Method binary. Search for searching sorted arrays Method equals for comparing arrays Method fill for placing values into arrays Method sort for sorting arrays

Outline Using. Arrays. java (1 of 4) Line 17 Line 18 Use static method. Lines fill of class 21 -22 Arrays to populate array with 7 s Use static method sort of class Arrays to sort array’s elements in ascending order Use static method arraycopy of class System to copy array int. Array into array int. Array. Copy 7

Outline Using. Arrays. java (2 of 4) Line 50 Use static method binary. Search of class Arrays to perform binary search on array 8

Outline Use static method equals of class Arrays to determine Using. Arrays. java whether values of the two arrays are equivalent (3 of 4) Line 56 and 60 9

Outline Using. Arrays. java (4 of 4) Program output 10

19. 4 Interface Collection and Class Collections • Interface Collection – Root interface in the collection hierarchy – Interfaces Set, Queue, List extend interface Collection • Set – collection does not contain duplicates • Queue – collection represents a waiting line • List – ordered collection can contain duplicate elements – Contains bulk operations • Adding, clearing, comparing and retaining objects – Provide method to return an Iterator object • Walk through collection and remove elements from collection 11

19. 4 Interface Collection and Class Collections (Cont. ) • Class Collections – Provides static methods that manipulate collections • Implement algorithms for searching, sorting and so on – Collections can be manipulated polymorphically • Synchronized collection • Unmodifiable collection 12

13 19. 5 Lists • List – Ordered Collection that can contain duplicate elements – Sometimes called a sequence – Implemented via interface List • Array. List • Linked. List • Vector

14 19. 5. 1 Array. List and Iterator • Array. List example – Demonstrate Collection interface capabilities – Place two String arrays in Array. Lists – Use Iterator to remove elements in Array. List

Outline Collection. Test. java (1 of 3) Lines 18 -19 Create Array. List objects and assign their references to variable list and remove. List, respectively 15

Outline Use List method add to add objects to list and remove. List, respectively Collection. Test. java Use List method (2 size of 3)to get the number of Array. List elements Lines 23 and 27 Use List method get to Line 32 values retrieve individual element Line 33 Line 36 Method remove. Colors takes two Collections as arguments; Line 36 passes two Lists, which extends Collection, to this method 16

Outline Method remove. Colors allows any Collections containing strings to be passed as arguments to this method Collection. Test. java Obtain Collection iterator (3 of 3) Iterator method has. Next Line the 47 Iterator determines whether contains more elements Line 50 53 returns a Iterator method. Line next reference to the next element Line 55 Collection method contains Line 55 determines whether collection 2 contains the element returned by next Line 56 Use Iterator method remove to remove String from Iterator 17

18 19. 5. 2 Linked. List • Linked. List example – Add elements of one List to the other – Convert Strings to uppercase – Delete a range of elements

Outline List. Test. java (1 of 4) Lines 17 -18 Line 22 Create two Linked. List objects Use List method add to append elements from array colors to the end of list 1 19

Outline Use List method add to append elements from array colors 2 to the end of list 2 List. Test. java Use List method add. All to append all 4) elements of list 2 to the end(2 ofoflist 1 Line 26 Line 28 Line 42 Method print. List allows any Lists containing strings to be passed as arguments to this method 20

Outline Method convert. To. Uppercase. Strings List. Test. java allows any Lists containing strings to be Invoke List method list. Iterator as argumentsiterator to this method topassed get a bidirectional (3 of 4)for the List Invoke List. Iterator method has. Next to determine whether the Invoke List. Iterator method next Line 53 List contains another element Invokethe List. Iterator set to replace the to obtain next String inmethod the List current String to which iterator Line refers 55 with the String returned by method to. Upper. Case Line 57 Linecontaining 59 Method remove. Items allows any Lists strings to method be passed as arguments method Invoke List sub. List to to this Line 60 obtain a portion of the List Line 65 Line 67 Method print. Reversed. List allows any Lists containing strings to be Invoke List method list. Iterator passed as arguments to this method Line 71 with one argument that specifies the starting position to. Line get a 73 bidirectional iterator for the list 21

The while condition calls method Outline has. Previous to determine whethere are more elements while traversing the list backward List. Test. java (4 of 4) Invoke List. Iterator method previous to get the previous Line 78 element from the list Line 79 Program output 22

23 19. 5. 2 Linkedlist (Cont. ) • static method as. List of class Arrays – View an array as a List collection – Allow programmer to manipulate the array as if it were a list – Any modification made through the List view change the array – Any modification made to the array change the List view – Only operation permitted on the view returned by as. List is set

Outline 24 Using. To. Array. java (1 of 2) Call method as. List to create a List Lines 13 -14 view of array colors, which is then used for creating a Linked. List Line 16 Lines 17 -18 Call Linked. List method add. Last to add “red” to the end Line of links 19 Call Linked. List method add to add “pink” as the last element and “green” as the element at index 3 Call Linked. List method add. First to add “cyan” as the new first item in the Linked. List

Outline 25 Use List method to. Array to obtain array representation of. Using. To. Array. java Linked. List (2 of 2) Line 22 Program output

26 19. 5. 3 Vector • Class Vector – Array-like data structures that can resize themselves dynamically – Contains a capacity – Grows by capacity increment if it requires additional space

Outline Vector. Test. java (1 of 4) Line 12 Create Vector of type String with initial capacity Line of 1710 element and capacity increment of zero Call Vector method add to add objects (Strings in this example) to the end of the Vector 27

Outline 28 Vector. Test. java Call Vector method first. Element to return a reference to the first element in the Vector (2 of 4) Call Vector method last. Element to return a Line 24 reference to the last element in the Vector method contains returns Line 25 boolean that indicates whether Line 34 Vector contains a specific Object Vector method removes the first occurrence of its argument Object from Vector method index. Of returns index of first location in Vector containing the argument Line 36 Line 40

Outline Vector. Test. java (3 of 4) Lines 52 and 53 Vector methods size and capacity return number of Line 56 elements in Vector and Vector capacity, Linerespectively 58 Method print. Vector allows any Vectors containing strings to be passed as arguments to this method Vector method is. Empty returns true if there are no elements in the Vector 29

Outline Vector. Test. java (4 of 4) Program output 30
- Slides: 30