Generics A Brief Review 16-Nov-18.

Slides:



Advertisements
Similar presentations
Transparency No. 1 Java Collection API : Built-in Data Structures for Java.
Advertisements

Java Review Interface, Casting, Generics, Iterator.
1-May-15 Java 1.5. Reason for changes “The new language features all have one thing in common: they take some common idiom and provide linguistic support.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter 17 – Generic Programming.
Java Generics.
11-Jun-15 Generics. A generic is a method that is recompiled with different types as the need arises The bad news: Instead of saying: List words = new.
Generic types for ArrayList Old Java (1.4 and older): ArrayList strings = new ArrayList(); strings.enqueue(“hello”); String word = (String) strings.get(0);
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L13 (Chapter 21) Generics.
Compilation 2007 Java 1.5 Features Michael I. Schwartzbach BRICS, University of Aarhus.
30-Jun-15 Generics. Arrays and collections In Java, array elements must all be of the same type: int[] counts = new int[10]; String[] names = { "Tom",
15-Jul-15 Generics. ArrayList s and arrays A ArrayList is like an array of Object s, but... Arrays use [ ] syntax; ArrayList s use object syntax An ArrayList.
16-Jul-15 Java Versions of Java Java 1 Java 2 Java 5.0 Oak: Designed for embedded devices Java 1.1: Adds inner classes and a completely new event-handling.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 17 Animated Version Generics and Type Safety.
16 - Generics. 2 NOEA2009Java-kursus – Generics ”Generic” Programming in C#/Java (as it was until Summer 2005) All classes inherit from Object So we can.
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.
Object Oriented Programming Ders 10: Data Structures Mustafa Emre İlal
Java 5 Part 1 CSE301 University of Sunderland Harry Erwin, PhD.
Generics CompSci 230 S Software Construction.
Types in programming languages1 What are types, and why do we need them?
Today’s Agenda  Generic  Iterators CS2336: Computer Science II.
Generic Instructor : Sarah Alodan. Problem?! Java code used to look like this: public class Courses { public static void main(String[] args) { ArrayList.
CMSC 330: Organization of Programming Languages Java Generics.
GENERIC TYPES AND THE JAVA COLLECTIONS FRAMEWORK Lecture 15 CS2110 – Fall 2013.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 19 Generics.
GENERICS AND THE JAVA COLLECTIONS FRAMEWORK Lecture 16 CS2110 – Fall 2015 Photo credit: Andrew Kennedy.
Generics Generics vs. heterogeneous collections Doing your own generics FEN 2014UCN Teknologi/act2learn1.
11-Jan-16 Bits and Pieces Some random things in Java.
More Java: Static and Final, Abstract Class and Interface, Exceptions, Collections Framework 1 CS300.
1 CSC 2053 New from AutoBoxing 3 Before J2SE 5.0, working with primitive types required the repetitive work of converting between the primitive.
1.Net programmingGenericsNOEA / 2009 Generics 11. Generics in.NET and C#
IMPLEMENTATION OF CLASS EXTENT by Paweł Świetlicki.
1 Chapter 21 Generics. 2 Objectives F To use generic classes and interfaces (§21.2). F To declare generic classes and interfaces (§21.3). F To understand.
19-Mar-16 Collections and ArrayLists.. 2 Collections Why use Collections. Collections and Object-Orientation. ArrayLists. Special Features. Creating ArrayLists.
OOP Tirgul 10. What We’ll Be Seeing Today  Generics – A Reminder  Type Safety  Bounded Type Parameters  Generic Methods  Generics and Inner Classes.
Collections ABCD ABCD Head Node Tail Node array doubly linked list Traditional Arrays and linked list: Below is memory representation of traditional.
EKT472: Object Oriented Programming
Java 1.5 New Features 22-Apr-18.
Sixth Lecture ArrayList Abstract Class and Interface
Software Development Java Collections
ADT’s, Collections/Generics and Iterators
More on Java Generics Multiple Generic Types Bounded Generic Types
Java Generics Lecture 14 CS2110 – Fall 2016
University of Central Florida COP 3330 Object Oriented Programming
Some random things in Java
Building Java Programs
Java Collections Overview
Introduction to Collections
Some random things in Java
Generics (Parametric Polymorphism)
Java Generics Lecture 22 CS2110 – Fall 2018
Introduction to Collections
Generics 27-Nov-18.
CSE 143 Lecture 27: Advanced List Implementation
Lecture 26: Advanced List Implementation
Java Collections Framework
Some random things in Java
Barb Ericson Georgia Institute of Technology June 2006
Collections James Brucker.
Generic Types and the Java Collections Framework
Building Java Programs
ArrayLists 22-Feb-19.
Interator and Iterable
Some random things in Java
Java 5 New Features 1-May-19.
Review: libraries and packages
Generics 2-May-19.
Chapter 19 Generics.
COMP204 Bernhard Pfahringer (with input from Robi Malik)
Chengyu Sun California State University, Los Angeles
Presentation transcript:

Generics A Brief Review 16-Nov-18

Generics A generic is a method that is recompiled with different types as the need arises The bad news: Instead of saying: List words = new ArrayList(); You'll have to say: List<String> words = new ArrayList<String>(); The good news: Replaces runtime type checks with compile-time checks No casting; instead of String title = (String) words.get(i); you use String title = words.get(i); Some classes and interfaces that have been “genericized” are: Vector, ArrayList, LinkedList, Hashtable, HashMap, Stack, Queue, PriorityQueue, Dictionary, TreeMap and TreeSet

Writing generic methods private void printListOfStrings(List<String> list) { for (Iterator<String> i = list.iterator(); i.hasNext(); ) { System.out.println(i.next()); } } This method should be called with a parameter of type List<String>, but it can be called with a parameter of type List The disadvantage is that the compiler won’t catch errors; instead, errors will cause a ClassCastException This is necessary for backward compatibility Similarly, the Iterator need not be an Iterator<String>

Type wildcards Here’s a simple (no generics) method to print out any list: private void printList(List list) { for (Iterator i = list.iterator(); i.hasNext(); ) { System.out.println(i.next()); } } The above still works in Java 1.5, but now it generates warning messages Java 1.5 incorporates lint (like C lint) to look for possible problems You should eliminate all errors and warnings in your final code, so you need to tell Java that any type is acceptable: private void printListOfStrings(List<?> list) { for (Iterator<?> i = list.iterator(); i.hasNext(); ) { System.out.println(i.next()); } }

Writing your own generic types public class Box<T> { private List<T> contents; public Box() { contents = new ArrayList<T>(); } public void add(T thing) { contents.add(thing); } public T grab() { if (contents.size() > 0) return contents.remove(0); else return null; } Sun recommends single capital letters (such as T) for type variables

Generic Iterators To iterate over generic collections, it’s a good idea to use a generic iterator List<String> listOfStrings = new LinkedList<String>(); ... for (Iterator<String> i = listOfStrings.iterator(); i.hasNext(); ) { String s = i.next(); System.out.println(s); } Or use the new for statement: for(type var : array) {...} or for(type var : collection) {...} You can use the new for statement with your own generic types, if they implement the Iterable interface This requires an Iterator<T> iterator() method

Summary If you think of a genericized type as a type, you won’t go far wrong Use it wherever a type would be used ArrayList myList becomes ArrayList<String> myList new ArrayList() becomes new ArrayList<String>() public ArrayList reverse(ArrayList list) becomes public ArrayList<String> reverse(ArrayList<String> list) Advantage: Instead of having collections of “Objects”, you can control the type of object Disadvantage: more complex, more typing

The End