Short Notes on Java Collections Framework

Java Collections Framework Notes:
Collection Interface:

The fundamental interface for collection classes in the Java library is the Collection interface.

The add method returns true if adding the element actually changes the collection, and false if the collection is unchanged.

The iterator method returns an object that implements the Iterator interface. You can use the iterator object to visit the elements in the collection one by one.

Iterator Interface:

if we reach the end of the collection, the next method throws a NoSuchElementException. Therefore, we need to call the hasNext() method before calling next().

If we want to inspect all elements in a collection, we can achieve this in below two methods.

From JAVA SE 1.5 onwards, we can achieve the same with for-each loop.

The “for each" loop works with any object that implements the Iterable interface, an interface with a single method.

Iterable Interface:

Note: The Collection interface extends the Iterable interface. Therefore, you can use the “for each" loop with any collection in the standard library.

Order of elements processing in a collection:

The order in which the elements are visited depends on the collection type.
If you iterate over an ArrayList , the iterator starts at index 0 and increments the index in each step.
However, in a HashSet , we will encounter them in essentially random order. but will encounter all elements of the collection during the course of the iteration, but you cannot make any assumptions about their ordering. This is usually not a problem because the ordering does not matter for computations such as computing totals or counting matches.

Removing Elements

if we want to remove an element in a particular position, you need to skip past the element.

So, there is a dependency between calls to the next and remove methods. It is
illegal to call remove if it wasn’t preceded by a call to next . If we try, an IllegalStateException is thrown.

To remove the first element in a collection of strings:

If you want to remove two adjacent elements, you cannot simply call

Instead, you must first call next to jump over the element to be removed.

Collection Type Description
ArrayList An indexed sequence that grows and shrinks dynamically
LinkedList An ordered sequence that allows efficient insertions and removal at any location
ArrayDeque A double-ended queue that is implemented as a circular array
HashSet An unordered collection that rejects duplicates
TreeSet A sorted set
EnumSet A set of enumerated type values
LinkedHashSet A set that remembers the order in which elements were inserted
PriorityQueue A collection that allows efficient removal of the smallest element
HashMap A data structure that stores key/value associations
TreeMap A map in which the keys are sorted
EnumMap A map in which the keys belong to an enumerated type
LinkedHashMap A map that remembers the order in which entries were added
WeakHashMap A map with values that can be reclaimed by the garbage collector if they are not used elsewhere
IdentityHashMap A map with keys that are compared by ==, not equals


Profile photo of Siva

About Siva

Senior Hadoop developer with 4 years of experience in designing and architecture solutions for the Big Data domain and has been involved with several complex engagements. Technical strengths include Hadoop, YARN, Mapreduce, Hive, Sqoop, Flume, Pig, HBase, Phoenix, Oozie, Falcon, Kafka, Storm, Spark, MySQL and Java.

Leave a comment

Your email address will not be published. Required fields are marked *

Review Comments
default image

I am a plsql developer. Intrested to move into bigdata.

Neetika Singh ITA Hadoop in Dec/2016 December 22, 2016