Advanced Java 02 - Collections
Advanced Java 02 - Collections
Agenda
Intro to Collections
Intro to Iterators
Using Iterators
Iterator Methods
Additonal Concepts
Introduction
Java Collections Framework provides a set of interfaces and classes to store and
manipulate groups of objects. Collections make it easier to work with groups of
objects, such as lists, sets, and maps. In this beginner-friendly tutorial, we'll
explore the basics of Java Collections and how to use iterators to traverse through
them.
Queue:: The Queue interface in Java is part of the Java Collections Framework and
extends the Collection interface. Queues typically, but do not necessarily, order
elements in a FIFO (first-in-first-out) manner. Among the exceptions are priority
queues, which order elements according to a supplied comparator, or the elements'
natural ordering. Implementations include ArrayDeque, LinkedList, PriorityQueue etc.
Set: An unordered collection that does not allow duplicate elements. Implementations
include HashSet, LinkedHashSet, and TreeSet.
import java.util.ArrayList;
import java.util.List;
import java.util.HashSet;
import java.util.Set;
import java.util.HashMap;
import java.util.Map;
Introduction to Iterators
An iterator is an interface that provides a way to access elements of a collection one
at a time. The Iterator interface includes methods for iterating over a collection and
retrieving elements.
Let's see how to use iterators with a simple example using a List:
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
// Getting an iterator
Iterator<String> iterator = myList.iterator();
n this example, we create a PriorityQueue of integers and add three elements to it. We
then use an Iterator to iterate over the elements and print them.
Keep in mind that when using a PriorityQueue, the order of retrieval is based on the
natural order (if the elements are comparable) or a provided comparator. The element
with the highest priority comes out first.
It's important to note that the iterator does not guarantee any specific order when
iterating over the elements of a PriorityQueue.
Iterator Methods
The Iterator interface provides several methods, including:
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
Additional Concepts
Hashmap with Custom Objects
Using a HashMap with custom objects in Java involves a few steps. Let's go through the
process step by step. Suppose you have a custom object called Person with attributes
like id, name, and age.
@Override
public String toString() {
return "Person{id=" + id + ", name='" + name + "', age=" + age + '}';
}
}
Step 2: Use Person as a Key in HashMap Now, you can use Person objects as keys
in a HashMap. For example:
import java.util.HashMap;
import java.util.Map;
// Add entries
Person person1 = new Person(1, "Alice", 25);
Person person2 = new Person(2, "Bob", 30);
personMap.put(person1, "Employee");
personMap.put(person2, "Manager");
In this example, Person objects are used as keys, and the associated values represent
their positions. Note that for keys to work correctly in a HashMap, the custom class
(Person in this case) should override the hashCode() and equals() methods.
@Override
public int hashCode() {
return Objects.hash(id, name, age);
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
By overriding these methods, you ensure that the HashMap correctly handles collisions
and identifies when two Person objects are considered equal.
Important Considerations
Immutability: It's often a good practice to make the custom objects used as keys
immutable. This helps in maintaining the integrity of the HashMap because the keys
should not be modified after being used.
Consistent hashCode():
Ensure that the hashCode() method returns the same value for two objects that are
considered equal according to the equals() method. This ensures proper functioning of
the HashMap.
Performance: Consider the performance implications when using complex objects as keys.
If the hashCode() and equals() methods are computationally expensive, it might affect
the performance of the HashMap. By following these steps and considerations, you can
effectively use custom objects as keys in a HashMap in Java.
Summary
Java Collections and Iterators are fundamental concepts for handling groups of objects
efficiently. Understanding the different collection interfaces, implementing classes,
and utilizing iterators will empower you to work with collections effectively in your
Java applications. Practice and explore the various methods available in the
Collections Framework to enhance your programming skills.
-- End --