Collections

Enumeration, Iterator and ListIterator in Java

Both Enumeration and Iterator are interfaces in Java for getting successive elements. Enumeration is older while iterator was introduced later with some improvements. Iterator has taken the place of Enumeration in the Java collections framework. ListIterator is a sub-interface of Iterator and allows the programmer to traverse the list in either direction, modify the list during iteration, and obtain the iterator's current position in the list. 

Introduction to Java Collections Framework

Collection framework in Java provides efficient implementations for commonly used data structures like list, set, queue, map etc.  We can reuse these implementations without writing our own implementations when we need to use these data structures.

Significance of equals and hashCode methods of the Object class

equals()

Method equals check if two objects are equal.

String class and most collection classes overrides equal to check if two objects are meaningfully equal, which means, if two different string objects have the same value.

The equals method for class Object will return true, for any non-null reference values x and y, if x and y refer to the same object. 

 

TreeMap Class

TreeMap is a Red-Black tree based NavigableMap implementation.

 

Important properties of TreeMap

  1. The map is sorted according to the natural ordering of its keys, or by a Comparator provided at map creation time.

  2. Provides guaranteed log(n) time cost for the containsKey, get, put and remove operations.

    • Algorithms are adaptations of those in Cormen, Leiserson, and Rivest's Introduction to Algorithms.

Collection Interface

Collection Interface is the Root interface in the collection hierarchy.

 

Important properties Collection interface

  1. JDK does not provide any direct implementations of this interface: it provides implementations of more specific subinterfaces like Set and List etc.

List Interface

List is an ordered collection.

 

Important points about List interface

  1. also known as a sequence.

  2. typically allow duplicate

    • typically allow multiple null elements if they allow null elements at all.

  3. Places additional stipulations on the contracts of the iterator, add, remove, equals, and hashCode methods.

ArrayList Class Basic Concepts

ArrayList is a resizable-array implementation of the List interface.

ArrayList Class Operations Summary

Creating ArrayList

There are three constructors for creating an ArrayList:

  1. When you create an ArrayList using the constructor ArrayList(), the internal array for storage is created with a size 10.

  2. The constructor ArrayList(int initialCapacity) allows us to set the initial capacity.

Comparable and Comparator Interfaces

You can sort a list of integers based on their natural ordering. You can compare two integers and see which one comes before the other in natural ordering. But how will you compare two objects and sort them in an order. You have two interfaces in Java Comparable and Comparator that will help us compare objects and sort them in an order.

 

SortedMap Interface

SortedMap interface extends Map interface to further provide a total ordering on its keys.

 

Important Properties of SortedMap

  • SortedMap is ordered according to the natural ordering of its keys, or by a Comparator typically provided at sorted map creation time.

  • Order is reflected when iterating over the sorted map's collection views returned by the entrySet, keySet and values methods.

  • SortedMap is the map analogue of SortedSet.

Pages

Search the Web

Custom Search

Searches whole web. Use the search in the right sidebar to search only within javajee.com!!!