Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming Lecture 5 The Collections API.
Advertisements

Introduction to Computation and Problem Solving Class 32: The Java® Collections Framework Prof. Steven R. Lerman and Dr. V. Judson Harward.
Transparency No. 1 Java Collection API : Built-in Data Structures for Java.
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
JAVA Programming (Session 7) When you are willing to make sacrifices for a great cause, you will never be alone. Instructor:
Collections Framework A very brief look at Java’s Collection Framework David Davenport May 2010.
Java Collections Framework COMP53 Oct 24, Collections Framework A unified architecture for representing and manipulating collections Allows collections.
1 L43 Collections (3). 2 OBJECTIVES  To use the collections framework interfaces to program with collections polymorphically.  To use iterators to “walk.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L16 (Chapter 22) Java Collections.
Sets and Maps Chapter 9. Chapter 9: Sets and Maps2 Chapter Objectives To understand the Java Map and Set interfaces and how to use them To learn about.
1 L41 Collections (1). 2 OBJECTIVES  What collections are.  To use class Arrays for array manipulations.  To use the collections framework (prepackaged.
24-Jun-15 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
© The McGraw-Hill Companies, 2006 Chapter 17 The Java Collections Framework.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L15 (Chapter 22) Java Collections.
Introducing Hashing Chapter 21 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 20 Lists, Stacks, Queues, and Priority.
Chapter 19 Java Data Structures
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 22 Lists, Stacks, Queues, and Priority.
Java Collections. Collections, Iterators, Algorithms CollectionsIteratorsAlgorithms.
CSE373 Optional Section Java Collections 11/12/2013 Luyi Lu.
Java's Collection Framework
SEG4110 – Advanced Software Design and Reengineering TOPIC G Java Collections Framework.
Java Collections Framework A presentation by Eric Fabricant.
Java Programming: Advanced Topics 1 Collections and Wealth of Utilities.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Collections F The limitations of arrays F Java Collection Framework hierarchy  Use the Iterator interface to traverse a collection  Set interface, HashSet,
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Collections in Java. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection with no duplicates SortedSet.
1 Java's Collection Framework By Rick Mercer with help from The Java Tutorial, The Collections Trail, by Joshua BlockThe Collections Trail.
Jan 12, 2012 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
GENERIC COLLECTIONS. Type-Wrapper Classes  Each primitive type has a corresponding type- wrapper class (in package java.lang).  These classes are called.
111 © 2002, Cisco Systems, Inc. All rights reserved.
Some Other Collections: Bags, Sets, Queues and Maps COMP T2 Lecture 4 School of Engineering and Computer Science, Victoria University of Wellington.
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.
Chapter 18 Java Collections Framework
Data structures Abstract data types Java classes for Data structures and ADTs.
Collections –data structures and Algorithms L. Grewe.
Android How to Program, 2/e © Copyright by Pearson Education, Inc. All Rights Reserved.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Generics and Collections Course Lecture Slides 19 th July 2010 “Never.
Collections Data structures in Java. OBJECTIVE “ WHEN TO USE WHICH DATA STRUCTURE ” D e b u g.
Copyright 2010 by Pearson Education Building Java Programs Chapter 10, 11 Lecture 22: 143 Preview optional reading: 10.1,
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 20 Lists, Stacks, Queues, and Priority.
Collections Mrs. C. Furman April 21, Collection Classes ArrayList and LinkedList implements List HashSet implements Set TreeSet implements SortedSet.
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.
1 Java's Collection Framework Map and Sets. 2 Collection Framework  A collections framework is a unified architecture for representing and manipulating.
CMSC 202 Containers and Iterators. Container Definition A “container” is a data structure whose purpose is to hold objects. Most languages support several.
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Object Oriented Programming and Data Abstraction Rowan University Earl Huff.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 21 Sets and Maps.
Arrays (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
Sets and Maps Chapter 9. Chapter Objectives  To understand the Java Map and Set interfaces and how to use them  To learn about hash coding and its use.
Collections Dwight Deugo Nesa Matic
3-1 Java's Collection Framework Another use of polymorphism and interfaces Rick Mercer.
Chapter  Array-like data structures  ArrayList  Queue  Stack  Hashtable  SortedList  Offer programming convenience for specific access.
(C) 2010 Pearson Education, Inc. All rights reserved. Java How to Program, 8/e.
CS202 Java Object Oriented Programming Introduction to Collection Classes Chengyu Sun California State University, Los Angeles.
Introduction to Java Collection. Java Collections What are they? –A number of pre-packaged implementations of common ‘container’ classes, such as LinkedLists,
©Karsten Lundqvist Example: Array and Arrays 1 import java.util.*; public class Example { private int intValues[] = { 1, 2, 3, 4, 5, 6 }; private double.
Strings, StringBuilder, and Character
Chapter 19 Java Data Structures
Chapter 20 Lists, Stacks, Queues, and Priority Queues
Road Map CS Concepts Data Structures Java Language Java Collections
CS313D: Advanced Programming Language
Chapter 20 Lists, Stacks, Queues, and Priority Queues
Object Oriented Programming in java
Collections Framework
Chapter 16 Generic Collections
Chapter 20 Lists, Stacks, Queues, and Priority Queues
Presentation transcript:

Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.

 Data structures  Collections of related data items.  Arrays  Data structures consisting of related data items of the same type.  Make it convenient to process related groups of values.  Remain the same length once they are created.  Common array manipulations with static methods of class Arrays from the java.util package.  ArrayList collection  Similar to arrays  Dynamic resizing  They automatically increase their size at execution time to accommodate additional elements © Copyright by Pearson Education, Inc. All Rights Reserved.

7.4 Array Example

 Arrays class  Provides static methods for common array manipulations.  Methods include  sort for sorting an array (ascending order by default)  binarySearch for searching a sorted array (need to sort it first)  equals for comparing arrays  fill for placing values into an array.  Methods are overloaded for primitive-type arrays and for arrays of objects.  System class static arraycopy method  Copies contents of one array into another. © Copyright by Pearson Education, Inc. All Rights Reserved.

 Java API provides several predefined data structures, called collections, used to store groups of related objects.  Each provides efficient methods that organize, store and retrieve your data without requiring knowledge of how the data is being stored.  Reduce application-development time.  Arrays do not automatically change their size at execution time to accommodate additional elements.  ArrayList (package java.util ) can dynamically change its size to accommodate more elements.  T is a placeholder for the type of element stored in the collection.  This is similar to specifying the type when declaring an array, except that only nonprimitive types can be used with these collection classes.  Classes with this kind of placeholder that can be used with any type are called generic classes. © Copyright by Pearson Education, Inc. All Rights Reserved.

 Figure 7.23 demonstrates some common ArrayList capabilities.  An ArrayList ’s capacity indicates how many items it can hold without growing.  When the ArrayList grows, it must create a larger internal array and copy each element to the new array.  This is a time-consuming operation. It would be inefficient for the ArrayList to grow each time an element is added.  An ArrayList grows only when an element is added and the number of elements is equal to the capacity—i.e., there is no space for the new element. © Copyright by Pearson Education, Inc. All Rights Reserved.

 Method add adds elements to the ArrayList.  One-argument version appends its argument to the end of the ArrayList.  Two-argument version inserts a new element at the specified position.  Collections indices start at zero.  Method size returns the number of elements in the ArrayList.  M ethod get obtains the element at a specified index.  Method remove deletes an element with a specific value.  An overloaded version of the method removes the element at the specified index.  Method contains determines if an item is in the ArrayList. © Copyright by Pearson Education, Inc. All Rights Reserved.

Java How to Program, 9/e Instructor: José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.

 Java collections framework  prebuilt data structures  interfaces and methods for manipulating those data structures  A collection is a data structure that can hold references to other objects.  Usually, collections contain references to objects that are all of the same type.  Figure 20.1 lists some of the interfaces of the collections framework.  Package java.util. © Copyright by Pearson Education, Inc. All Rights Reserved.

 Each primitive type has a corresponding type-wrapper class (in package java.lang ).  Boolean, Byte, Character, Double, Float, Integer, Long and Short.  Each type-wrapper class enables you to manipulate primitive-type values as objects.  Collections cannot manipulate variables of primitive types.  They can manipulate objects of the type-wrapper classes, because every class ultimately derives from Object. © Copyright by Pearson Education, Inc. All Rights Reserved.

 Each of the numeric type-wrapper classes— Byte, Short, Integer, Long, Float and Double — extends class Number.  The type-wrapper classes are final classes, so you cannot extend them.  Primitive types do not have methods, so the methods related to a primitive type are located in the corresponding type-wrapper class. © Copyright by Pearson Education, Inc. All Rights Reserved.

 A boxing conversion converts a value of a primitive type to an object of the corresponding type-wrapper class.  An unboxing conversion converts an object of a type-wrapper class to a value of the corresponding primitive type.  These conversions can be performed automatically (called autoboxing and auto-unboxing).  Example:  // create integerArray Integer[] integerArray = new Integer[ 5 ]; // assign Integer 10 to integerArray[ 0 ] integerArray[ 0 ] = 10; // get int value of Integer int value = integerArray[ 0 ]; © Copyright by Pearson Education, Inc. All Rights Reserved.

 Interface Collection is the root interface from which interfaces Set, Queue and List are derived.  Interface Collection contains bulk operations for adding, clearing and comparing objects in a collection.  A Collection can be converted to an array.  Class Collections provides static methods that search, sort and perform other operations on collections. © Copyright by Pearson Education, Inc. All Rights Reserved.

 A List (sometimes called a sequence) is a Collection that can contain duplicate elements.  List indices are zero based.  List also provides methods for manipulating elements via their indices, manipulating a specified range of elements, and searching for elements.  Interface List is implemented by several classes, including ArrayList, LinkedList and Vector. © Copyright by Pearson Education, Inc. All Rights Reserved.

 List method addAll appends all elements of a collection to the end of a List.  String method toUpperCase gets an uppercase version of a String.  String method toLowerCase returns a lowercase version of a String.  List method subList obtains a portion of a List.  This is a so-called range-view method, which enables the program to view a portion of the list. © Copyright by Pearson Education, Inc. All Rights Reserved.

 List method clear remove the elements of a List.  List method size returns the number of items in the List.  LinkedList method addLast adds an element to the end of a List.  LinkedList method add also adds an element to the end of a List.  LinkedList method addFirst adds an element to the beginning of a List. © Copyright by Pearson Education, Inc. All Rights Reserved.

 Class Collections provides several high- performance algorithms for manipulating collection elements.  The algorithms (Fig. 20.5) are implemented as static methods. © Copyright by Pearson Education, Inc. All Rights Reserved.

 static Collections method binarySearch locates an object in a List.  If the object is found, its index is returned.  If the object is not found, binarySearch returns a negative value.  Method binarySearch determines this negative value by first calculating the insertion point and making its sign negative.  Then, binarySearch subtracts 1 from the insertion point to obtain the return value, which guarantees that method binarySearch returns positive numbers (>= 0) if and only if the object is found.  List MUST be sorted binarySearch to work. © Copyright by Pearson Education, Inc. All Rights Reserved.

 Class Stack in the Java utilities package ( java.util ) extends class Vector to implement a stack data structure.  Stack method push adds an element to the top of the stack.  Stack method pop removes the top element of the stack.  If there are no elements in the Stack, method pop throws an EmptyStackException, which terminates the loop.  Method peek returns the top element of the stack without popping the element off the stack.  Method isEmpty determines whether the stack is empty. © Copyright by Pearson Education, Inc. All Rights Reserved.

 Interface Queue extends interface Collection and provides additional operations for inserting, removing and inspecting elements in a queue.  PriorityQueue orders elements by their natural ordering.  Elements are inserted in priority order such that the highest-priority element (i.e., the largest value) will be the first element removed from the PriorityQueue.  Common PriorityQueue operations are  offer to insert an element at the appropriate location based on priority order  poll to remove the highest-priority element of the priority queue  peek to get a reference to the highest-priority element of the priority queue  clear to remove all elements in the priority queue  size to get the number of elements in the queue. © Copyright by Pearson Education, Inc. All Rights Reserved.

 A Set is an unordered Collection of unique elements (i.e., no duplicate elements).  The collections framework contains several Set implementations, including HashSet and TreeSet.  HashSet stores its elements in a hash table (a data structure that uses a function to compute an index to locate an element)  TreeSet stores its elements in a tree (a data structure that uses a hierarchy to store elements) © Copyright by Pearson Education, Inc. All Rights Reserved.

 The collections framework also includes the SortedSet interface (which extends Set ) for sets that maintain their elements in sorted order.  Class TreeSet implements SortedSet.  TreeSet method headSet gets a subset of the TreeSet in which every element is less than the specified value.  TreeSet method tailSet gets a subset in which each element is greater than or equal to the specified value.  SortedSet methods first and last get the smallest and largest elements of the set, respectively. © Copyright by Pearson Education, Inc. All Rights Reserved.

 Map s associate keys to values.  The keys in a Map must be unique, but the associated values need not be.  If a Map contains both unique keys and unique values, it is said to implement a one-to-one mapping.  If only the keys are unique, the Map is said to implement a many-to-one mapping—many keys can map to one value.  Three of the several classes that implement interface Map are Hashtable, HashMap and TreeMap.  Hashtable s and HashMap s store elements in hash tables, and TreeMap s store elements in trees. © Copyright by Pearson Education, Inc. All Rights Reserved.

 Interface SortedMap extends Map and maintains its keys in sorted order—either the elements’ natural order or an order specified by a Comparator.  Class TreeMap implements SortedMap.  Hashing is a high-speed scheme for converting keys into unique array indices.  A hash table’s load factor affects the performance of hashing schemes.  The load factor is the ratio of the number of occupied cells in the hash table to the total number of cells in the hash table.  The closer this ratio gets to 1.0, the greater the chance of collisions. © Copyright by Pearson Education, Inc. All Rights Reserved.

 Map method containsKey determines whether a key is in a map.  Map method put creates a new entry in the map or replaces an existing entry’s value.  Method put returns the key’s prior associated value, or null if the key was not in the map.  Map method get obtain the specified key’s associated value in the map.  HashMap method keySet returns a set of the keys.  Map method size returns the number of key/value pairs in the Map.  Map method isEmpty returns a boolean indicating whether the Map is empty. © Copyright by Pearson Education, Inc. All Rights Reserved.