Java Collections Framework: A Basic Overview

The Collection is a framework in Java that enables architecture to manipulate and store a group of objects.

Java Collection can do all those operations120 and help to achieve data execution, like sorting, manipulation, searching, insertion, and deletion.

The Collection framework offers a variety of interfaces (List, Set, Deque, Queue), and classes (ArrayList, LinkedList, PriorityQueue, Vectors, TreeSet, HashSet).  

Let’s take a look into this article to get an overview of the Java Collections Frameworks in detail. 

Components Inside Java Collections Framework

As mentioned above, the collection in the field of java helps to store as well as handle different groups of objects.

There is an extensive range of components in Java Collections Framework and this Framework consists of these following things:-

Collection Interfaces

The data types representing the Collections are abstract. However, the interface allows the independent manipulation of the collections despite the details of their representation. Interfaces form a hierarchy in the object-oriented languages. 

The collection framework consists of various types of interfaces. They are as follows:-
1. The Collection Interface- This is the root of the interface. It provides the general-purpose methods which are followed by all the classes. In addition, it is used by all the other collection classes as well.
2.The List Interface- List is a collection of elements. With its help, we can access elements using their integer index. Some important List interfaces are- LinkedList, ArrayList, CopyOnWriteArrayList, Vector, and Stack.
3.The Set- It is a collection of elements, but does not contain any duplicate elements. Apart from that, it does not guarantee any return of the element  in a predicted order.
4. The Map-This interface allows us to store data in key-value pairs and does not contain any duplicate keys. Every key of the map interface has only one value.
5.The Map.Entry- Basically, this defines an element inside a map and is an inner class of Map. 
6.The SortedMap- This helps the Map to maintain the keys in ascending order. 
7.The Enumeration- This is a legacy interface that helps you to distinguish the elements in a collected group and is now surpassed by Iterator.

The Collection Classes

Java provides a category of basic classes, that can accomplish the Collection interfaces. Some of these classes can be used as it is, while others are of abstract class and offer only a skeletal structure.

However, this performance is the beginning of making a concrete collection of the following different types of collection classes:-

1.AbstractCollection- This is used for implementing the Collection interface.
2.AbstractList- It helps to implement most of the List interface.
3.AbstractSequentialList- It allows the AbstractList to be used by collection, which uses sequential elements. 
4.LinkedList- It employs a linked list by enlarging the AbstractSequential list.
5.ArrayList- Applies a strong array by expanding the AbstractList. 
6.AbstractSet- It enlarges the AbstractCollection and employs most of the Set interface.
7.HashSet- Expands the AbstractSet to be used with a hash table.
8.AbstractMap- Applies all the Map interface.
9.HashMap- It enlarges the AbstractMap to be used as a hash table.

The collection framework distinguishes a number of algorithms which can be added to the collections and maps. These algorithms are known as static methods inside the Collections class. 

Collections have 3 variables: EMPTY_LIST,  EMPTY_SET, and EMPTY_MAP. These variables cannot be altered. The methods of the collection framework’s algorithm are given below:-

1.static int binarySearch (List lit, Objective value, Comparator c)-According to c, search for the value in the list. Now, go back to the position of the value in the list, if you still can’t find the value then go to- 1. 

2. static ArrayList list (Enumeration enum)- Do not support an ArrayList that has the elements of enum.

3. static Object max(Collection c, Comparator comp)- Returns the majority of elements in c as set by the comp.

4. static Object max(Collection c)- Sends back the maximum amount of element in c as set by comp. This does not require to be classified.

5. static Object min(Collection c, Comparator comp)- Returns the least number of elements in c as set by comp. The collection does not need to be classified.

6. static Object mini(Collection c)- Returns the least amount of elements in c as set by natural ordering.

7. static List nCopies(int nun, Object obj)- Returns the number of copies of obj contained in a fixed list. However, the number must be equal to or greater than zero. 

8. static void reverse(List list)- It reverses the sequence in the list.

9. static Comparator reverseOrder( )-It returns a reverse comparator.

10. static void rotate(List list, int n)- It moves the list by n places to the right. If you want to move it to the left, use a negative value for n

11. static void shuffle(List list, Random r)- It jumbles the elements contained in the list by using r as a source of random numbers.

12. static void shuffle(List list)- It shuffles all the elements in the list.

13. static Set singleton(Object obj)- It sends back obj as an unchangeable set. It is the easiest way of converting a single object into a set.

14. static List singletonList(Object obj)- It sends back obj as an unchangeable set. It is the easiest way of converting a single object into a list.

15. static Map singletonMap(Object k, Object v)-It returns the key/value pair k/v as an unalterable map. This is one of the simplest ways of converting a single key/value pair into a map.

16. static void sort(List list, Comparator comp)- It helps to distinguish the elements in the list as decided by comp.

17. static void sort(List list)-It helps to sort the elements in the list as decided by their natural ordering.

18. static void swamp(List list, int idx1, int idx2)- It interchanges the elements in the list at the indices fixed by idx1 and idx2.

19. static Collection synchronizedCollection(Collection c)- Sends back a thread-safe collection backed by c.

20.static List synchronizedList(List list)- Sends back a thread-safe list supported by list.

 21. static Map synchronizedMap(Map m)- Sends back a thread-safe map supported by m.

22. static Set synchronizedSet(Set s)- Sends back a thread-safe not supported by s

23. static SortedMap synchronizedSortedMap(SortedSet sm)- Sends back a thread-safe categorized by sm.

24. static SortedSet synchronizedSortedSet(SortedSet ss)- Sends back a thread-safe set supported by c.

25. static Collection unmodifiableCollection(Collection c)- Sends back an unchanged collection supported by c.

26. static List unmodifiableList(List list)- Returns an unchanged map backed by m.

27. static Map unmodifiableMap(Map m)- Returns an unchanged map backed by m.

28. static Set unmodifiableSet(Set s)- Returns an unchanged set backed by s. 

29. static SortedMap unmodifiableSortedMap(SortedMap sm)- Returns an unchanged sorted map backed by sm.

30. static SortedSet unmodifiableSortedSet (SortedSet ss)- Returns an unchanged sorted set backed by ss.

Procedure of Using an Iterator

There will be instances where you will need a rotation through the elements in a collection. Nevertheless, using an iterator will be a rather easy process. It assists you in applying the Iterator or the ListIterator interface. 

The iterator will help you to go through a collection and acquire or separate elements. Listiterator helps in expanding the Iterator to allow the traveling of the list in a bidirectional manner and improve the elements.

Using a Comparator   

TreeSet and TreeMap can arrange the elements in a properly sorted manner. However, the comparator is the thing that describes the meaning of sorted order. 

This interface allows us to sort any collection in many different ways. However, you can also use it for sorting any classes ( even classes that cannot be modified).

Conclusion

The Java Collections Framework helps the programmer to a huge extent. It grants them access to the prepackaged data structure and provides algorithms to control them.

Including that, the collection provides access to various other objects. Its interfaces proclaim the activities that can be done on each kind of collection.

Hopefully, this article was helpful enough to enlighten you about the Java collections framework.

Leave a Reply

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