COMP 121 Week 8: Generic Collections. Objectives To understand type variables and how they are used in generic programming To be able to implement and.

Slides:



Advertisements
Similar presentations
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Advertisements

ITEC200 Week04 Lists and the Collection Interface.
Generics, Lists, Interfaces
INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
Generics and the ArrayList Class
Generic programming in Java
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter 17 – Generic Programming.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 18 – Generic Classes.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
ITEC200 – Week03 Inheritance and Class Hierarchies.
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
Class template Describing a generic class Instantiating classes that are type- specific version of this generic class Also are called parameterized types.
Chapter 14 Generics and the ArrayList Class Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
© The McGraw-Hill Companies, 2006 Chapter 17 The Java Collections Framework.
Building Java Programs Inner classes, generics, abstract classes reading: 9.6, 15.4,
Chapter 10 2D Arrays Collection Classes. Topics Arrays with more than one dimension Java Collections API ArrayList Map.
CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.
CS221 - Computer Science II Polymorphism 1. CS221 - Computer Science II Polymorphism 2 Outline  Explanation of polymorphism.  Using polymorphism to.
Chapter 19 Java Data Structures
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
COMP 121 Week 14: Queues. Objectives Learn how to represent a queue Learn how to use the methods in the Queue interface Understand how to implement the.
CS2110: SW Development Methods Textbook readings: MSD, Chapter 8 (Sect. 8.1 and 8.2) But we won’t implement our own, so study the section on Java’s Map.
COMP 121 Week 7: Object-Oriented Design and Efficiency of Algorithms.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Marcus Frean.
COMP 121 Week 13: Stacks. Objectives Learn about the stack data type and how to use its four methods: push, pop, peek, and empty Understand how Java implements.
Chapter 21 Generics 1. Generics - Overview Generic Methods specify a set of related methods Generic classes specify a set of related types Software reuse.
The Java Collections Framework (Part 2) By the end of this lecture you should be able to: Use the HashMap class to store objects in a map; Create objects.
Lists Ellen Walker CPSC 201 Data Structures Hiram College.
Big Java Chapter 16.
Chapters (ArrayList / Generic)
Generics and Collections. Introduction Generics New feature of J2SE 5.0 Provide compile-time type safety Catch invalid types at compile time Generic methods.
Chapter 14 Generics and the ArrayList Class Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights.
CSS446 Spring 2014 Nan Wang  Java Collection Framework ◦ LinkedList ◦ Set ◦ Map 2.
The Java Collections Framework By the end of this lecture you should be able to: use the ArrayList class to store a list of objects; use the HashSet class.
15440 Distributed Systems Recitation 1 Objected-Oriented Java Programming.
1 Generics Chapter 21 Liang, Introduction to Java Programming.
COP INTERMEDIATE JAVA Data Structures. A data structure is a way of organizing a collection of data so that it can be manipulated effectively. A.
Aug 9, CMSC 202 ArrayList. Aug 9, What’s an Array List ArrayList is  a class in the standard Java libraries that can hold any type of object.
CIS 270—Application Development II Chapter 18-Generics.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
COMP 121 Week 11: Linked Lists.
PROGRAMMING IN C#. Collection Classes (C# Programming Guide) The.NET Framework provides specialized classes for data storage and retrieval. These classes.
©SoftMoore ConsultingSlide 1 Generics “Generics constitute the most significant change in the Java programming language since the 1.0 release.” – Cay Horstmann.
Java Generics. It is nice if we could write a single sort method that could sort array of any type of elements: – Integer array, – String array, Solution:
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Thomas Kuehne.
1 CSC 2053 New from AutoBoxing 3 Before J2SE 5.0, working with primitive types required the repetitive work of converting between the primitive.
CSE 1201 Object Oriented Programming ArrayList 1.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Chapter 22 Generic Programming. Chapter Goals To understand the objective of generic programming To be able to implement generic classes and methods To.
Generics Starring: Rite-Aid Aspirin Co-Starring: CVS Paper Plates.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Marcus Frean.
Collections Dwight Deugo Nesa Matic
Array: Sequence of values of the same type Construct array: new double[10] Store in variable of type double[] : double[] data = new double[10]; When array.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
19-Mar-16 Collections and ArrayLists.. 2 Collections Why use Collections. Collections and Object-Orientation. ArrayLists. Special Features. Creating ArrayLists.
Java Generics. Lecture Objectives To understand the objective of generic programming To be able to implement generic classes and methods To know the limitations.
Lecture 5:Interfaces and Abstract Classes Michael Hsu CSULA.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
Slides by Donald W. Smith
Sixth Lecture ArrayList Abstract Class and Interface
Unit 1 Hadoop and big data
COMP 121 Week 9: ArrayList.
Abstract Data Types Abstraction is to distill a system to its most fundamental parts. Applying the abstraction paradigm to the design of data structures.
Inheritance Lakshmish Ramaswamy.
Presentation transcript:

