This tutorial is designed to introduce you to the Java Collections Framework, a comprehensive architecture that manipulates and stores groups of data as a single unit.
By the end of this tutorial, you will be familiar with the key interfaces and classes in the Java Collections Framework, understand how to use them to store and manipulate data, and learn best programming practices related to them.
Prerequisites:
- Basic knowledge of Java programming language.
- An understanding of Java's Object-Oriented Programming (OOP) concepts.
The Java Collections Framework consists of several core interfaces, most notably Collection and Map. The Collection interface (java.util.Collection) is used for sequences (lists and sets), and the Map interface (java.util.Map) for key/value pairs (maps).
The main subinterfaces of Collection are Set, List, and Queue. Here are some key points:
1. Set: A collection that cannot contain duplicate elements. It models the mathematical set abstraction.
2. List: A collection that can contain duplicate elements. In addition to the operations inherited from Collection, the List interface includes operations for the following: Positional access, Search, Iteration, and Range-view.
3. Queue: A collection designed for holding elements prior to processing. Queues typically, but do not necessarily, order elements in a FIFO (first-in, first-out) manner.
The Map interface maps unique keys to values. A key is an object that you use to retrieve a value at a later date.
// Create a HashMap object called capitalCities
Map<String, String> capitalCities = new HashMap<String, String>();
// Add keys and values (Country, City)
capitalCities.put("England", "London");
capitalCities.put("Germany", "Berlin");
capitalCities.put("Norway", "Oslo");
capitalCities.put("USA", "Washington DC");
import java.util.*;
public class Main {
public static void main(String[] args) {
// Creating an ArrayList
List<String> fruits = new ArrayList<String>();
// Adding elements to ArrayList
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Cherry");
fruits.add("Date");
// Printing elements
System.out.println(fruits);
}
}
Output:
[Apple, Banana, Cherry, Date]
import java.util.*;
public class Main {
public static void main(String[] args) {
// Creating a HashSet
Set<String> fruits = new HashSet<String>();
// Adding elements to HashSet
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Cherry");
fruits.add("Date");
fruits.add("Apple"); // This will not be added as Set does not allow duplicate elements
// Printing elements
System.out.println(fruits);
}
}
Output:
[Apple, Banana, Cherry, Date]
In this tutorial, we have introduced the Java Collections Framework, including the Collection and Map interfaces, and their key subinterfaces and implementations. We have also looked at how to use these interfaces and classes to store and manipulate data.
Next, you should practice using the Java Collections Framework in your own programs, and explore its other features, like the ability to sort collections, create read-only collections, and more.
Solutions:
1. java
List<Integer> numbers = new ArrayList<>();
numbers.add(1);
numbers.add(2);
numbers.add(3);
numbers.add(4);
numbers.add(5);
System.out.println(numbers); // Outputs: [1, 2, 3, 4, 5]
2. java
Set<Integer> numbers = new HashSet<>();
numbers.add(1);
numbers.add(2);
numbers.add(3);
numbers.add(4);
numbers.add(5);
numbers.add(3); // This will not be added as Set does not allow duplicate elements
System.out.println(numbers); // Outputs: [1, 2, 3, 4, 5]
3. java
Map<Integer, String> map = new HashMap<>();
map.put(1, "One");
map.put(2, "Two");
map.put(3, "Three");
System.out.println(map); // Outputs: {1=One, 2=Two, 3=Three}
Keep practicing to get more comfortable with Java Collections!