1.  Array List

The ArrayList class extends AbstractList and implements the List interface. ArrayList supports dynamic arrays that can grow as needed.

Standard Java arrays are of a fixed length. After arrays are created, they cannot grow or shrink, which means that you must know in advance how many elements an array will hold. Array lists are created with an initial size. When this size is exceeded, the collection is automatically enlarged. When objects are removed, the array may be shrunk.

Methods of ArrayList

add(), get(), remove(), size(), clear(), isEmpty(), indexOf(), lastIndexOf(), contains(), get()

Output:

Initial size of al: 0

Size of al after additions: 7

Contents of al: [C, A2, A, E, B, D, F]

Size of al after deletions: 5

Contents of al: [C, A2, E, B, D]

2.  Linked List

Like arrays, Linked List is a linear data structure. Unlike arrays, linked list elements are not stored at the contiguous location, the elements are linked using pointers.

//Methods are same as arraylist

Output

Initial size of list: 0

Size of list after additions: 7

Contents of list: [C, A2, A, E, B, D, F]

Size of list after deletions: 5

Contents of list: [C, A2, E, B, D]

3.  Hash Map

HashMap is a part of Java’s collection since Java 1.2. It provides the basic implementation of Map interface of Java. It stores the data in (Key, Value) pairs. To access a value one must know its key. HashMap is known as HashMap because it uses a technique called Hashing. Hashing is a technique of converting a large String to small String that represents the same String. A shorter value helps in indexing and faster searches.

//Methods is same as Hash set

Output:

{vaibhav=20, vishal=10, sachin=30}

Size of map is:- 3

value for key "vishal" is:- 10 After Clearing Map:

4.  Hash Set

The HashSet class implements the Set interface, backed by a hash table which is actually a HashMap instance. Few important features of HashSet are:

  • Implements Set Interface.
  • Underlying data structure for HashSet is hashtable.
  • As it implements the Set Interface, duplicate values are not allowed.
  • Objects that you insert in HashSet are not guaranteed to be inserted in same order.
  • Objects are inserted based on their hash key.
  • NULL elements are allowed in HashSet.

//Methods are similar to map set interface

Output:

[South Africa, Australia, India]

List contains India or not:true

List after removing Australia:

[South Africa, India]

5. Tree Set

TreeSet is one of the most important implementations of the SortedSet interface in Java that uses a Tree for storage. The ordering of the elements is maintained by a set using their natural ordering whether or not an explicit comparator is provided. This must be consistent with equals if it is to correctly implement the Set interface.

Few important features of TreeSet are as follows:

  • TreeSet implements the SortedSet interface so duplicate values are not allowed.
  • Objects in a TreeSet are stored in a sorted and ascending order.
  • TreeSet does not preserve the insertion order of elements but elements are sorted by keys.
  • TreeSet does not allow to insert Heterogeneous It will throw classCastException at Runtime if trying to add hetrogeneous objects.
  • TreeSet serves as an excellent choice for storing large amounts of sorted information which are supposed to be accessed quickly because of its faster access and retrieval time.

//Methods are same as set interface

Output:

[Australia, India, South Africa]

List contains India or not:true

List after removing Australia:

[India, South Africa]

 

Accessing Collections using Iterator

         

Output:

Iterating over list:

South Africa

Australia

India

Comparator in Java

Comparator in Java are very useful for sorting the collection of objects. Java provides some inbuilt methods to sort primitive types array or Wrapper classes array or list.

           

      

Output:

[Hari, Ram, Shyam]