Computer Science 209 Software Development Java Collections
Collections A collection is a container for 0 or more objects Organized in a specific manner (list, stack, queue, set, map/dictionary, tree, graph) Operations include insert, remove, access, size, iteration, etc. Arrays are too restrictive, more like an implementation structure; collections are smarter objects
The java.util Package View documentation or download from Oracle’s Web site Includes docs for all interfaces, classes, and methods
Interfaces An interface specifies the behavior of a set of implementing classes Really just a name and a set of method headers The behavior is abstract and conceptually the same, regardless of how it is realized in an implementing class
Example: Lists The List interface includes the methods add, remove, get, and set, among many others The ArrayList and LinkedList classes implement List, so the above methods can be run, in the same manner, with either class of list You just study the interface for the logical behavior, then choose an implementing class based on its performance characteristics
Java Collection Interfaces > Collection > Iterable > List > Set > Map > SortedMap > SortedSet > Queue = extends
Java Collection Classes – Lists, Stacks, Queues > Collection > Iterable > List > Queue LinkedListArrayListVector Stack Abstract Collection AbstractList AbstractSequentialList = extends = implements
Java Collection Classes - Sets > Collection > Iterable > Set HashSetTreeSet > SortedSet Abstract Collection AbstractSet = extends = implements
Java Collection Classes - Maps > Map > SortedMap AbstractMap TreeMapHashMap = extends = implements
Declaration and Instantiation List names = new ArrayList (); List students = new LinkedList (); // Now use the same methods with both lists Always use an interface name for the type of a collection variable The type parameter (in red) restricts the type of objects that can go into a collection Select the methods to use from the interface
All Collections Are Iterable List names = new ArrayList (); List students = new LinkedList (); // Now use the same methods with both lists for (int i = 1; i <= 10; i++) students.add(new Student("Name" + i, 3)); for (Student s : students) System.out.println(s);
Syntax of Parameterized Collections Variable declaration: interface-name variable-name; List names; Set statementHandles; Object instantiation: variable-name = class-name (); names = new ArrayList (); statementHandles = new HashSet ();
Collections of Numbers are Funky List evens = new ArrayList (); for (int i = 1; i <= 10; i++) evens.add(i * 2); for (int i : evens) System.out.println(i); Need to use a wrapper class for the element type when it’s a primitive type Java wraps values before insertion and unwraps them before access
Lists vs Queues // List methods (among many others, including Collection) public E get(int index) public E remove(int index) public E set(int index, E newElement) // Queue methods (as well as Collection) public E peek() public E remove() public boolean add(E newElement) > Collection > Iterable > List > Queue LinkedList
Use Queue to Restrict Access // All List and Collection methods apply List list = new LinkedList (); // Only queue and Collection methods apply Queue queue = new LinkedList (); > Collection > Iterable > List > Queue LinkedList
Multiple Type Parameters Variable declaration: interface-name variable-name; SortedMap concordance; Object instantiation: variable-name = class-name (); concordance = new TreeMap (); The keys are all strings and the values are all integers
The Collection Interface > Collection > Iterable > List > Set > SortedSet > Queue A list, queue, set, or sorted set can be used wherever a collection is specified
Type Conversion List listOfNames = new ArrayList (); // Add a bunch of names to the list in random order // Now copy the names to a new sorted set (sorts them // and removes duplicates) SortedSet setOfNames = new SortedSet (listOfNames); Collection classes that implement Collection usually include a constructor with a Collection parameter!
The java.util.Collections Class Like the Arrays class, includes class methods for processing collections that implement the List interface (array lists, linked lists, stacks) Sorting, searching, find the maximum element, etc. Some methods assume compareto for collection elements Collections.sort( ) Collections.binarySearch( )