Introducing Java Generics and Collections

Slides:



Advertisements
Similar presentations
Generic programming in Java
Advertisements

Java Generics.
Java Collections. Lecture Objectives To understand the concepts of Java collections To be able to implement Java programs based on Collections.
1 Generics and Using a Collection Generics / Parameterized Classes Using a Collection Customizing a Collection using Inheritance Inner Classes Use of Exceptions.
Generic Programming Amit Shabtay. March 3rd, 2004 Object Oriented Design Course 2 The Problem Assume we have a nice Stack implementation. Our stack receives.
Static and Dynamic Behavior Fall 2005 OOPD John Anthony.
1 L39 Generics (1). 2 OBJECTIVES  To create generic methods that perform identical tasks on arguments of different types.  To create a generic Stack.
Slides prepared by Rose Williams, Binghamton University Chapter 16 Collections and Iterators.
Generic Subroutines and Exceptions CS351 – Programming Paradigms.
Generic Java 21/ What is generics? To be able to assign type variables to a class These variables are not bound to any specific type until the.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 17 Animated Version Generics and Type Safety.
Appendix A.2: Review of Java and Object-Oriented Programming: Part 2 “For the object-oriented project, remember that the primary unit of decomposition.
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Java Classes, Interfaces, and Types 1.
Generics In Java 1.5 By Manjunath Beeraladinni. Generics ➲ New feature in JDK1.5. ➲ Generic allow to abstract over types. ➲ Generics make the code clearer.
CSC 142 O 1 CSC 142 Java More About Inheritance & Interfaces [Reading: chapter 13]
Chapter 21 Generics 1. Generics - Overview Generic Methods specify a set of related methods Generic classes specify a set of related types Software reuse.
Effective Java: Generics Last Updated: Spring 2009.
Generics1 Parametrized classes and methods. Generics2 What are generics Generics are classes or interfaces that can be instantiated with a variety of.
Generics1 Parametrized classes and methods. Generics2 What are generics Generics are classes or interfaces that can be instantiated with a variety of.
CMSC 202 Generics. Nov Generalized Code One goal of OOP is to provide the ability to write reusable, generalized code. Polymorphic code using.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Generics1 Parametrized classes and methods. Generics2 What are generics Generics are classes or interfaces that can be instantiated with a variety of.
1 Generics and Using a Collection Generics / Parameterized Classes Using a Collection Customizing a Collection using Inheritance Inner Classes Use of Exceptions.
Generics and Collections. Introduction Generics New feature of J2SE 5.0 Provide compile-time type safety Catch invalid types at compile time Generic methods.
1 Generics Chapter 21 Liang, Introduction to Java Programming.
CS-2851 Dr. Mark L. Hornick 1 Generic Java Classes Implementing your own generic classes.
Lecture 4 Generic programming Advanced Java Programming 1 dr hab. Szymon Grabowski dr inż. Wojciech Bieniecki
CMSC 330: Organization of Programming Languages Java Generics.
1 CSE 331 Generics (Parametric Polymorphism) slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 19 Generics.
CSCE 314 Programming Languages Java Generics II Dr. Hyunyoung Lee 1.
©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:
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Chapter 4Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapters 4 and 5: Excerpts l Class and Method Definitions l Information.
Slides prepared by Rose Williams, Binghamton University Chapter 16 Collections and Iterators.
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
(C) 2010 Pearson Education, Inc. All rights reserved. Java How to Program, 8/e.
OOP Tirgul 10. What We’ll Be Seeing Today  Generics – A Reminder  Type Safety  Bounded Type Parameters  Generic Methods  Generics and Inner Classes.
Sixth Lecture ArrayList Abstract Class and Interface
Computer Engineering Department Islamic University of Gaza
Component Based Software Engineering
Intro To Classes Review
Chapter 20 Generic Classes and Methods
A tree set Our SearchTree class is essentially a set.
Generics, Lambdas, Reflections
Continuing Chapter 11 Inheritance and Polymorphism
Java Generics.
Data Types.
CSE 143 Lecture 9 References and Linked Nodes reading: 3.3; 16.1
Chapter 19 Generics Dr. Clincy - Lecture.
Generics (Parametric Polymorphism)
CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm.
A tree set Our SearchTree class is essentially a set.
Generics.
Generic programming in Java
Chapter 19 Generics.
Chapter 21 Generics.
CISC124 Assignment 4 on Inheritance due today at 7pm.
Collections Not in our text.
More About Inheritance & Interfaces
ArrayLists 22-Feb-19.
CMSC 202 Generics.
Chapter 11 Inheritance and Polymorphism Part 2
Review: libraries and packages
Generics 2-May-19.
Chapter 21 Generics.
Chapter 19 Generics.
Chapter 19 Generics.
Presentation transcript:

