Presentation is loading. Please wait.

Presentation is loading. Please wait.

Using the Java Collection Libraries COMP 103 # T2

Similar presentations


Presentation on theme: "Using the Java Collection Libraries COMP 103 # T2"— Presentation transcript:

1 Using the Java Collection Libraries COMP 103 #2 2012 T2

2 Menu: Programming with libraries Collections
Programming with Lists of objects (Needed for CrowdRenderer)

3 Course Structure First half:
Programming with unstructured and linear collections Different Kinds of collections: Lists, Sets, Bags, Maps, Stacks, Queues, Priority Queues Implementing, using, sorting, searching Linear collections... Second half: Programming with Hierarchical collections Building, traversing Tree structured collections Re-implementing linear collections with binary search trees with partially ordered trees hash tables

4 Recurring Themes Good Design: Efficiency: Testing:
Which choices should you make? Choosing appropriate implementations for collections Making the right choice the first time Efficiency: How fast is it? How much memory does it take? By analysis, and by benchmarking Testing: Does it work right?

5 Programming with Libraries
Modern programs (especially GUI and network) are too big to build from scratch. ⇒ Have to reuse code written by other people Libraries are collections of code designed for reuse. Java has a huge collection of standard libraries…. Packages, which are collections of Classes and Interfaces The comp102 library has about 10 classes (UI, Trace, UIMouseListener, UIFileChooser, …) There are lots of other libraries as well Learning to use libraries is essential. Java API

6 Libraries for COMP 103 java.util Collection classes Other utility classes java.io Classes for input and output comp102 UI, etc We will use these libraries in almost every program We will use other libraries also, where appropriate.

7 Using Libraries Read the documentation to pick useful library
            Using Libraries Read the documentation to pick useful library import the package or class into your program import java.util.*; import java.io.*; (or just the class you want:) import java.awt.BorderLayout; import javax.imageio.ImageIO; Read the documentation to identify how to use Constructors for making instances methods that you can call Interfaces that you can implement Use the classes as if they were part of your program Java API

8 Collections What is a Collection?
An object that contains other objects What defines the collection type? Structure (no/linear/hierarchical) Constraints on access, modification, etc Mostly don’t care how it’s stored or manipulated inside: As long as it has the right behaviour, the inside doesn't matter.

9 “Standard” Collections
Common ways of organising a collection of values: Collection Bag Graph Set Tree Map Stack List Queue What's the difference?

10 Collections: What’s the difference
Different types of values Different structures No structure – just a collection of values Linear structure of values – the order matters Set of key-value pairs Hierarchical structures Grid/table …. Different constraints duplicates allowed/not allowed get, put, remove anywhere get, put, remove only at the ends, or only at the top, or … get, put, remove by position, or by value, or by key, or …

11 Abstract Data Types Set, Bag, Queue, List, Stack, Map, etc are
an ADT is a type of data, described at an abstract level: Specifies the operations that can be done to an object of this type Specifies how it will behave. We don't care how it is implemented underneath though we will always need some concrete implementation of it.

12 Eg: Set ADT (simple version) Operations: Behaviour: add(value ),
remove(value ), contains(value )→boolean Behaviour: A new set contains no values. A set will contain a value iff the value has been added to the set and it has not been removed since adding it. A set will not contain a value iff the value has never been added to the set, or it has been removed from the set and has not been added since it was removed.

13 Java Collections Library
Defines interfaces ⇒ Abstract Data Types and classes: Collection interfaces extends Set List Queue implements SortedSet HashSet LinkedHashSet TreeSet ArrayList LinkedList PriorityQueue classes

14 Java Collections library
Interfaces: Collection = Bag (most general) List = ordered collection Set = unordered, no duplicates Queue ordered collection, limited access (add at one end, remove from other) Map = key-value pairs (or mapping) Specify the Types: Classes List classes: ArrayList, LinkedList, Vector Set classes: HashSet, TreeSet, EnumSet, LinkedHashSet,… Map classes: EnumMap, HashMap, TreeMap, LinkedHashMap, WeakHashMap, … Implement the interfaces Each implementation has advantages and disadvantage.

15 Java Interfaces and ADT’s
A Java Interface corresponds to an Abstract Data Type Specifies what methods can be called on objects of this type (specifies name, parameters and types, and type of return value) Behaviour of methods is only given in comments (but cannot be enforced) No constructors - can’t make an instance: new Set() No fields - doesn’t say how to store the data No method bodies. - doesn’t say how to perform the operations public interface Set { public void add(Object item); /*…description…*/ public void remove(Object item); /*…description…*/ public boolean contains(Object item); /*…description…*/ // (plus lots more methods in the Java Set interface)

16 Using Java Collection Interfaces
Your program can Declare a variable, parameter, or field of the interface type private List students; // a list of Students Create a collection object students= new ArrayList (); Call methods on that variable, parameter, or field students.add(new Student("James Noble", )) students.get(3).reportGrades() Problem: How do we specify the type of the values in the collection? This won’t work! why?

17 Parameterised Types. The structure and access discipline of a collection is the same, regardless of the type of value in it: A set of Strings, a set of Persons, a set of Shapes, a set of integers all behave the same way. ⇒ Only want one Interface for each kind of collection (there is only one Set interface) Need to specify kind of values in a particular collection ⇒ The collection Interfaces (and classes) are parameterised: Interface has a type parameter When declaring a variable collection, you specify the type of the collection and the type of the elements of the collection

18 Parameterised Types. It’s a Set of something, as yet unspecified Interfaces may have type parameters (eg, type of the element): public interface Set <E> { public void add(E item); /*…description…*/ public void remove(E item); /*…description…*/ public boolean contains(E item); /*…description…*/ … // (lots more methods in the Java Set interface) When declaring variable, specify the actual type of element private Set <Person> friends; private List <Shape> drawing; Type of value in Collection Collection Type

19 Using Java Collection Interfaces
Your program can Declare a variable, parameter, or field of the interface type private List <Student> students; // a list of Students Create a collection object students= new ArrayList <Student> (); Call methods on that variable, parameter, or field students.add(new Student("James Noble", )) students.get(3).reportGrades() Note: value type can't be a primitive type: int, double, boolean, etc; have to use the "wrappers": Integer, Double, Boolean, etc Can then put int, double, boolean into the List. Now this works! why?

20 Using ArrayList: methods
Lists have many methods, including: size(): returns the number of items in the list add(item ): adds an item to the end of the list add(index, item ): inserts an item at index (moves later items up) set(index, item ): replaces the item at index with item get(index ): returns the item at position index contains(item ): true if the list contains an item that equals item indexOf(item ): returns the index of item, (-1 if item not present) remove(item ): removes an occurrence of item remove(index ): removes the item at position index (both move later items down) You can use the “for each” loop on an array list, as well as a for loop Read the documentation from the sidebar of course home page.

21 Using ArrayList private ArrayList <Student> students = new ArrayList <Student>(); : Student s = new Student("James Noble", ) students.add(s); students.add(0, new Student("Stuart Marshall", )); for (Student st : students( System.out.println(st); for (int i = students.size()-1; i>=0; i--) System.out.println(students.get(i)); if (students.contains(current)){ file.println(current); students.remove(current); } Collections.shuffle(students); Collections.sort(students); The Collections class has useful static methods that operate on ArrayLists and other Collections


Download ppt "Using the Java Collection Libraries COMP 103 # T2"

Similar presentations


Ads by Google