Download java collections library - School of Engineering and Computer Science

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
COMP 103
2014-T2, Lecture 2
JAVA COLLECTIONS LIBRARY
Marcus Frean
School of Engineering and Computer Science, Victoria University of Wellington
TODAY
2




Libraries and code reuse
Java’s Collections Library
ArrayList (our first example)
Interfaces and Classes
Administrivia:

Yesterday’s course outline had two typos:
st (not 9th)
 Test will be on August 21
 Exam will be 2 hours long (not 3)
Programming with Libraries
3
Modern programs (especially GUI and network)
are too big to build from scratch, so we have to reuse code written by
other people....

Libraries contain code designed for reuse
 Java
has a huge number of standard libraries...
 Packages,
Java API
which are groups of Classes
 The
ecs100 library has some very useful classes (eg. UI)
 There are LOTS of other libraries as well

Learning to use libraries is ESSENTIAL
Libraries for COMP103
4

ecs100
Special classes for text/graphical
input and output, especially for GUI

java.util
the Collection classes,
and other utility classes

java.io
Classes for input and output
javax.swing
jawa.awt
Large libraries of classes for GUI
programs

We will use libraries in almost every program
5
Using Libraries
Read the documentation to pick useful library Java API
 Import the package or class into your program

import java.util.*;
import ecs100.*;

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
“Standard” Collections
6

Common ways of organizing a collection of values:
some collections...
Bag
Graph
Set
Tree
Map

List
Queue
Stack
Each of these is a different type of collection
Java Collections Library
7


Standard collections (eg. Set, List, Map, etc.) are
implemented for you
Java Collections Library
 Code
written by others – for you to use
 Collections specified 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)....
You have been using ArrayList
8

Part of the Java Collections framework

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

8
Using ArrayList: declaring
9
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

9
Using ArrayList: methods
10
ArrayList has many methods!



returns the number of items in the list
adds an item to the end of the list
inserts an item at index
(moves later items up)
replaces the item at index with item
true if the list contains an item that equals item
returns the item at position index
removes an occurrence of item
removes the item at position index
(both “removes” move the later items down)

set(index, item):
contains(item):
get(index):
remove(item):
remove(index):

You can use the “for each” loop on an ArrayList, as well as for loop




10
size():
add(item):
add(index, item):
TIP: Read Documentation from sidebar of course homepage
11
Using ArrayList
....
private List <Student> students = new ArrayList <Student> ();
Student s = new Student(“Davy Jones”, 300012345);
....
students.add(s);
for(Student st: students)
UI.println(st.toString());
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);
}
12

Collections
An object that serves as a repository for other
objects (like a “container”)
Type
of Elements (a collection of ….)
Constraints (duplicates? Access: add
anywhere, remove from one end only, get
from top, etc…)
Structure (no/linear/hierarchical)
13

Example
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)
14
Is ArrayList a Collection type then?
Collection
type
Element
type
Type
of Elements
ArrayList of Students
Structure
linear order
Constraints
Duplicates allowed, add/remove
anywhere
actually no! ArrayList is not a Collection type
List is!
ArrayList is just ONE WAY to “do” (implement) a List
• an interface: says what you’re doing
• an implementation (=a class): how you’re doing it
And there’s a hierarchy to interfaces, so we say they “extend” one another
16
Java Collections Library
Interfaces:
abstract
 Collection
Classes:
 List
 Set
unordered, no duplicates
 Map
classes:
EnumMap, HashMap, TreeMap,
LinkedHashMap,
WeakHashMap, …
 Queue
ordered collection with limited access
(add at one end, remove from other)
 Map
classes:
HashSet, TreeSet, EnumSet,
LinkedHashSet,…
ordered collection
 Set
classes:
ArrayList, LinkedList, Vector
most general: “a bag”
 List
concrete
…
key-value pairs (or mapping)
Specify the Types
Implement the interfaces
advantages and disadvantages
17

Java Interfaces
At the simplest level:
 An
interface is a bunch of method signatures with a name.
(Method signature only, no bodies)

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 implement the Interface
Abstract Data Types (ADT)
18

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
 Outlines how it will behave

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
No fields
No method bodies.
public interface Set {
- can’t make an instance: new Set()
- doesn’t say how to store the data
- doesn’t say how to perform the operations
public void add(??? item);
/*…description…*/
public void remove(??? item);
/*…description…*/
public boolean contains(??? item);
/*…description…*/
…
// (plus lots more methods in the Java Set interface)
declaring a List
19

Earlier, we declared as ArrayList like this:
private ArrayList <Student> students = new ArrayList <Student>();

BETTER / NOW: declare as an instance of the interface List:
private List <Student> students = new ArrayList <Student>();
19
Summary
20



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.
What’s Next?
21

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?