Java Intermediate

0% completed

Previous
Next
Basics of LinkedHashMap Class

The LinkedHashMap class in Java is a hash table and linked list implementation of the Map interface. It maintains a predictable iteration order, which is typically the order in which keys were inserted. This makes it particularly useful when you need both the fast performance of a HashMap and the ability to iterate through the entries in a defined order.

Image

In this lesson, we'll cover the key characteristics of LinkedHashMap, its syntax, and provide examples demonstrating its basic operations.

Key Characteristics

  • Implements Map Interface: LinkedHashMap implements the Map interface, so it provides all the standard methods for handling key-value pairs.
  • Maintains Insertion Order: Unlike a standard HashMap, LinkedHashMap maintains a doubly-linked list of its entries. This ensures that the iteration order is predictable (by insertion order by default).
  • Efficient Operations: Like HashMap, LinkedHashMap offers fast performance for basic operations (add, remove, contains, and get), typically in constant time (O(1)).
  • Optional Access Order: Besides maintaining insertion order, LinkedHashMap can be configured to maintain access order (the order in which entries were last accessed), which is useful for implementing caches.

Syntax

Basic Syntax to Create a LinkedHashMap:

LinkedHashMap<KeyType, ValueType> map = new LinkedHashMap<KeyType, ValueType>();
  • Explanation:
    • LinkedHashMap<KeyType, ValueType> declares a LinkedHashMap that stores keys of type KeyType and values of type ValueType.
    • The constructor initializes an empty LinkedHashMap that will maintain the order of insertion.

Additional Constructors:

You can also specify an initial capacity and load factor, similar to HashMap:

LinkedHashMap<KeyType, ValueType> map = new LinkedHashMap<KeyType, ValueType>(initialCapacity, loadFactor);
  • Explanation:
    • Initial Capacity: Determines the number of buckets initially allocated.
    • Load Factor: Determines when the LinkedHashMap should be resized (commonly set to 0.75).

Example: Basic Operations with LinkedHashMap

In this example, we create a LinkedHashMap to store student names (keys) and their corresponding grades (values). We add several entries, and then we iterate over the map to display the key-value pairs in the order they were inserted.

Java
Java

. . . .

Example Explanation:

  • LinkedHashMap Creation:
    • A LinkedHashMap<String, Integer> named studentGrades is created.
  • Inserting Elements:
    • Key-value pairs are inserted using put(), preserving the order of insertion.
  • Accessing an Element:
    • The get("Alice") method retrieves the grade for Alice.
  • Iteration:
    • The entrySet() method is used to iterate over the map, ensuring that entries are processed in the order they were added.
  • Outcome:
    • The program prints Alice's grade and then displays all student grades in the order they were inserted.

By understanding these concepts, you can effectively use LinkedHashMap in your Java applications to maintain order while leveraging the advantages of hash-based data structures.

.....

.....

.....

Like the course? Get enrolled and start learning!
Previous
Next