Skip to content

Collections Framework In Java Programming Language

In this article, you will learn about Collections Framework in Java Programming Language.

The data structures which consists classes such as Vector, Dictionary, Properties, Stack, Hashtable etc. are very useful to manipulate elements in a collection of objects. But they were proved to be deficient in many features where high performance of an application is required.

This deficiency is fulfilled by Java Collection Frameworks which are designed to provide high performance to a java application.

A Collections Framework bring together Interfaces, Classes and Algorithms thus forming an integrated architecture in order to manipulate and access collections of objects. For Example: Mail Folder which represents a collection of letters.

In Java, collections frameworks consists the following:

  1. Collection Interfaces
  2. Collection Classes
  3. Collection Algorithms

COLLECTION INTERFACES

Interfaces represents a collection of abstract data types. These enable a programmer to manipulate elements in a collection of objects independently of the state of their representation details.

We have listed below the interfaces in Collections Framework.

[1] COLLECTION INTERFACE

The methods of this interface are shown below.

  • boolean add(Object obj)
  • boolean addAll(Collection c)
  • boolean contains(Object obj)
  • boolean containsAll(Collection c)
  • boolean equals(Object obj)
  • boolean isEmpty( )
  • boolean remove(Object obj)
  • boolean removeAll(Collection c)
  • boolean retainAll(Collection c)
  • Iterator iterator( )
  • int size( )
  • int hashCode( )
  • void clear( )
  • Object[ ] toArray( )
  • Object[ ] toArray(Object array[ ])

[2] LIST INTERFACE

The methods of this interface are shown below.

  • boolean addAll(int index, Collection c)
  • int indexOf(Object obj)
  • int lastIndexOf(Object obj)
  • Object get(int index)
  • Object remove(int index)
  • Object set(int index, Object obj)
  • ListIterator listIterator( )
  • ListIterator listIterator(int index)
  • List subList(int start, int end)
  • void add(int index, Object obj)

[3] SET

The methods of this interface are shown below.

  • add( )
  • isEmpty( )
  • iterator( )
  • clear( )
  • contains( )
  • size( )
  • remove( )

[4] SORTEDSET

The methods of this interface are shown below.

  • Object first( )
  • Object last( )
  • Comparator comparator( )
  • SortedSet headSet(Object end)
  • SortedSet subSet(Object start, Object end)
  • SortedSet tailSet(Object start)

[5] MAP

The methods of this interface are shown below.

  • boolean containsKey(Object k)
  • boolean containsValue(Object v)
  • boolean equals(Object obj)
  • boolean isEmpty( )
  • void clear( )
  • void putAll(Map m)
  • Set entrySet( )
  • Set keySet( )
  • Collection values( )
  • int hashCode( )
  • int size( )
  • Object get(Object k)
  • Object put(Object k, Object v)
  • Object remove(Object k)

[6] SORTEDMAP

The methods of this interface are shown below.

  • SortedMap headMap(Object end)
  • SortedMap subMap(Object start, Object end)
  • SortedMap tailMap(Object start)
  • Comparator comparator( )
  • Object firstKey( )
  • Object lastKey( )

[7] MAP.ENTRY

The methods of this interface are shown below.

  • int hashCode( )
  • boolean equals(Object obj)
  • Object getKey( )
  • Object getValue( )
  • Object setValue(Object v)

[8] ENUMERATION

The methods of this interface are shown below.

  • boolean hasMoreElements( )
  • Object nextElement( )

COLLECTION CLASSES

The Collections Framework defines several classes that implements collection interfaces. These classes are listed below.

  1. AbstractCollection
  2. AbstractList
  3. AbstractSet
  4. AbstractMap
  5. AbstractSequentialList
  6. LinkedList
  7. LinkedHashSet
  8. LinkedHashMap
  9. ArrayList
  10. HashSet
  11. TreeSet
  12. HashMap
  13. TreeMap
  14. IdentityHashMap
  15. WeakHashMap

COLLECTION ALGORITHMS

Collection Algorithms contains several static methods that are used to perform certain operations that includes sorting, searching, swapping, copying etc.

Some of these methods are listed below.

  1. static int binarySearch(List list, Object value)
  2. static void copy(List list1, List list2)
  3. static void fill(List list, Object obj)
  4. static int lastIndexOfSubList(List list, List subList)
  5. static Object max(Collection c, Comparator cmp)
  6. static Object min(Collection c, Comparator cmp)
  7. static List nCopies(int num, Object obj)
  8. static void reverse(List list)
  9. static void rotate(List list, int n)
  10. static void shuffle(List list)
  11. static List singletonList(Object obj)
  12. static void sort(List list, Comparator comp)
  13. static void swap(List list, int idx1, int idx2)
  14. static List synchronizedList(List list)
  15. static Set synchronizedSet(Set s)
  16. static SortedSet synchronizedSortedSet(SortedSet ss)
  17. static List unmodifiableList(List list)
  18. static Set unmodifiableSet(Set s)
  19. static SortedSet unmodifiableSortedSet(SortedSet ss)
  20. static SortedMap synchronizedSortedMap(SortedMap sm)
  21. static Enumeration enumeration(Collection c)
  22. static int indexOfSubList(List list, List subList)

ADVANTAGES OF JAVA COLLECTIONS FRAMEWORK

  • Provides interoperability feature that enables a programmer to make less efforts in writing adapter objects and other codes.
  • The interchangeability between implementations of interfaces improves the program’s performance and results in the improved quality and speed of the program.
  • The collection interfaces and the algorithms that implements these interfaces are reusable. It means you can reuse the codes in designing other applications as well.
  • With the inclusion of collection interfaces, learning and using new API’s is very easy.
  • Designing new API’s becomes easy using these collections framework.

Also Read:- Generic Methods and Classes in Java Programming Language

We have provided you a brief description on the Collections Framework used in Java Programming Language. Hope you like this post. For more updates and related information, stay connected to our blogs on Java Programming Language.

Facebook
Twitter
LinkedIn
Pinterest