Introducing Java Generics and Collections CSC 202

Before Java 1.5 . . . No generics. Collections existed (since 1.2), but the capabilities made possible by generics obviously were not available. 20060808

Generics? What is it (are they)? The ability to pass types as arguments. Aside: What’s the difference between parameters and arguments? To use C++ terminology, Java parameters are similar to formal parameters which appear in the function definition, and arguments are like actual parameters which appear in the function call. 20060808

Popularity of “Generics” A.K.A. Templates Parametric polymorphism Found in Ada C++ C# 20060808

Syntax Change due to Generics Use of angle brackets For example LinkedList <String> Read as “LinkedList of String.” Why not parentheses? To set them apart from variable parameters. 20060808

The Purpose of Generics Two words: type safety. “The behaviors classified as type errors by any given programming language are generally those that result from attempts to perform on some value (or values) an operation that is not appropriate to its type (or their types).” Wikipedia One of the first common-sense principles learned in software engineering . . . The earlier we can uncover a defect, the cheaper it is to correct it. 20060808

Generics Used With Collections The generic feature in Java lets you tell the compiler about the type that you expect to load into a collection class. As an extra benefit, you no longer have to write as many casts. 20060808

Example of Using Generics To Enhance Type Safety Before 1.5, collection classes (such as ArrayList) could contain Objects only. If you intended for an ArrayList to contain only Strings, nothing prevented the accidental insertion of an Integer. Discovery of the unintended Integer (or other non-String) in the ArrayList might not happen until much later, perhaps causing a run-time error. Now, however, the Integer insertion can be reported at compile time! 20060808

ArrayList and other Collections TreeSet HashMap LinkedList HashSet LinkedHashMap Others . . . Each has its own characteristics that may make it uniquely suited for an application. See the Java API for details. p. 533, p. 557, and API (Java Collection Framework) 20060808

Elements of a Generic Class Denoted as “E” or “T” “E” is a stand-in for the type of element you want a collection to hold and return. See ArrayList documentation in API GenericStack.java example (text, p. 688) 20060808

Generic Methods Use type parameter in declaration. public <T extends Animal> void takeThing (ArrayList <T> list) T can be “any type of Animal”. Note: This is not the same as public void takeThing (ArrayList <Animal> list) The first one takes any ArrayList declared as type Animal or any subtype of Animal (e.g., Dog, Cat). The second takes only ArrayLists of Animal type. Another note: Generic types must be reference types (not primitive types). 20060808

Generic Methods See example listing 21.2 (text, p. 690). public <T extends Animal> void takeThing (ArrayList <T> list) T can be “any type of Animal”. Note: This is not the same as public void takeThing (ArrayList <Animal> list) The first one takes any ArrayList declared as type Animal or any subtype of Animal (e.g., Dog, Cat). The second takes only ArrayLists of Animal type. Another note: Generic types must be reference types (not primitive types). 20060808

Raw Types Specification of a generic class without a type parameter. Allowed for backward compatibility. Unsafe. 20060808

Confused yet? In Java generics syntax, extends really means extends or implements. The “?” (wildcard) Use any type in place of the type parameter. Example: public void sampleMethod(String arg1, ArrayList<?> arg2) sampleMethod has two arguments, the first of which must be a String. The second can be an ArrayList<T> with any base type. 20060808

Why use “?” ? The difference is that Equivalent examples: T stands for any subtype, but it can be a specific subtype within the context, and ? stands for any subtype. Equivalent examples: public <T extends Animal> void takeThing (ArrayList<T> one, ArrayList<T> two) Public void takeThing (ArrayList<? extends Animal> one, ArrayList<? extends Animal> two) 20060808

“T” is not a magic letter Note: you may use any nonkeyword identifier for the type parameter; you need not use T. (Savitch, Absolute Java, 2d ed., p. 782) The custom is to start the identifier with an uppercase letter; using a single letter is a weaker tradition. 20060808