Hello, dear Jetto Net followers!
We continue our series on Java Object-Oriented Programming (OOP). In this chapter, we will explore a powerful tool provided by Java: the Collections Framework. Let's start by explaining what Collections are:
What are Collections?
Collections are data structures that hold multiple objects together. In Java, collections are provided through classes and interfaces located in the java.util package. Collections are more flexible and useful than arrays. While arrays have a fixed size, collections can dynamically grow or shrink.
Collection Interfaces
The Java Collections Framework provides various interfaces to represent collections. These interfaces define the basic characteristics and behaviors of collections. The most commonly used collection interfaces are:
- List : Holds elements in a sequential order. Duplicate elements are allowed. Examples: ArrayList, LinkedList.
- Set : Holds unique elements. Duplicate elements are not allowed. Examples: HashSet, TreeSet.
- Queue : Holds elements in a specific order (FIFO - First In First Out). Examples: PriorityQueue, LinkedList.
- Map : Stores data in key-value pairs. Each key is unique. Examples: HashMap, TreeMap.
Collection Classes
The Java Collections Framework provides various collection classes to meet different needs. These classes implement a specific interface and provide its characteristics and behaviors. The most commonly used collection classes are:
- ArrayList: A dynamically resizable list.
- LinkedList: A list that is faster for adding and removing elements.
- HashSet: A set that uses a hash table to quickly find elements.
- TreeSet: A set that keeps elements in a sorted order.
- HashMap: A map that uses a hash table to quickly find key-value pairs.
- TreeMap: A map that keeps keys in a sorted order.
Operations on Collections
We can perform various operations on collections. The most commonly used operations are:
- Adding: We can add elements to collections using the add() method.
- Removing: We can remove elements from collections using the remove() method.
- Updating: We can update the value of an element in a collection using the set() method.
- Searching: We can check if an element exists in a collection using the contains() method.
- Sorting: We can sort collections using the Collections.sort() method.
- Filtering: We can filter collections using lambda expressions or the stream API.
Iterator
The Iterator is an interface that allows us to traverse the elements of a collection. We can obtain an iterator for a collection using the iterator() method. The Iterator checks if more elements are available using the hasNext() method and retrieves the next element with the next() method.
Comparing Collections
The choice of which collection class to use depends on your needs and usage scenarios. For example, if you need fast access to elements, you can use ArrayList or HashMap. If frequent insertions and deletions are required, LinkedList may be more suitable. If elements need to be unique, you can use HashSet or TreeSet.
Using Collections
import java.util.*;
public class CollectionsExample {
public static void main(String[] args) {
// Using List
List<String> cities = new ArrayList<>();
cities.add("Istanbul");
cities.add("Ankara");
cities.add("Izmir");
cities.add(1, "Bursa"); // Insert at a specific index
System.out.println("Cities List: " + cities); // Cities List: [Istanbul, Bursa, Ankara, Izmir]
// Using Set
Set<Integer> numbers = new HashSet<>();
numbers.add(5);
numbers.add(3);
numbers.add(1);
numbers.add(5); // Duplicate element is not added
System.out.println("Numbers Set: " + numbers); // Numbers Set: [1, 3, 5]
// Using Queue
Queue<String> queue = new LinkedList<>();
queue.offer("Ahmet");
queue.offer("Mehmet");
queue.offer("Veli");
System.out.println("Queue: " + queue); // Queue: [Ahmet, Mehmet, Veli]
System.out.println("Name removed from Queue: " + queue.poll()); // FIFO - First In First Out
// Name removed from Queue: Ahmet
// Using Map
Map<String, Integer> licensePlates = new HashMap<>();
licensePlates.put("Istanbul", 34);
licensePlates.put("Ankara", 06);
licensePlates.put("Izmir", 35);
System.out.println("License Plates: " + licensePlates); // License Plates: {Istanbul=34, Ankara=6, Izmir=35}
// Operations on Collections
Collections.sort(cities); // Sorting the list
System.out.println("Sorted Cities List: " + cities); //Sorted Cities List: [Ankara, Bursa, Istanbul, Izmir]
boolean exists = numbers.contains(3); // Check if element exists in the set
System.out.println("Is number 3 in the set? " + exists); // Is number 3 in the set? true
// Using Iterator
Iterator<String> iterator = cities.iterator();
while (iterator.hasNext()) {
String city = iterator.next();
System.out.println(city);
}
// Ankara
// Bursa
// Istanbul
// Izmir
}
}
Display More
This example will help you understand how collections are used in Java. For more information and examples, you can check the Java Collections Framework documentation.
Conclusion
Collections are a powerful tool for managing data in Java. By learning about different collection types and operations on them, you can manage your data more effectively.
In our next article, we will continue to explore other important concepts in OOP. Feel free to leave your questions or thoughts in the comments section.
Happy Coding!