Presentation is loading. Please wait.

Presentation is loading. Please wait.

JAVA COLLECTIONS LIBRARY

Similar presentations


Presentation on theme: "JAVA COLLECTIONS LIBRARY"— Presentation transcript:

1 JAVA COLLECTIONS LIBRARY
COMP 103 JAVA COLLECTIONS LIBRARY

2 TODAY Libraries, Collections : Java Collections Library
2 Libraries, Collections : Java Collections Library Interfaces and Classes Abstract Data Types ArrayList (used in assignment #1)

3 Programming with Libraries
3 Modern programs (especially GUI and network) are too big to build from scratch Have to reuse code written by other people Libraries contain code designed for reuse Java has a huge number of standard libraries... Packages, which are groups of Classes The comp102 library has some very useful classes (ex. UI Class) There are LOTS of other libraries as well Learning to use libraries is ESSENTIAL Java API

4 Libraries for COMP103 4 Comp102 Special UI class for text/graphical input and output java.util the Collection classes, other utility classes java.io Classes for input and output javax.swing Large library of classes for GUI jawa.awt programs We will use these libraries in almost every program

5 Using Libraries Read the documentation to pick useful library
5 Read the documentation to pick useful library Import the package or class into your program import java.util.*; import java.io.*; import comp102.*; 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

6 Collections 6 An object that serves as a repository for other objects (like a “container”) Type of Elements (a collection of ….) Structure (no/linear/hierarchical) Constraints (duplicates? Access: add anywhere, remove from one end only, get from top, etc…) Don’t care how it’s stored or manipulated inside

7 Example Bag : a example of a collection Type of Elements
7 Bag : a example of a collection Collection type Element type Type of Elements Bag of Students Structure No structure/order Constraints Duplicates allowed, add/remove anywhere (no order)

8 “Standard” Collections
8 Common ways of organizing a collection of values: Each of these is a different type of collection Collections Bag Graph Tree Stack Queue List Map Set

9 Java Collections Library
Standard collections (eg. Bag, Set, List, Map, etc.) are implemented for you! Java Collections Library Code written by others – for you to use! Collections implemented as interfaces Collection, Set, List, Queue – are all interfaces Using collections implies using one of the many concrete classes that implement these interfaces. Eg. You can use: ArrayList (a class) which implements List (an interface).

10 Java Collections Library (partial overview)
Interfaces, hmm…. Source:

11 Java Interfaces 11 At the simplest level: An interface is a bunch of method signatures (no bodies) with a name. At a higher level: An interface is an abstract concept that defines: the operations that can be done to an object of this type how it will behave No concrete details! No constructors - can’t make an instance No fields - doesn’t say how to store the data No method bodies. - doesn’t say how to perform the operations Details provided by Classes that implements the Interface A Java Interface corresponds to an Abstract Data Type (ADT) I removed ADT slide as we never really properly build on this concept – mentioning by name and telling them about it informally is enough for now. (Alex)

12 Java Collections Library
12 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 disadvantages abstract concrete Alex: This BAG stuff has gotta go!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

13 ArrayList (used lots in Assignment1)
13 Part of the Java Collections framework Predefined class, implements List interface Stores a LIST of items a collection of items kept in a particular order Part of the java.util package ⇒ need import java.util.*; at the head of your file You can make a new ArrayList object, and put items in it Don’t have to specify its size: Like an infinitely stretchable array Should specify the type of items But, you can’t use the [...] notation You have to call methods to access and assign

14 Using ArrayList: declaring
14 List of students (assume we have a class called Student) Array: private static final int maxStudents = 1000; private Student[ ] students = new Student[maxStudents]; private int count = 0; ArrayList: private ArrayList <Student> students = new ArrayList <Student>(); Type of elements in the list is between “<” and “>” after ArrayList No maximum, no initial size (!), no explicit count

15 QUICK TIP: Read Documentation from sidebar of course homepage
Using ArrayList: methods 15 ArrayList has many methods! 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 contains(item): true if the list contains an item that equals item get(index): returns the item at position index remove(item): removes an occurrence of item remove(index): removes the item at position index (both “removes” move the later items down) You can use the “for each” loop on an ArrayList, as well as for loop QUICK TIP: Read Documentation from sidebar of course homepage

16 Using ArrayList 16 private ArrayList <Student> students = new ArrayList <Student> (); Student s = new Student(“Davy Jones”, ); students.add(s); for(int i = students.size()-1; i>=0; i--) { UI.println(students.get(i).toString()); } if(students.contains(current)) { UI.println(current); students.remove(current); .... ....

17 Summary Libraries Collections
17 Libraries Collections Interfaces – correspond to ADTs. No concrete details. Eg. Collection, List, Set… ArrayList – example of a class that implements an interface (List) from the Java Collections Library.

18 What’s Next? 18 How do we define the type of element a collection contains? Constructing a new object of an appropriate collection class. What can we do with them? What methods can we call on them? How do we iterate down all the elements of a collection? How do we choose the right collection interface and class? What if there isn’t the right class for what we want?


Download ppt "JAVA COLLECTIONS LIBRARY"

Similar presentations


Ads by Google