Lists in the Java Collections API

Cards (5)

  • Collections API
    A part of the java language library's that is implementations of common data structures
  • Collection Interface

    • A collection stores a collection of identically type objects
    • The notion is abstracted in the Collection interface
    • Has many of the obvious methods
    • Extends Iterable interface
  • Classes that implement the Iterable interface can have the enhanced for loop used on them to view all their names
  • Iterator
    • Collections that implement the Iterable interface must provide a method named iterator that returns an object of the type Iterator
    • .next gives the next item in the collection
    • .hasNext used to tell you if there is a next item
    • Only beneficial for a simple traversal through the collection due to the limited set of methods available in its interface
    • Iterators remove method main advantage is that the it is less expensive to remove an item when you know where is is
    • Fundamental rule when using an iterator directly (rather than indirectly through an enhanced for look): If you make a structural change to the collection being iterated then the iterator is no longer valid. This means you shouldn't obtain an iterator until immediately prior to the need to use it
  • List Interface, Array List, and LinkedList
    • Package: java.util
    • The list interface extends Collection, therefore it contains all the methods in the collection interface plus a few others