Tuesday, 9 May 2023

LinkedHashTable implementation in Java

HashTable

HashTable is a data structure that maps keys to values. Any non-null object can be used as a key or as a value.

 

HashTable vs HashMap

HashMap is almost equivalent to HashTable, except the HashMap is not synchronized and allow null values as key.

 

LinkedHashMap

LinkedHashMap is another Map implementation, where it maintain the insertion of entires.

 


Implementation of LinkedHashTable

By wrapping the LinkedHashMap, we can implement LinkedHashTable effectively.

public class LinkedHashtable<K, V> extends Hashtable<K, V> {

	private final LinkedHashMap<K, V> linkedHashMap;

}

Constructors definition

public LinkedHashtable() {
	linkedHashMap = new LinkedHashMap<>();
}

public LinkedHashtable(int initialCapacity) {
	linkedHashMap = new LinkedHashMap<>(initialCapacity);
}

public LinkedHashtable(int initialCapacity, float loadFactor) {
	linkedHashMap = new LinkedHashMap<>(initialCapacity, loadFactor);
}

public LinkedHashtable(Map<? extends K, ? extends V> map) {
	linkedHashMap = new LinkedHashMap<>(map);
}

public LinkedHashtable(int initialCapacity, float loadFactor, boolean accessOrder) {
	linkedHashMap = new LinkedHashMap<>(initialCapacity, loadFactor, accessOrder);
}

Search operations

@Override
public synchronized boolean contains(Object value) {
	return containsKey(value);
}

@Override
public synchronized boolean containsKey(Object value) {
	return linkedHashMap.containsKey(value);
}

@Override
public synchronized boolean containsValue(Object value) {
	return linkedHashMap.containsValue(value);
}

Get operations

@Override
public synchronized Set<Map.Entry<K, V>> entrySet() {
	return linkedHashMap.entrySet();
}

@Override
public synchronized V get(Object k) {
	return linkedHashMap.get(k);
}

@Override
public synchronized Enumeration<V> elements() {
	return Collections.enumeration(values());
}

@Override
public Enumeration<K> keys() {
	return Collections.enumeration(keySet());
}

@Override
public synchronized Set<K> keySet() {
	return linkedHashMap.keySet();
}

@Override
public synchronized Collection<V> values() {
	return linkedHashMap.values();
}

Update operation

@Override
public synchronized V put(K k, V v) {
	return linkedHashMap.put(k, v);
}

@Override
public synchronized void putAll(Map<? extends K, ? extends V> m) {
	linkedHashMap.putAll(m);
}

Removal operation

@Override
public synchronized V remove(Object k) {
	return linkedHashMap.remove(k);
}

Find the below working application.

 

LinkedHashtable.java

package com.sample.app.collections;

import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

public class LinkedHashtable<K, V> extends Hashtable<K, V> {
	private static final long serialVersionUID = 94349865567L;

	private final LinkedHashMap<K, V> linkedHashMap;

	// Constructors
	public LinkedHashtable() {
		linkedHashMap = new LinkedHashMap<>();
	}

	public LinkedHashtable(int initialCapacity) {
		linkedHashMap = new LinkedHashMap<>(initialCapacity);
	}

	public LinkedHashtable(int initialCapacity, float loadFactor) {
		linkedHashMap = new LinkedHashMap<>(initialCapacity, loadFactor);
	}

	public LinkedHashtable(Map<? extends K, ? extends V> map) {
		linkedHashMap = new LinkedHashMap<>(map);
	}

	public LinkedHashtable(int initialCapacity, float loadFactor, boolean accessOrder) {
		linkedHashMap = new LinkedHashMap<>(initialCapacity, loadFactor, accessOrder);
	}

	// Search operations
	@Override
	public synchronized boolean contains(Object value) {
		return containsKey(value);
	}

	@Override
	public synchronized boolean containsKey(Object value) {
		return linkedHashMap.containsKey(value);
	}

	@Override
	public synchronized boolean containsValue(Object value) {
		return linkedHashMap.containsValue(value);
	}

	// Get operations
	@Override
	public synchronized Set<Map.Entry<K, V>> entrySet() {
		return linkedHashMap.entrySet();
	}

	@Override
	public synchronized V get(Object k) {
		return linkedHashMap.get(k);
	}

	@Override
	public synchronized Enumeration<V> elements() {
		return Collections.enumeration(values());
	}

	@Override
	public Enumeration<K> keys() {
		return Collections.enumeration(keySet());
	}

	@Override
	public synchronized Set<K> keySet() {
		return linkedHashMap.keySet();
	}

	@Override
	public synchronized Collection<V> values() {
		return linkedHashMap.values();
	}

	@Override
	public synchronized V put(K k, V v) {
		return linkedHashMap.put(k, v);
	}

	@Override
	public synchronized void putAll(Map<? extends K, ? extends V> m) {
		linkedHashMap.putAll(m);
	}

	// Removal operation
	@Override
	public synchronized V remove(Object k) {
		return linkedHashMap.remove(k);
	}

	@Override
	public synchronized boolean isEmpty() {
		return linkedHashMap.isEmpty();
	}

	@Override
	public synchronized int size() {
		return linkedHashMap.size();
	}

	public synchronized void clear() {
		linkedHashMap.clear();
	}

	@Override
	public synchronized boolean equals(Object o) {
		return linkedHashMap.equals(o);
	}

	@Override
	public synchronized int hashCode() {
		return linkedHashMap.hashCode();
	}

	@Override
	public synchronized String toString() {
		return linkedHashMap.toString();
	}

}

LinkedHashtableDemo.java

package com.sample.app.collections;

import java.util.Hashtable;
import java.util.Map;

public class LinkedHashtableDemo {

	public static void main(String[] args) {
		Hashtable<String, Integer> numbers = new LinkedHashtable<String, Integer>();
		numbers.put("one", 1);
		numbers.put("three", 3);
		numbers.put("two", 2);

		for (Map.Entry<String, Integer> entry : numbers.entrySet()) {
			System.out.println(entry.getKey() + "-->" + entry.getValue());
		}
	}

}

Output

one-->1
three-->3
two-->2

 

You may like

Interview Questions

Collection programs in Java

Array programs in Java

Implement stack data structure using List

Get a composite unmodifiable array list from two arrays

Join the collection elements by a separator using streams

Get the stream from an iterator in Java

Get the stream from Enumeration in Java

No comments:

Post a Comment