List
Interface
ArrayList
LinkedList
Vector
Stack
Set
Interface
HashSet
LinkedHashSet
TreeSet
(implements NavigableSet
, which extends SortedSet
)Queue
Interface
LinkedList
(also implements List
)PriorityQueue
ArrayDeque
Interface | Allows Duplicates | Ordered? | Common Classes |
---|---|---|---|
List |
Yes | Yes | ArrayList , LinkedList , Vector , Stack |
Set |
No | No* | HashSet , LinkedHashSet , TreeSet |
Queue |
Yes | Varies | LinkedList , PriorityQueue , ArrayDeque |
HashMap
stores elements as key-value pairs. You can use the serial number as the key and the actual object as the value.HashMap
provides, on average, constant-time performance (O(1)) for get()
operations (retrieving a value based on its key). This means that searching for an object by its serial number will be very fast, even with a large number of objects in the Collection
.HashMap
ensures that all keys are unique. This aligns well with the concept of a serial number, which should typically uniquely identify an object.HashMap
:
import java.util.HashMap; import java.util.Map; // Assuming your objects have a getSerialNumber() method Map<String, YourObjectType> objectMap = new HashMap<>();
HashMap
: Iterate through your objects and put them into the map using their serial number as the key.
for (YourObjectType object : listOfObjects) { objectMap.put(object.getSerialNumber(), object); }
get()
method of the HashMap
to retrieve the object based on its serial number.
String searchSerialNumber = "SN12345"; YourObjectType foundObject = objectMap.get(searchSerialNumber); if (foundObject != null) { System.out.println("Found object with serial number: " + searchSerialNumber); // Process the found object } else { System.out.println("Object with serial number " + searchSerialNumber + " not found."); }
List
(e.g., ArrayList
, LinkedList
): Searching in a List
typically requires iterating through the elements until the desired object is found (O(n) time complexity), which is inefficient for frequent lookups.Set
(e.g., HashSet
, TreeSet
): While HashSet
offers fast average-case performance for contains()
(checking if an element exists), it's not designed for directly retrieving an object based on a specific attribute like a serial number without iterating. TreeSet
maintains elements in a sorted order, which doesn't inherently speed up searching by an arbitrary attribute.TreeMap
: TreeMap
keeps entries sorted by key. While it offers logarithmic time complexity (O(log n)) for lookups, HashMap
's constant-time average performance is generally better for direct key-based searches.
Integer x = 10; // int → Integer
int y = x; // Integer → int
List<Integer> list = new ArrayList<>(); list.add(100); // Autoboxed int num = list.get(0); // Unboxed
inline classes
) to reduce autoboxing penalties.var
, and Java SE 11–22 enhanced type inference for lambda expressions.Integer a = 127; Integer b = 127; System.out.println(a == b); // true due to caching Integer c = 128; Integer d = 128; System.out.println(c == d); // false (different objects)
Aspect | Java SE 5 | Java SE 22 |
---|---|---|
Autoboxing/Unboxing syntax | ✔️ Introduced | ✔️ Unchanged |
Performance optimizations | 🚫 Minimal | ✅ Improved |
Wrapper class enhancements | Basic | Some new APIs but not core conversion |
Type inference impact | 🚫 | ✅ Some |
Valhalla/Value types | 🚫 | 🧪 Incubating |