Special Offer - Enroll Now and Get 2 Course at $398/- Only Explore Now!

All Courses
Collections in Java

Collections in Java

April 30th, 2019

Collections in Java

Collections is a generally called handling group of elements. Its used to add the Elements in dynamically in memory and also arrange  the Elements in structure. It allows different types of collections to work in a similar manner and with a high degree of interoperability. Implementation of fundamentals of Collection Process are Highly Efficient.
Collection classes are used static methods to operate collection algorithms.

Collection Algorithm

Implementation of classes

Implementation of classes

 

  • class HashMap  implements Map
  • class TreeMap  implements SortedMap

Set Interface

  • It is an Unordered Collection. It returns the elements in ascending order
  • It does not allows duplicate to store.
  • No need of index to store elements in set
  • Ordering of elements done by sorted set interface.

HashSet:

It stores the element based on hashcode value of the object. So it returns the value in hashcode format arranging i.e ascending order of elements with hashcode. It allows different type of elements.

TreeSet:

It stores the element based on ascending order. It allows only similar type of elements. Using TreeSet we can retrieve the elements in descending order also.

List Interface

  • It is an ordered collection. returns the same order we enter
  • It allows duplicate elements to store.
  • It stored elements based on index value.

ArrayList:

  • ArrayList is implemented from List Interface.
  • It allows to store different types of elements when compare to array.
  • It also allows increase and decrease the elements dynamically.
  • It store the elements based on index and print the same order we given.
  • It allows user to store duplication of elements.

Vector:

  • Vector class extends AbstractList and implements List, RandomAccess, Cloneable, Serializable.
  • It is based on reserved memory storage called capacity.
  • It will maintain default capacity of 10 bytes. It allows user can define their own capacity based on constructor.
  • It allows to retrieve first and last element respectively.

Map:

It is similar to hashtable storage. It allows elements to store based on key-value pair. Here it doesn’t allows duplicate keys. Values may duplicate.
It allows to retrieve keys individually, values individually and key-value pair.

SortedMap:

It is used to retrieve the keys in ascending order mappings.
Mostly this kind of interface used in dictionaries and telephone directories application.

HashMap:

It allows different types of keys to store elements. Ordering keys based on hashcode of the keys object.

TreeMap:

It allows similar type of keys to store elements. Using SortedMap to arrange keys objects in ascending order.

Reading Interfaces:

In Java Collections Framework using reading elements in collections are following  Interfaces

  • Iterator Interface
  • Enumeration Interface

Iterator Interface:

It will obtain all the elements of a collection at a time and check them in sequentially by itself. It will used for all type of collection classes.

Methods:

  • boolean has Next() –  It is used for check the next elements in the collection and place the cursor in the position.
  • E next() – It is used to returns the next element.
  • void remove() – It will remove the last element given by iterator.

Enumeration Interface:

It will obtain only one element from the collection at a time. It familiarly used for Vector and Stack classes.

Methods:

  • boolean has More Elements() – It returns true if next element is available for extract.
  • E nextElement() – It returns the next element in the collection.