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

Slides:



Advertisements
Similar presentations
Java Programming: Guided Learning with Early Objects
Advertisements

Introduction to Java 2 Programming Lecture 5 The Collections API.
Generics, Lists, Interfaces
Lesson 3 Working with Formulas.
Computer Science 209 Software Development Equality and Comparisons.
1 Various Methods of Populating Arrays Randomly generated integers.
Collections Framework A very brief look at Java’s Collection Framework David Davenport May 2010.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 11: Sorting and Searching  Searching Linear Binary  Sorting.
Chapter 11 Sorting and Searching. Topics Searching –Linear –Binary Sorting –Selection Sort –Bubble Sort.
1 L43 Collections (3). 2 OBJECTIVES  To use the collections framework interfaces to program with collections polymorphically.  To use iterators to “walk.
15-Jun-15 Lists in Java Part of the Collections Framework.
1 L41 Collections (1). 2 OBJECTIVES  What collections are.  To use class Arrays for array manipulations.  To use the collections framework (prepackaged.
Objectives Learn how to implement the sequential search algorithm Explore how to sort an array using the selection sort algorithm Learn how to implement.
CS 106 Introduction to Computer Science I 03 / 07 / 2008 Instructor: Michael Eckmann.
Lists in Java Part of the Collections Framework. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection.
C++ Programming: Program Design Including Data Structures, Second Edition Chapter 22: Standard Template Library (STL)
Data Structures Using C++1 Chapter 13 Standard Template Library (STL) II.
12-Jul-15 Lists in Java Part of the Collections Framework.
 Pearson Education, Inc. All rights reserved Arrays.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 22 Lists, Stacks, Queues, and Priority.
 2004 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - JavaScript: Arrays Outline 11.1 Introduction 11.2 Arrays 11.3 Declaring and Allocating Arrays.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Lambdas and Streams. Functional interfaces Functional interfaces are also known as single abstract method (SAM) interfaces. Package java.util.function.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 24 - Collections Outline 24.1Introduction 24.2Overview 24.3Class Arrays 24.4Interface Collection.
Lists in Python.
Part 2. Searching Arrays Looking for a specific element in an array E.g., whether a certain score (85) is in a list of scores Linear search Binary search.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
INHERITANCE, POLYMORPHISM, CLASS HIERARCHIES AND GENERICS.
Building Java Programs Chapter 13 Searching reading: 13.3.
Sorting Algorithms. Sorting Sorting is a process that organizes a collection of data into either ascending or descending order. public interface ISort.
 2006 Pearson Education, Inc. All rights reserved Searching and Sorting.
 Pearson Education, Inc. All rights reserved Arrays.
 Pearson Education, Inc. All rights reserved Arrays.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Generics and Collections. Introduction Generics New feature of J2SE 5.0 Provide compile-time type safety Catch invalid types at compile time Generic methods.
 Pearson Education, Inc. All rights reserved Collections.
Sorting and Searching Pepper. Common Collection and Array Actions Sort in a certain order ◦ Max ◦ Min Shuffle Search ◦ Sequential (contains) ◦ Binary.
Collections –data structures and Algorithms L. Grewe.
Collections in Java. 2 Collections Hierarchy > ArrayListVector Stack LinkedList > Arrays Collections.
Introduction to LINQ Chapter 11. Introduction Large amounts of data are often stored in a database—an organized collection of data. A database management.
Week # 2: Arrays.  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently  Types of data.
Project 1: Using Arrays and Manipulating Strings Essentials for Design JavaScript Level Two Michael Brooks.
CLUSTERS and ARRAYS. Array Functions  Build an array  Size an array  Form an array from a cluster or a cluster into an array  Index an array  Find.
1 Interfaces in Java’s Collection Framework Rick Mercer.
CS Ananda Gunawardena.  A collection (sometimes called a container) is simply an object that groups multiple elements into a single unit.  Collections.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. Chapter 10 Lists 1.
CSE 143 Lecture 2 More ArrayList ; classes and objects reading: 10.1; slides created by Marty Stepp and Hélène Martin
Priority Queues. Priority Queue ADT A priority queue stores a collection of entries Each entry is a pair (key, value) Main methods of the Priority Queue.
Chapter 10: Class Vector and String, and Enumeration Types Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
University of Limerick1 Collections The Collection Framework.
Arrays (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
3-1 Java's Collection Framework Another use of polymorphism and interfaces Rick Mercer.
 2005 Pearson Education, Inc. All rights reserved Arrays.
Building Java Programs Chapter 13 Sorting reading: 13.3, 13.4.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
Sorting Algorithms. Sorting Sorting is a process that organizes a collection of data into either ascending or descending order. public interface ISort.
Chapter 11.  Large amounts of data are often stored in a database—an organized collection of data.  A database management system (DBMS) provides mechanisms.
Introduction to Java Collection. Java Collections What are they? –A number of pre-packaged implementations of common ‘container’ classes, such as LinkedLists,
Lecture 10 Collections Richard Gesick.
Chapter 10 Lists.
19 Collections.
CS313D: Advanced Programming Language
Chapter 10 Lists.
24 Searching and Sorting.
Chapter 16 Generic Collections
Sorting Algorithms.
Presentation transcript:

1 L42 Collections (2)

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

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

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

5 Fig | Collections algorithms.

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 Outline Sort1.java (1 of 2) Line 15 Create List of String s

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 Outline Sort2.java (1 of 2)

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 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 Outline Sort3.java (1 of 2)

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

Algorithm shuffle shuffle – Randomly orders List elements

15 Outline DeckOfCards.java (1 of 4)

16 Outline DeckOfCards.java (2 of 4)

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 Outline DeckOfCards.java (4 of 4) Program output

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 Outline Algorithms1.java (1 of 3) Line 24 Use method reverse of class Collections to obtain List in reverse order

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 Outline Algorithms1.java (3 of 3) Program output

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 Outline BinarySearchTest.java (1 of 3) Line 18 Sort List in ascending order

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

26 Outline BinarySearchTest.java (3 of 3)

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 Outline Algorithms2.java (1 of 3)

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 Outline Algorithms2.java (3 of 3) Line 45 Invoke method disjoint to test whether Collection s list and vector have elements in common