Sitemap
AnjaliKesherwani

AnjaliKesherwani

I possess a solid foundation in HTML, CSS, Bootstrap, JavaScript, MySQL, MongoDB, PHP, Laravel, Python, Django, Machine Learning, java, and MEAN Stack.

COLLECTION IN JAVA

2 min readJul 2, 2024

--

The Java Collections Framework provides a comprehensive set of interfaces and classes for managing collections of objects. These collections allow for efficient storage, retrieval, manipulation, and communication of aggregate data.

Key Interfaces in the Java Collections Framework

  1. Collection Interface: The root interface of the collection hierarchy. Various sub-interfaces and classes implement this interface to provide different collection functionalities.
  • List: An ordered collection that can contain duplicate elements. Implementations include:
  • ArrayList
  • LinkedList
  • Vector
  • Stack
  • Set: A collection that does not allow duplicate elements. Implementations include:
  • HashSet
  • LinkedHashSet
  • TreeSet
  • Queue: A collection used to hold multiple elements prior to processing. It follows the First-In-First-Out (FIFO) principle. Implementations include:
  • PriorityQueue
  • LinkedList
  • Map: An object that maps keys to values. Although not directly extending the Collection interface, it is a part of the framework. Implementations include:
  • HashMap
  • TreeMap
  • LinkedHashMap
  • Hashtable

Get AnjaliKesherwani’s stories in your inbox

Join Medium for free to get updates from this writer.

About ArrayList

ArrayList is a resizable array implementation of the List interface. It allows for dynamic resizing, providing flexibility over standard arrays.

  • void add(int index, E element): Inserts the specified element at the specified position in this list.
  • boolean add(E element): Appends the specified element to the end of this list.
  • E get(int index): Returns the element at the specified position in this list.
  • E remove(int index): Removes the element at the specified position in this list.
  • int size(): Returns the number of elements in this list.
import java.util.ArrayList;

public class ArrayListExample {
public static void main(String[] args) {
// Create an ArrayList
ArrayList<String> fruits = new ArrayList<>();

// Add elements to the ArrayList
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Cherry");

// Print the ArrayList
System.out.println("Fruits: " + fruits);

// Get element at index 1
String fruit = fruits.get(1);
System.out.println("Fruit at index 1: " + fruit);

// Remove element at index 2
fruits.remove(2);
System.out.println("Fruits after removal: " + fruits);

// Iterate over the ArrayList
System.out.println("Iterating over ArrayList:");
for (String f : fruits) {
System.out.println(f);
}
}
}
Press enter or click to view image in full size
Outputs

Conclusion

The ArrayList class in Java provides a flexible and dynamic way to handle collections of objects. Its ability to resize automatically and provide easy access methods makes it an essential tool for developers. The Java Collections Framework, with its variety of interfaces and classes, offers powerful and versatile data structures that significantly enhance the capability to manage collections in Java applications. Understanding and using these collections effectively can lead to more efficient and maintainable code.

--

--

Written by AnjaliKesherwani

I possess a solid foundation in HTML, CSS, Bootstrap, JavaScript, MySQL, MongoDB, PHP, Laravel, Python, Django, Machine Learning, java, and MEAN Stack.