Understanding Collection in Java

In Java, the Collection framework serves as a comprehensive set of classes and interfaces that offer efficient ways to store, manipulate, and retrieve groups of objects. It provides a unified architecture that abstracts the underlying data structures, enabling developers to work with collections of objects in a consistent manner.
The framework includes several core interfaces such as List, Set, Queue, and Map, each tailored to specific requirements:

1.List:Lists allow for ordered collections of elements, where duplicates are permitted. Implementations include ArrayList, LinkedList, and Vector.

i.ArrayList:In Java, ArrayList is a dynamic array-based implementation of the List interface provided by the Collection framework. It offers resizable arrays, allowing for the addition and removal of elements while automatically handling resizing and capacity management.
Example of ArrayList:

#Write a Java program to create a new array list, add some elements (string) and print out the collection. import java.util.*; public class Exercise1 { public static void main(String[] args) { List<String> list_Strings = new ArrayList<String>(); //we can write List or ArrayList - same thing. Symbol of array which is in form of a list: [ ... ] list_Strings.add("Red"); list_Strings.add("Green"); list_Strings.add("Orange"); list_Strings.add("White"); list_Strings.add("Black"); System.out.println(list_Strings); } }

OUTPUT: [Red, Green, Orange, White, Black]

ii.LinkedList: LinkedList is a class that implements the List interface and represents a doubly linked list. It provides a flexible data structure for storing elements with dynamic sizing.

Example of LinkedList:

//Write a Java program to append the specified element to the end of a linked list. import java.util.LinkedList; //similar to ArrayList but capacity is unlimited. public class Exercise1 { public static void main(String[] args) { // create an empty linked list LinkedList<String> l_list = new LinkedList<String>(); // use add() method to add values in the linked list l_list.add("Java"); l_list.add("C#"); l_list.add("C"); l_list.add("C++"); l_list.add("JavaScript"); l_list.add("CSS"); // print the list System.out.println("The linked list: " + l_list); } }

OUTPUT:
The linked list: [Java, C#, C, C++, JavaScript, CSS]

2.Set:Set is a collection interface representing an unordered collection of unique elements. It prohibits duplicates and does not maintain insertion order. Implementations include HashSet, TreeSet, and LinkedHashSet. HashSet uses hashing for storage, TreeSet utilizes a red-black tree, and LinkedHashSet maintains insertion order. Sets are commonly used for ensuring uniqueness and performing membership tests efficiently in Java collections.


a.HashSet:

Example of Set in Java:

import java.util.HashSet; public class Exercise1 { public static void main(String[] args) { // Create a empty hash set HashSet<String> h_set = new HashSet<String>(); // use add() method to add values in the hash set h_set.add("Red"); h_set.add("Green"); h_set.add("Black"); h_set.add("White"); h_set.add("Pink"); h_set.add("Yellow"); // print the hash set System.out.println("The Hash Set: " + h_set); } }
  • Conclusion of Collection:
  • In Java, collections are vital for managing data efficiently. Offering dynamic resizing, type safety, and polymorphic behavior, they ensure versatility. With standardization and diverse data structures, they cater to varied needs. Integrated with algorithms and iterators, they simplify tasks like sorting and processing. Collections empower developers to write cleaner, high-performance code, promoting productivity and scalability in Java applications.