0% found this document useful (0 votes)
3 views2 pages

Java Hashtable: Features & Examples

A Hashtable in Java is a synchronized data structure that stores unique key-value pairs and is part of the Java Collections Framework. It is thread-safe, does not allow null keys or values, and offers fast lookups due to its hashing technique. However, it is slower than HashMap due to its synchronization and has limitations on null entries.

Uploaded by

adilmohd7799
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

Java Hashtable: Features & Examples

A Hashtable in Java is a synchronized data structure that stores unique key-value pairs and is part of the Java Collections Framework. It is thread-safe, does not allow null keys or values, and offers fast lookups due to its hashing technique. However, it is slower than HashMap due to its synchronization and has limitations on null entries.

Uploaded by

adilmohd7799
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

**Hashtable in Java**

A **Hashtable** in Java is a data structure that stores key–value pairs. It is part of the Java Collections
Framework and works similarly to HashMap but is **synchronized**, meaning it is thread-safe.

### **Features of Hashtable** 1.

Stores data in key-value pairs.

2. Keys are unique.

3. Does not allow null keys or null values.

4. Thread-safe (synchronized).

5. Based on hashing technique.

### **Syntax**

```java

Hashtable hashtable = new Hashtable<>();

```

### **Example**

```java import

[Link];

public class HashtableExample {

public static void main(String[] args) {

Hashtable table = new Hashtable<>();

[Link](1, "Apple");

[Link](2, "Banana");

[Link](3, "Cherry");
[Link]("Value at key 2: " + [Link](2));

[Link]("All elements:");

for (Integer key : [Link]()) {

[Link](key + " => " + [Link](key));

```

### **Output**

```

Value at key 2: Banana

All elements: 1 =>

Apple

2 => Banana

3 => Cherry

```

### **Advantages**

- Thread-safe

- Fast lookup due to hashing

- Good for multi-threaded applications

### **Disadvantages**

- Slower than HashMap because it is synchronized

- Does not allow null keys or values

You might also like