Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
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 http://docs.oracle.com/javase/7/docs/api/ 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 = extends <<Interface>> Iterable <<Interface>> Collection <<Interface>> Queue <<Interface>> List <<Interface>> Set <<Interface>> Map <<Interface>> SortedSet <<Interface>> SortedMap Java Collection Classes – Lists, Stacks, Queues <<Interface>> Iterable <<Interface>> Collection <<Interface>> Queue Abstract Collection <<Interface>> List = extends AbstractList = implements AbstractSequentialList LinkedList ArrayList Vector Stack <<Interface>> Iterable Java Collection Classes - Sets <<Interface>> Collection Abstract Collection <<Interface>> Set <<Interface>> SortedSet AbstractSet = extends = implements HashSet TreeSet Java Collection Classes - Maps <<Interface>> Map AbstractMap = extends = implements <<Interface>> SortedMap TreeMap HashMap Declaration and Instantiation List<String> names = new ArrayList<String>(); List<Student> students = new LinkedList<Student>(); // 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<String> names = new ArrayList<String>(); List<Student> students = new LinkedList<Student>(); // 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<element-type> variable-name; List<String> names; Set<Token> statementHandles; Object instantiation: variable-name = class-name<element-type>(); names = new ArrayList<String>(); statementHandles = new HashSet<Token>(); Collections of Numbers are Funky List<Integer> evens = new ArrayList<Integer>(); 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) <<Interface>> Iterable // Queue methods (as well as Collection) public E peek() public E remove() public boolean add(E newElement) <<Interface>> Collection <<Interface>> Queue <<Interface>> List LinkedList Use Queue to Restrict Access // All List and Collection methods apply List<string> list = new LinkedList<String>(); // Only queue and Collection methods apply Queue<String> queue = new LinkedList<String>(); <<Interface>> Iterable <<Interface>> Collection <<Interface>> Queue <<Interface>> List LinkedList Multiple Type Parameters Variable declaration: interface-name<element-type1, …, element-type n> variable-name; SortedMap<String, Integer> concordance; The keys are all strings and the values are all integers Object instantiation: variable-name = class-name< element-type1, …, element-type n >(); concordance = new TreeMap<String, Integer>(); The Collection Interface <<Interface>> Iterable <<Interface>> Collection <<Interface>> Queue <<Interface>> List A list, queue, set, or sorted set can be used wherever a collection is specified <<Interface>> Set <<Interface>> SortedSet Type Conversion List<String> listOfNames = new ArrayList<String>(); // 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<String> setOfNames = new SortedSet<String>(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(<a List>) Collections.binarySearch(<a List>)