Generics in Java
Generics in Java
Generics in Java provide a way to define classes, interfaces, and methods with type parameters. They
allow for type safety, meaning you can enforce that certain data types are used in your collections
and other classes. This helps catch errors at compile time, rather than at runtime, improving code
reliability.
Generics allow you to write code that works with any type of object, while maintaining strict type
safety. Instead of using raw types (like Object), you can specify the type of elements contained within
a collection or passed to a method.
// A generic class
class Box<T> {
private T value;
public T getValue() {
return value;
this.value = value;
integerBox.setValue(42);
stringBox.setValue("Hello, Generics");
}
Here, the Box class is a generic class where T represents the type of object it will store. The T could
be any type like Integer, String, etc.
Collections in Java:
Collections in Java are part of the Java Collections Framework (JCF) and provide a set of classes and
interfaces to store and manipulate groups of objects. Collections are primarily used to hold data and
perform various operations like searching, sorting, and inserting.
• List: An ordered collection that allows duplicates. Examples include ArrayList, LinkedList.
• Set: A collection that does not allow duplicates. Examples include HashSet, TreeSet.
• Queue: A collection that follows the FIFO (First In First Out) principle. Examples include
LinkedList, PriorityQueue.
• Map: A collection that stores key-value pairs. Examples include HashMap, TreeMap.
Both ArrayList and Vector are classes in Java that implement the List interface and allow storing a
dynamic collection of elements. However, they differ in some key aspects:
1. Synchronization:
• ArrayList: ArrayList is not synchronized. This means it is not thread-safe, and if multiple
threads access the same ArrayList concurrently, external synchronization (e.g., using
Collections.synchronizedList()) is needed to prevent issues.
• ArrayList: By default, ArrayList grows by 50% of its current size when it reaches its capacity.
• Vector: Vector grows by doubling its size when its capacity is exceeded.
3. Performance:
• ArrayList: ArrayList is generally faster than Vector because it is not synchronized, and its
resizing strategy is more efficient.
4. Legacy:
• ArrayList: ArrayList is a part of the Java Collections Framework and is generally preferred for
most scenarios in modern Java development.
• Vector: Vector is part of the original JDK and is considered a legacy class. It's less commonly
used today in favor of ArrayList and other more modern collections.
Example Comparison:
Using ArrayList:
import java.util.ArrayList;
list.add("Apple");
list.add("Banana");
list.add("Cherry");
// Access elements
// Remove an element
list.remove("Banana");
Using Vector:
import java.util.Vector;
vector.add("Apple");
vector.add("Banana");
vector.add("Cherry");
// Access elements
vector.remove("Banana");
Growth
Grows by 50% of its current size Doubles its size when full
Strategy
Summary:
• Generics in Java allow you to define types that work with different data types while
maintaining type safety.
• Collections in Java are powerful and flexible tools for working with groups of objects, with
classes like ArrayList, Vector, HashSet, HashMap, and others.
• ArrayList is preferred over Vector in most modern applications due to its better
performance, though Vector still has use cases in multi-threaded environments where
synchronization is required.