Collections in Java programming
Arrays are static in nature it means once the size of the array has been defined that can’t be changed. In array elements will be stored in indexing representation. Consider, there is an array which is of length 100 and in which 10 elements are stored. In order to access the 10 elements, we need to traverse throughout the 100 indexes which is a time taking process. In array usually, we can store one type of elements.
- Object obj = new Object;
- obj = new Hello();
- obj = new Hai();
- obj = new String(“Xadmin”);
In array there is no way to avoid the duplicate of the elements If you want to access the elements of an array in reverse order then first, we need to initialize the counter and check the condition and perform decrement that means we need to do many tasks. An array, there is no way of sorting the elements. In order to solve this problem Sun company has introduced the following classes:
These five classes are called legacy classes and these classes solved many problems such as:
- It can store different type of elements.
- It is dynamic in nature.
- Elements can be accessed in a numerous manner.
But still, it has not solved all the problem like a duplicate of elements. These above five legacy classes are completely synchronized so it reduces the concurrency of application which means at a time only one user can access the element. Stack and vector used to store different type of elements. Whereas, properties, dictionary, and hashtable are used to store elements in a form of key and value formate, which is also of no use.In order to solve the problem with the legacy class. The collection has been introduced by rearranging some legacy class, vector and hashtable are also introduced in the collection framework.
Difference between Arrays and Collections
|Fixed in size||Growable in nature|
|With respect to memory, arrays are not good||With respect to memory, collections are good|
|Performance wise arrays are good||Performance wise collections are not good|
|Homogenous in nature that means can store only the same type of elements||Both heterogeneous and homogenous that means a same and different type of elements can be stored|
|Supports the underlying data structure||Supports standard data structure|
|Using arrays we can hold primitives and object types of data||Only object type of data can be stored in collections|
What are the Collections and Collection framework:
Collection in java is a group of individuals object as a sing entity. Collection framework defines several classes and interfaces which can be used to represent a collection. The required classes and interfaces
Difference between Collection and Collections:
|It is an interface||It is a class|
|It is used to store a group of an individual object as a single entity||Collections is a utility class i.e, it comes under java.util.package and provides different methods like sorting|
Most common methods present inside the collection:
|public boolean add(Object o)||This method is used to insert an element in this collection|
|public boolean addAll(Collection c)||This method is used to insert the specified collection elements in the invoking collection|
|public boolean remove(Object o)||This method is used to delete an element from the collection|
|public boolean removeAll(Collection<?> c)||This method is used to delete all the elements of the specified collection from the invoking collection.|
|public boolean retainAll(Collection<?> c)||It is used to delete all the elements of the invoking collection except the specified collection.|
|public int size()||It returns the total number of elements in the collection|
|public void clear()||It removes the total number of elements from the collection|
|public boolean contains(Object o)||It is used to search an element|
|public Object toArray()||It converts the collection into an array|
|public boolean isEmpty()||This method checks if the collection is empty|
|public int hashcode()||This returns the hash code number of the collection|
|public boolean equals(Object element)||It matches two collections|
The Java collection framework is very powerful and includes numerous capabilities not mentioned in this article. We will learn more about the collection interface in the next articles. Please, feel free to post a comment if you find anything wrong in the article regarding the topic or you want to share more information about the topic.
READ NOW :- How to check Index Fragmentation in SQL Database