Presentation is loading. Please wait.

Presentation is loading. Please wait.

Software Development Java Collections

Similar presentations


Presentation on theme: "Software Development Java Collections"— Presentation transcript:

1 Software Development Java Collections
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

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
= extends <<Interface>> Iterable <<Interface>> Collection <<Interface>> Queue <<Interface>> List <<Interface>> Set <<Interface>> Map <<Interface>> SortedSet <<Interface>> SortedMap

7 Java Collection Classes – Lists, Stacks, Queues
<<Interface>> Iterable Java Collection Classes – Lists, Stacks, Queues <<Interface>> Collection <<Interface>> Queue Abstract Collection <<Interface>> List = extends AbstractList = implements AbstractSequentialList LinkedList ArrayList Vector Stack

8 Java Collection Classes - Sets
<<Interface>> Iterable <<Interface>> Collection Abstract Collection <<Interface>> Set <<Interface>> SortedSet AbstractSet = extends = implements HashSet TreeSet

9 Java Collection Classes - Maps
<<Interface>> Map AbstractMap = extends <<Interface>> SortedMap TreeMap HashMap = implements

10 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

11 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);

12 Syntax of Parameterized Collections
Variable declaration: interface-name<element-type> variable-name; List<String> names; Set<Token> statementHandles; Map<String, String> table; Object instantiation: variable-name = class-name<element-type>(); names = new ArrayList<String>(); statementHandles = new HashSet<Token>(); table = new HashMap<String, String>();

13 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

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) <<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

15 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

16 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>();

17 The Collection Interface
A list, queue, set, or sorted set can be used wherever a collection is specified <<Interface>> Iterable <<Interface>> Collection <<Interface>> Queue <<Interface>> List <<Interface>> Set <<Interface>> SortedSet

18 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!

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(<a List>) Collections.binarySearch(<a List>)

20 Zipping Lists into a Map
import java.util.*; public class TestZip{ public static Map<String, String> zip(List<String> keys, List<String> values){ Map<String, String> result = new HashMap<>(); for (int i = 0; i < keys.size(); i++) result.put(keys.get(i), values.get(i)); return result; } public static void main(String[] args){ List<String> names = Arrays.asList("Ken", "Sara", "Simon"); List<String> courses = Arrays.asList("209", "325", "313"); Map<String, String> tasks = zip(names, courses); for (String key : tasks.keySet()) System.out.println(key + " " + tasks.get(key));

21 File streams and file processing
For Monday File streams and file processing


Download ppt "Software Development Java Collections"

Similar presentations


Ads by Google