Skip to main content

Mastering the Java Collection Framework: A Guide with Examples

Java Collection Framework: A Comprehensive Guide

The Java Collection Framework provides a set of interfaces and classes for storing and manipulating collections of objects in Java. In this article, we will provide a comprehensive guide to the Java Collection Framework, covering the main types of collection classes and how to use them.

Classification of Java Collection

The Java Collection Framework can be classified into four main types of collection classes:

  • Lists: An ordered collection of elements that can contain duplicates. Examples include ArrayList, LinkedList, and Vector.
  • Sets: An unordered collection of elements that cannot contain duplicates. Examples include HashSet, TreeSet, and LinkedHashSet.
  • Queues: A collection used to hold multiple elements prior to processing. Examples include PriorityQueue and ArrayDeque.
  • Maps: A collection of key-value pairs. Examples include HashMap, TreeMap, and LinkedHashMap.

Lists

A List is an ordered collection of elements that can contain duplicates. The main implementations of the List interface in the Java Collection Framework are:

  • ArrayList: A resizable array implementation of the List interface.
  • LinkedList: A doubly-linked list implementation of the List interface.
  • Vector: A synchronized resizable array implementation of the List interface.

Here's an example of how to use an ArrayList in Java:


import java.util.ArrayList;

public class ArrayListExample {
  public static void main(String[] args) {
    ArrayList names = new ArrayList<>();
    names.add("Alice");
    names.add("Bob");
    names.add("Charlie");
    System.out.println(names);
  }
}
    

Sets

A Set is an unordered collection of elements that cannot contain duplicates. The main implementations of the Set interface in the Java Collection Framework are:

  • HashSet: A hash table implementation of the Set interface.
  • TreeSet: A red-black tree implementation of the Set interface.
  • LinkedHashSet: A linked list implementation of the Set interface that maintains insertion order.

Here's an example of how to use a HashSet in Java:


import java.util.HashSet;

public class HashSetExample {
  public static void main(String[] args) {
    HashSet names = new HashSet<>();
    names.add("Alice");
    names.add("Bob");
    names.add("Charlie");
    System.out.println(names);
  }
}
    

Queues

A Queue is a collection used to hold multiple elements prior to processing. The main implementations of the Queue interface in the Java Collection Framework are:

  • PriorityQueue: An unbounded priority queue based on a priority heap.
  • ArrayDeque: A resizable array implementation of the Deque interface.

Here's an example of how to use a PriorityQueue in Java:


import java.util.PriorityQueue;

public class PriorityQueueExample {
public static void main(String[] args) {
PriorityQueue names = new PriorityQueue<>();
names.add("Bob");
names.add("Charlie");
names.add("Alice");
System.out.println(names);
}
}

Maps

A Map is a collection of key-value pairs. The main implementations of the Map interface in the Java Collection Framework are:

  • HashMap: A hash table implementation of the Map interface.
  • TreeMap: A red-black tree implementation of the Map interface.
  • LinkedHashMap: A linked list implementation of the Map interface that maintains insertion order.

Here's an example of how to use a HashMap in Java:


import java.util.HashMap;

public class HashMapExample {
public static void main(String[] args) {
HashMap ages = new HashMap<>();
ages.put("Alice", 25);
ages.put("Bob", 30);
ages.put("Charlie", 35);
System.out.println(ages);
}
}

Conclusion

The Java Collection Framework provides a powerful set of tools for storing and manipulating collections of objects in Java. Understanding the different types of collection classes and how to use them is essential for writing effective and efficient Java code. By using the appropriate collection class for each situation, you can improve the performance and maintainability of your code.

Comments

Popular posts from this blog

JSP page directives

A jsp page directive looks like this: <%@ directive attribute="value" %> I am not gonna explain each and every page directives here . I would like to discuss about two page directives  which is import and include. First of all consider the import directive . The following simple program illustrate the use of import page directive: The output would be something looks like this: <%@ page language="java" contentType="text/html; charset=UTF-8"     pageEncoding="UTF-8"%>  <%@ page import="java.util.Date" %>   //page directive <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Jsp Basics</title> </head> <body> <%=new Date() %> </body> </html> Tue Nov 12 17:42:34 I...

a simple example for jdbc PreparedStatement

a simple example for PreparedStatement package basics.in.java.blogspot.in; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class Main { private static final String USERNAME="root"; private static final String PASSWORD=""; private static final String CONN_STRING="jdbc:mysql://localhost/basicsinjavablogspot"; public static void main(String[] args) throws ClassNotFoundException, SQLException { Class.forName("com.mysql.jdbc.Driver"); Connection conn=null; Statement stmt=null; ResultSet rs=null; try { conn= DriverManager.getConnection(CONN_STRING, USERNAME, PASSWORD); System.out.println("database connection successful"); //stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY); String sql="select * fr...