Java Collections

Cards (82)

  • Collection
    • represents a single unit of objects
    • one-stop solutions for all the data manipulation such as storing data, searching, sorting, insertion, deletion and updating of data
  • Framework
    • provides readymade architecture
    • body of pre-written code acting as a template or skeleton, which a developer can then use and reuse to create an application
    • represents a set of classes and interfaces
  • Java Collection Framework (JCF)
    • enables the user to perform various data manipulation operations like storing data, searching, sorting, deletion and updating of data on the group of elements
    • provides an architecture to store and manipulate a group of objects
  • Interfaces - these are abstract data types that represents collections
  • Implementations - theses are the concrete implementations of the collection interface. In essence, they are reusable data structures
  • Algorithms - methods that perform useful computations, such as searching and sorting. The algorithms are said to be polymorphic: that is, the same method can be used on many different implementations of the appropriate collection interface
  • Set
    • a collection that does not contain duplicate values
    • it has its implementation in various classes such as HashSet, TreeSet and LinkedHashSet
  • List
    • collection that can contain the duplicate values which are stored sequentially
    • list enables the user to maintain an ordered collection of elements with the help of indexing methods and can perform data manipulation operations such as insert, update, delete and many more
  • Queue
    • contains an ordered list of objects with its usage specifically limited to inserting of the elements at the end of the list
    • follows the FIFO(First In First Out) principle
  • Iterable Interface - root interface for all collection classes. The Collection Interface extends the iterable interface and therefore all the subclasses of Collection Interface also implements the iterable interface
  • boolean add(Object o)
    Insert an element into the collection
  • boolean addAll(Collection c)

    Insert a collection into collection
  • void clear()
    Removes all elements from collection
  • boolean remove(Object o)

    Delete an element from the collection
  • boolean removeAll(Collection c)
    Delete all from specified collection
  • boolean retainAll(Collection c)

    Remove all elements exception collection c
  • boolean contains(Object O)

    Used to search an element
  • boolean containsAll(Collection c)

    Used to search a collection
  • boolean equals(Object o)
    Used to check quality of object
  • boolean isEmpty()

    Check the collection if its empty or not
  • int size()
    Get number of element in a collection
  • int hashCode()
    Return the hashcode number for collection
  • iterator iterator()
    Return an iterator
  • Object[] toArray()
    Convert the collection to array
  • Generics Framework
    • was introduced in Java SE 5.0 and provided support that allows parameterization of types <E>
    • reduces the amount of code that needs to be written when placing an object collection
    • eliminates the use of cast
    • makes the code stable by detecting the bugs at compile time
    • denoted by the diamond operation with a type in it <E>
  • The Diamond (<>) Operator
    • before Java SE 7, you have to specify the type of object you will put in the collection
    • should be done both in the declaration and instantiation
  • ArrayList
    • implementation of List Interface where the elements can be dynamically added or removed from the list
    • similar to an array but has no size limit
    • maintains an insertion order
  • LinkedList
    • a sequence of links which contains items. Each link contains a connection to another link
    • if programmers need to a lot of insertion and deletions of elements/nodes, the LinkedList is preferrable to an ArrayList
  • Vectors
    • similar to arrays, where the elements of the vector object can be accessed via an index into the vector
    • implements a dynamic array
    • not limited to a specific size, it can shrink or grow automatically whenever required.
    • similar to an ArrayList but it has two differences:
    1. vector is synchronized
    2. contains many legacy methods that are not part of the collections framework
  • ArrayList and Vectors allows random access
  • The memory location of ArrayList and Vectors are contagious while the LinkedList is not contagious
  • Vector is synchronized while ArrayList and LinkedList are not
  • ArrayList, LinkedList and Vectors supports null values
  • ArrayList and Vectors are a Dynamic Array while Linked List is a Dynamic Linked List
  • ArrayList, LinkedList and Vector allows duplicate values
  • The insertion and deletion of the ArrayList is slow
  • The insertion and deletion of the LinkedList is fast
  • The insertion and deletion of data in Vector is slow
  • Set Interface
    • provides the features of the mathematical set in Java
    • cannot contain duplicate values unlike the List Interface
    • under this interface, we can use three classes which are HashSet, LinkedHashSet and TreeSet
  • Java HashSet Class
    • used to create a collection that uses a hash table for storage
    • contains unique elements only
    • allows null values