Download presentation
Presentation is loading. Please wait.
Published byOwen Jackson Modified over 8 years ago
1
Computer Science 209 Software Development Java Collections
2
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
3
The java.util Package View documentation or download from Oracle’s Web site Includes docs for all interfaces, classes, and methods http://docs.oracle.com/javase/7/docs/api/
4
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
5
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
6
Java Collection Interfaces > Collection > Iterable > List > Set > Map > SortedMap > SortedSet > Queue = extends
7
Java Collection Classes – Lists, Stacks, Queues > Collection > Iterable > List > Queue LinkedListArrayListVector Stack Abstract Collection AbstractList AbstractSequentialList = extends = implements
8
Java Collection Classes - Sets > Collection > Iterable > Set HashSetTreeSet > SortedSet Abstract Collection AbstractSet = extends = implements
9
Java Collection Classes - Maps > Map > SortedMap AbstractMap TreeMapHashMap = extends = implements
10
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
11
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);
12
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 ();
13
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
14
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
15
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
16
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
17
The Collection Interface > Collection > Iterable > List > Set > SortedSet > Queue A list, queue, set, or sorted set can be used wherever a collection is specified
18
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!
19
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( )
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.