In this post, we will explore Java collections objects, which represent data structures in the programming language. Collections serve as containers for individual elements, storing them as a single unit. Java collections are objects that hold multiple elements in a specific order, facilitating easy retrieval. The Java collections framework comprises interfaces and classes designed for implementing various data structures and algorithms. Java Collections support a wide range of data operations, including searching, sorting, insertion, manipulation, and deletion.
Hierarchy of Java Collection
The collection classes and interfaces are found in the java.util
package. This package provides a range of classes and interfaces that facilitate various data structures and algorithm implementations. There are three main levels of Java collections: Iterable, Map, and Iterator.
Within Iterable, three types of collection classes exist: List, Set, and Queue. Lists store ordered collections, sets store unordered collections, and queues store sequential collections.
Maps use a key-value model to store objects. The key is a unique identifier used to retrieve values stored in the map. Maps are primarily employed to expedite data retrieval in Java collections and can be stored in a sorted or unsorted manner.
Iterators are used to retrieve all elements in a data structure. They offer a method to obtain all values one by one and can be applied to both lists and maps.
Java Interfaces
In the Java util
package, several key interfaces for Java collections are supported. Here are the main interfaces:
- Collection Interface: This is a top-level interface that encompasses all other collection interfaces. It is implemented by all collection classes and contains methods required by all such classes.
- List Interface: The List interface is employed to create an ordered collection of objects. It allows for duplicate values, and elements can be accessed using their index in the collection.
- Set Interface: Use the Set interface to create an unordered collection of objects. It does not permit duplicate values.
- Queue Interface: The Queue interface represents a queue mechanism, following the first-in, first-out (FIFO) model. Conversely, a stack uses the first-in, last-out (FILO) model.
- Iterator Interface: This interface is designed for sequential collections. It provides a means to access all elements in a collection in a specific order.
Java Array List
The ArrayList
class in the Java util
package is designed to store elements of the same data type in a sequential model. Elements in the ArrayList can be accessed using their index, providing a way to identify and manipulate the data. This class supports operations such as adding, removing, deleting, and modifying elements. Additionally, it facilitates iteration through all elements using their respective indices. The ArrayList
is a widely used data structure in the Java programming language.
package com.test; import java.util.ArrayList; public class JavaTest { public static void main(String[] args) { ArrayList list = new ArrayList(); list.add(10); list.add(20); list.add(30); list.add(40); System.out.println(list); } }
Java Hash Map
The Java HashMap
is employed to store elements in key-value pairs, where a unique key is utilized to identify the associated values. Retrieving values is efficiently performed using the corresponding key. The HashMap
in Java supports iteration over both keys and values. To optimize value retrieval, the hash mechanism is employed for storing values in hash maps. In Java programming, hash maps are commonly utilized for managing key-value pairs.
package com.test; import java.util.HashMap; public class JavaTest { public static void main(String[] args) { HashMap<Integer, String> map = new HashMap<Integer, String>(); map.put(1, "One"); map.put(2, "Two"); map.put(3, "Three"); System.out.println(map); } }