COMP 121 Week 8: Generic Collections

Objectives To understand type variables and how they are used in generic programming To be able to implement and use generic classes and methods To introduce generic collections and the Java Collection hierarchy

Type Variables Generic programming: creation of programming constructs that can be used with many different types  In Java, achieved with inheritance or with type variables For example:  Type variables: Java's ArrayList (e.g. ArrayList )  Inheritance: Using Object to enable a data structure to store any type of object A Generic class is declared with a type variable  In an ArrayList, the type variable denotes the element type Horstmann, C. (2008). Big Java (3 rd ed.). New York: John Wiley & Sons. public class ArrayList { public ArrayList() {... } public void add(E element) {... }... }

Type Variables (cont’d) Must be instantiated  The actual type must be supplied when you use the generic class  Can be instantiated with class or interface types ArrayList ArrayList  Cannot use a primitive type as a type variable ArrayList // Wrong!  Use corresponding wrapper class instead ArrayList Supplied type replaces type variable in class interface Type variables make generic code safer and easier to read Horstmann, C. (2008). Big Java (3 rd ed.). New York: John Wiley & Sons.

Instantiating a Generic Class GenericClassName Example: ArrayList HashMap Purpose: To supply specific types for the type variables of a generic class. Horstmann, C. (2008). Big Java (3 rd ed.). New York: John Wiley & Sons.

Generic Methods Generic method: a method with a type variable  Can be defined inside ordinary and generic classes The type variables of a generic method are specified between the modifiers and the method return type No need to instantiate the type variables Horstmann, C. (2008). Big Java (3 rd ed.). New York: John Wiley & Sons.

Generic Methods (cont’d) Non-generic method to print all the String elements in an array public static void print(String[] a) { for (String e : a) System.out.print(e + " "); System.out.println(); } Generic method to print all the elements in an array public static void print(E[] a) { for (E e : a) System.out.print(e + " "); System.out.println(); } Horstmann, C. (2008). Big Java (3 rd ed.). New York: John Wiley & Sons.

Conventions for Type Variable Names Type VariableName Meaning EElement type in a collection KKey type in a map VValue type in a map TGeneral type S, UAdditional general types Horstmann, C. (2008). Big Java (3 rd ed.). New York: John Wiley & Sons.

Generic Collections Generics allow you to define a collection that contains references to objects of a specific type ArrayList myList = new ArrayList (); specifies that myList is a List of String where String is a type parameter Only references to objects of type String can be stored in myList, and all items retrieved would be of type String A type parameter is analogous to a method parameter Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

Creating a Generic Collection Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

The Collection Hierarchy Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

Common Features of Collections Collection interface specifies a set of common methods Fundamental features include:  Collections grow as needed  Collections hold references to objects that can be accessed using an Iterator object  Collections have at least two constructors (one to create an empty collection and one to make a copy of an existing collection)  Collections override equals, hashCode, and toString (inherited from Object) in a reasonable way that includes the elements they contain  Collections are considered “unordered,” meaning that there is no guarantee about where in a collection an added element will be placed Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

Some Collection Methods Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

Summary In Java, generic programming can be achieved with inheritance or with type variables A generic class has one or more type variables Type variables can be instantiated with class or interface types Type variables make generic code safer and easier to read Type variables of a generic class follow the class name and are enclosed in angle brackets Horstmann, C. (2008). Big Java (3 rd ed.). New York: John Wiley & Sons.

Summary (continued) Type variables can be used for fields, method parameters, and return values Type variables of a generic method are specified between the modifiers and the return type Type variables do not need to be instantiated when calling a generic method Generics are used in the Java Collection framework to define a collection that contains references to objects of a specific type Horstmann, C. (2008). Big Java (3 rd ed.). New York: John Wiley & Sons.

Any Questions?