hashCode()
method, the hash code could vary between the serialized and deserialized states, leading to unexpected behavior, especially in collections like HashMap
where hash codes are used for object placement and retrieval.hashCode()
method should consistently return the same hash code for the same object during an execution of a Java application, unless one of the fields used in the computation changes. Since transient fields might not be restored to their original values upon deserialization, this consistency can be compromised.equals()
and hashCode()
mandates that if two objects are equal according to the equals(Object)
method, then calling the hashCode()
method on each of the two objects must produce the same integer result. If transient variables are part of the hashCode()
but not the equals()
check, this contract might be violated.class Example { private final int id; // Not transient private transient int counter; // Transient, do not use in hashCode public Example(int id) { this.id = id; } @Override public int hashCode() { // Use only non-transient fields return Objects.hash(id); } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null || getClass() != obj.getClass()) return false; Example example = (Example) obj; return id == example.id; } }
java.util.Collection
: The root interface for all collection classes, representing a group of objects known as elements.java.util.List
: An ordered collection (also known as a sequence) that allows duplicate elements.java.util.Set
: A collection that does not allow duplicate elements.java.util.Map
: An object that maps keys to values, with no duplicate keys allowed.java.util.Queue
: A collection designed for holding elements prior to processing, typically following FIFO (first-in, first-out) order.java.util.Deque
: A double-ended queue that allows insertion and removal of elements from both ends.java.lang.Runnable
: Represents a task that can be executed by a thread.java.util.concurrent.Callable
: Similar to Runnable
but can return a result and throw a checked exception.java.util.function.Function
: Represents a function that accepts one argument and produces a result.java.util.function.Predicate
: Represents a boolean-valued function of one argument.java.util.function.Consumer
: Represents an operation that accepts a single input argument and returns no result.java.util.function.Supplier
: Represents a supplier of results, providing a result without any input.java.util.stream.Stream
: Represents a sequence of elements supporting sequential and parallel aggregate operations.java.util.stream.IntStream
, LongStream
, DoubleStream
: Specialized streams for primitive data types.java.util.concurrent.Executor
: A simple interface that supports launching new tasks.java.util.concurrent.ExecutorService
: A more extensive interface that manages the lifecycle of asynchronous task execution.java.util.concurrent.CompletableFuture
: A Future
that may be explicitly completed and used to build complex asynchronous pipelines.java.nio.file.Path
: Represents a path in the file system.java.nio.file.attribute.BasicFileAttributes
: Interface to access a file's basic attributes.java.lang.AutoCloseable
: Defines the close()
method, which is invoked automatically on objects managed by the try-with-resources statement.java.util.Iterator
: Provides methods to iterate over a collection.java.lang.Comparable
: Allows objects to be compared for order.java.util.Comparator
: Defines a comparison function, imposing a total ordering on some collection of objects.
import java.util.HashMap; import java.util.Map; public class MapExample { public static void main(String[] args) { Map<String, Integer> map = new HashMap <> (); // Adding elements map.put("one", 1); map.put("two", 2); map.put("three", 3); // Retrieving an element System.out.println("Value for key 'two': " + map.get("two")); // Output: Value for key 'two': 2 // Checking if a key exists System.out.println("Contains key 'four': " + map.containsKey("four")); // Output: Contains key 'four': false // Size of the map System.out.println("Size of map: " + map.size()); // Output: Size of map: 3 // Iterating over entries for (Map.Entry<String, Integer> entry : map.entrySet()) { System.out.println("Key: " + entry.getKey() + ", Value: " + entry.getValue()); } } }The Map interface is incredibly versatile, serving as the foundation for various map implementations which cater to different needs in terms of performance, thread-safety, and ordering.