Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "The Java Collections Framework (JCF) Introduction and review 1."— Presentation transcript:

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

2 CS-2851 Dr. Mark L. Hornick 2 You learned about ArrayList and List in SE1011 and SE1021 ArrayList 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

3 CS-2851 Dr. Mark L. Hornick 3 The Java Collections Framework …is implemented as a series of hierarchies with interfaces at the top abstract classes in the middle and fully defined classes at the bottom More than 200 methods in all! The interfaces are:

4 Review What is an interface? CS-2851 Dr. Mark L. Hornick 4

5 CS-2851 Dr. Mark L. Hornick 5 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

6 CS-2851 Dr. Mark L. Hornick 6 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

7 CS-2851 Dr. Mark L. Hornick 7 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

8 CS-2851 Dr. Mark L. Hornick 8

9 CS-2851 Dr. Mark L. Hornick 9 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.

10 CS-2851 Dr. Mark L. Hornick 10 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. InterfaceGeneral-purpose Implementations Hash TableResizable array TreeLinked ListHash Table + Linked List SetHashSetTreeSetLinkedHash Set ListArrayListLinkedList Queue MapHashMapTreeMapLinkedHash Map

11 CS-2851 Dr. Mark L. Hornick 11 JCF Collection class: ArrayList List salaryList = new ArrayList (); salaryList.add(1000); salaryList.add(0,2000); // add before 1000 salaryList.remove(1); // remove 2 nd element salaryList.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

12 CS-2851 Dr. Mark L. Hornick 12 JCF Collection class: LinkedList List salaryList = new LinkedList (); salaryList.add(1000); salaryList.add(0,2000); // add before 1000 salaryList.remove(1); // remove 2 nd element salaryList.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-2851 Dr. Mark L. Hornick 13

14 CS-2851 Dr. Mark L. Hornick 14 Demo Looking at ArrayList and LinkedList ArrayList LinkedList


Download ppt "The Java Collections Framework (JCF) Introduction and review 1."

Similar presentations


Ads by Google