The Java Collections Framework (JCF) Introduction and review 1.
-
Upload
christopher-parsons -
Category
Documents
-
view
220 -
download
6
Transcript of The Java Collections Framework (JCF) Introduction and review 1.
1
The Java Collections Framework (JCF)
Introduction and review
2CS-2851Dr. Mark L. Hornick
You learned about ArrayList<E> and List<E> in SE1011 and SE1021ArrayList<E> is one type of Collection class in the java.util
package. A Collection represents a group of objects, or elements.
Some methods of ArrayList are:
boolean add( E o)
Adds an object o of type E to the list
void clear( )
Clears this list, i.e., make the list empty
Object get( int index )
Returns the element at position index
boolean remove( int index )
Removes the element at position index
int size()
Returns the number of elements in the list
3CS-2851Dr. Mark L. Hornick
The Java Collections Framework…is implemented as a series of hierarchies with
interfaces at the topabstract classes in the middleand fully defined classes at the bottom
More than 200 methods in all!
The interfaces are:
4
Review
What is an interface?
CS-2851Dr. Mark L. Hornick
5CS-2851Dr. Mark L. Hornick
Collection is the base interface that many other interfaces, abstract classes, and classes inherit
The Collection interface defines certain basic behaviors, such as (to name just a few): add(e) – allows an element to be added to the collection clear() – removes all elements from the collection contains(e) – determines if an element is in the collection isEmpty() – determines if the collection is empty remove(e) – removes a specific element from the collection size() – gets the number of elements in the collection
Note: Collection does not define a way to retrieve an element from a collection
6CS-2851Dr. Mark L. Hornick
Set, List, and Queue are derived interfaces that define more specific behavior
List – an ordered collection, or a sequence that defines specific control over where elements are placed in the collection
Set – a collection that does not allow duplicate items SortedSet – self explanatory
Queue – an ordered collection supporting a specific way of adding and removing elements from a collection
7CS-2851Dr. Mark L. Hornick
The List interface declares methods for inserting, removing and retrieving an element at a certain location in a collection
The List interface defines some index-oriented methods, such as:
add(index,e) – adds an element at a specific location/index in the list
get(index) – retrieve the element at the specific location/index remove(index) – remove the element at the specific location/index set(index, e) – replace the element at the specific location/index
8CS-2851Dr. Mark L. Hornick
<interface>>
Collection
E
<<interface>>
List
E
AbstractList E
ArrayList E
<interface>>
Set
AbstractSet
LinkedList E
TreeSet HashSet E E
E
E
9CS-2851Dr. Mark L. Hornick
JCF Abstract Classes reside between interfaces and collection classes
have undefined methods (like an interface) as well as defined methods (like a regular class).
What does an abstract class provide that an
interface does not? Simple definitions of common methods that need not be
overridden in the fully defined subclasses.
One example is that a subclass of AbstractList need not override the isEmpty() or size() methods.
10CS-2851Dr. Mark L. Hornick
JCF Collection Classes At the bottom of the JCF hierarchy are the fully-defined
collection classes (or containers) A container is another name for a class whose instances are
collections (of elements) Collection classes implement the interfaces at the lowest level JCF provides (most) common containers with iterators and an
assortment of algorithms.
Interface General-purpose Implementations
Hash Table Resizable array
Tree Linked List Hash Table + Linked List
Set HashSet TreeSet LinkedHashSet
List ArrayList LinkedList
Queue
Map HashMap TreeMap LinkedHashMap
11CS-2851Dr. Mark L. Hornick
JCF Collection class: ArrayList
List<Double> salaryList = new ArrayList<Double>();salaryList.add(1000); salaryList.add(0,2000); // add before 1000salaryList.remove(1); // remove 2nd elementsalaryList.clear(1000); // clear list
This creates an instance, salaryList, of the ArrayList<> collection class. The elements in salaryList must be (references to) Double objects.
The size need not be specified
12CS-2851Dr. Mark L. Hornick
JCF Collection class: LinkedList
List<Double> salaryList = new LinkedList<Double>();salaryList.add(1000); salaryList.add(0,2000); // add before 1000salaryList.remove(1); // remove 2nd elementsalaryList.clear(1000); // clear list
This creates an instance, salaryList, of the LinkedList<> collection class. The elements in salaryList must be (references to) Double objects.
Does LinkedList sound identical to ArrayList?
13
So what’s the difference between ArrayList and LinkedList?
CS-2851Dr. Mark L. Hornick
14CS-2851Dr. Mark L. Hornick
Demo
Looking at ArrayList and LinkedList
ArrayListLinkedList