1 / 14

The Java Collections Framework (JCF)

The Java Collections Framework (JCF). Introduction and review. You learned about ArrayList <E> and List<E> in SE1011 and SE1021. ArrayList <E> is one type of C ollection c lass in the java.util package. A C ollection represents a group of objects, or elements .

shira
Télécharger la présentation

The Java Collections Framework (JCF)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. You learned about ArrayList<E> and List<E> in SE1011 and SE1021 ArrayList<E> is one type of Collection classin the java.util package. A Collection represents a group of objects, or elements. Some methods of ArrayList are: 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: CS-2851 Dr. Mark L. Hornick

  4. Review • What is an interface? 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 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 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 CS-2851 Dr. Mark L. Hornick

  8. 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 AbstractListneed not override the isEmpty() or size() methods. CS-2851 Dr. Mark L. Hornick

  10. JCF Collection Classes • At the bottom of the JCF hierarchy are the fully-defined collection classes (orcontainers) • 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. CS-2851 Dr. Mark L. Hornick

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

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

  13. So what’s the difference between ArrayListand LinkedList? CS-2851 Dr. Mark L. Hornick

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

More Related