Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 L42 Collections (2). 2 OBJECTIVES  To use collections framework algorithms to manipulate  search  sort  and fill collections.

Similar presentations


Presentation on theme: "1 L42 Collections (2). 2 OBJECTIVES  To use collections framework algorithms to manipulate  search  sort  and fill collections."— Presentation transcript:

1 1 L42 Collections (2)

2 2 OBJECTIVES  To use collections framework algorithms to manipulate  search  sort  and fill collections.

3 3 19.6 Collections Algorithms Collections framework provides set of algorithms – Implemented as static methods List algorithms – sort – binarySearch – reverse – shuffle – fill – copy

4 4 Collection algorithms – min – max – addAll – frequency – disjoint

5 5 Fig. 19.7 | Collections algorithms.

6 6 19.6.1 Algorithm sort sort – Sorts List elements Order is determined by natural order of elements’ type List elements must implement the Comparable interface Or, pass a Comparator to method sort Sorting in ascending order – Collections method sort Sorting in descending order – Collections static method reverseOrder Sorting with a Comparator – Create a custom Comparator class

7 7 Outline Sort1.java (1 of 2) Line 15 Create List of String s

8 8 Outline Sort1.java (2 of 2) Lines 18 and 23 Line 20 Program output Implicit call to the list ’s toString method to output the list contents Use algorithm sort to order the elements of list in ascending order

9 9 Outline Sort2.java (1 of 2)

10 10 Outline Sort2.java (2 of 2) Line 21 Method reverseOrder of class Collections returns a Comparator object that represents the collection’s reverse order Method sort of class Collections can use a Comparator object to sort a List

11 11 Outline TimeComparator.java Line 5 Line 7 Custom comparator TimeComparator implements Comparator interface and compares Time2 object Implement method compare to determine the order of two Time2 objects

12 12 Outline Sort3.java (1 of 2)

13 13 Outline Sort3.java (2 of 2) Line 23 Program output Sort in order using a custom comparator TimeComparator

14 14 19.6.2 Algorithm shuffle shuffle – Randomly orders List elements

15 15 Outline DeckOfCards.java (1 of 4)

16 16 Outline DeckOfCards.java (2 of 4)

17 17 Outline DeckOfCards.java (3 of 4) Line 55 Line 57 Line 64 Line 65 Use enum type Suit outside of class Card, qualify the enum ’s type name ( Suit ) with the class name Card and a dot (. ) separator Use enum type Face outside of class Card, qualify the enum ’s type name ( Face ) with the class name Card and a dot (. ) separator Use method shuffle of class Collections to shuffle List Invoke static method asList of class Arrays to get a List view of the deck array

18 18 Outline DeckOfCards.java (4 of 4) Program output

19 19 19.6.3 Algorithm reverse, fill, copy, max and min reverse – Reverses the order of List elements fill – Populates List elements with values copy – Creates copy of a List max – Returns largest element in List min – Returns smallest element in List

20 20 Outline Algorithms1.java (1 of 3) Line 24 Use method reverse of class Collections to obtain List in reverse order

21 21 Outline Algorithms1.java (2 of 3) Line 28 Line 32 Line 45 Line 46 Use method copy of class Collections to obtain copy of List Use method fill of class Collections to populate List with the letter ‘R’ Obtain maximum value in List Obtain minimum value in List

22 22 Outline Algorithms1.java (3 of 3) Program output

23 23 19.6.4 Algorithm binarySearch binarySearch – Locates object in List Returns index of object in List if object exists Returns negative value if Object does not exist – Calculate insertion point – Make the insertion point sign negative – Subtract 1 from insertion point

24 24 Outline BinarySearchTest.java (1 of 3) Line 18 Sort List in ascending order

25 25 Outline BinarySearchTest.java (2 of 3) Line 39 Use method binarySearch of class Collections to search list for specified key

26 26 Outline BinarySearchTest.java (3 of 3)

27 27 19.6.5 Algorithms addAll, frequency and disjoint addAll – Insert all elements of an array into a collection frequency – Calculate the number of times a specific element appear in the collection Disjoint – Determine whether two collections have elements in common

28 28 Outline Algorithms2.java (1 of 3)

29 29 Outline Algorithms2.java (2 of 3) Line 31 Line 40 Invoke method addAll to add elements in array colors to vector Get the frequency of String “red” in Collection vector using method frequency

30 30 Outline Algorithms2.java (3 of 3) Line 45 Invoke method disjoint to test whether Collection s list and vector have elements in common


Download ppt "1 L42 Collections (2). 2 OBJECTIVES  To use collections framework algorithms to manipulate  search  sort  and fill collections."

Similar presentations


Ads by Google