14
The Java Collections Framework (JCF) Introduction and review 1

The Java Collections Framework (JCF) Introduction and review 1

Embed Size (px)

Citation preview

Page 1: The Java Collections Framework (JCF) Introduction and review 1

1

The Java Collections Framework (JCF)

Introduction and review

Page 2: The Java Collections Framework (JCF) Introduction and review 1

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

Page 3: The Java Collections Framework (JCF) Introduction and review 1

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:

Page 4: The Java Collections Framework (JCF) Introduction and review 1

4

Review

What is an interface?

CS-2851Dr. Mark L. Hornick

Page 5: The Java Collections Framework (JCF) Introduction and review 1

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

Page 6: The Java Collections Framework (JCF) Introduction and review 1

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

Page 7: The Java Collections Framework (JCF) Introduction and review 1

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

Page 8: The Java Collections Framework (JCF) Introduction and review 1

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

Page 9: The Java Collections Framework (JCF) Introduction and review 1

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.

Page 10: The Java Collections Framework (JCF) Introduction and review 1

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

Page 11: The Java Collections Framework (JCF) Introduction and review 1

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

Page 12: The Java Collections Framework (JCF) Introduction and review 1

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?

Page 13: The Java Collections Framework (JCF) Introduction and review 1

13

So what’s the difference between ArrayList and LinkedList?

CS-2851Dr. Mark L. Hornick

Page 14: The Java Collections Framework (JCF) Introduction and review 1

14CS-2851Dr. Mark L. Hornick

Demo

Looking at ArrayList and LinkedList

ArrayListLinkedList