280 likes | 391 Views
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010. Generics and Collections. “Never recreate from your memory. Always imagine new places .” -- Dom Cobb, Inception .
E N D
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Generics and Collections “Never recreate from your memory. Always imagine new places.” -- Dom Cobb, Inception Course Lecture Slides19th July 2010 GaneshViswanathan
Collection • A container object that groups multiple objects into a single unit • Example: Arrays • Related Classes/Interfaces together form the Java Collection Framework • Part of java.util package
Recall: Arrays • Hold several objects (called elements) of the same type • Static in size– once their size is set, it can not be changed (expanded/shrunk) • Issues: • you have to decide apriori how many elements you need to store • Inserting/removing element at/from a particular index is cumbersome
java.util.ArrayList • Like arrays, hold several values (called elements) • Unlike arrays • Size need not be specifiedat the time that the collection object is first created • Will automatically grow in size as new items are added (true of virtually all collections besides arrays) • Provides methods to perform operations that arrays don’t provide.
ArrayList Example import java.util.ArrayList; ArrayList numbers = new ArrayList(); // no type or size specified //designed to hold objects of type Object numbers.add(new Integer(7)); numbers.add(3); //auto-boxing; same as numbers.add(new Integer(3)); int sum = 0; int number = 0; for (inti = 0; i < numbers.size(); i++) { number = (Integer)numbers.get(i); // cast is required; why? // auto-unboxing sum += number; }
ArrayList Example import java.util.*; ArrayList numbers = new ArrayList(); numbers.add(3); numbers.add(7); numbers.add(“Test”); // will compile int sum = 0; int number = 0; for (int i = 0; i < numbers.size(); i++) { number = (Integer)numbers.get(i); sum += number; // runtime error when i= 2 }
Generic ArrayList (introduced in Java 5) • The type of objects it can hold is specified at the time of declaration ArrayList<Integer> numbers = new ArrayList<Integer>();
Example import java.util.*; ArrayList<Integer> numbers = new ArrayList<Integer>(); numbers.add(3); numbers.add(7); numbers.add("Test"); // won't compile – prevents runtime error int sum = 0; int number = 0; for (int i = 0; i < numbers.size(); i++) { number = numbers.get(i) // no cast needed sum += number; }
ArrayList Implementation in Java Collections Framework • The elements of the ArrayList are stored in an array • This array is a private member • This array has an initial capacity • Which is either specified in constructor • Otherwise is set to a default value • When the number of elements in the list exceeds the capacity, the internal array is replaced by a bigger one
Efficiency of ArrayLists • ArrayList • Reallocation of underlying array required when the array is full and you need to add more elements • Add and remove operations are costly because they require shifting of elements in the underlying array • => Linked lists overcomes this
Singly-linked list • Consists of a number of nodes chained together • Each node in a linked list stores • Data (or element) • Link (or reference) to the next node
Limitation of a single-linked list • Can traverse the list only in the forward direction
Doubly-linked lists • Removes the limitation of singly-linked lists • by adding a reference in each node to the previous node DoublyLinkedList head tail
The LinkedList<E> Class • Implements a double-linked list
Stack • Represents something like a stack of books on a table • Last-in-first-out structure • You can add, or remove an element only from the top of the stack. • Main Operations: • push • pop • peek
java.util.Stack<E> • JAVA API does not implement a “pure” stack • Extends Vector<E> which is like an ArrayList • Hence allows access of elements at any index
Other Collections • Queue<E> • first-in/first-out data structure • Set<E> • Allows no duplicates
The Collections Class • Consists of static methods that operate on or • return collections • Some useful methods: • sort, binarySearch, min, max, reverse • For details: see http://java.sun.com/javase/6/docs/api/java/util/Collections.html
Comparable<T> interface • Only one method: intcompareTo(T o) public class Student implements Comparable<Student>{ String id;String name;double gpa; public int compareTo(Student s) { return this.name.compareTo(s.name); } } main(…) { Student s1 = new Student(); Student s2 = new Student(); System.out.println(s1.compareTo(s2)); //ok System.out.println(s1.compareTo(“aaa”)); // compiler error }
Comparator<T> interface • intcompare(T o1, T o2) • Compares its two arguments for order. Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.
Get more info! • Java docs: Collections • http://download.oracle.com/docs/cd/E17476_01/javase/1.5.0/docs/api/java/util/Collection.html • Java Tutorial on Collectionshttp://download.oracle.com/docs/cd/E17409_01/javase/tutorial/collections/ • Java docs: Genericshttp://download.oracle.com/docs/cd/E17476_01/javase/1.5.0/docs/guide/language/generics.html