1
Sun Nov 26 2023 17:33:54 GMT+0000 (Coordinated Universal Time)
Saved by
@jack
import java.util.Hashtable;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
public class BasicHashingTechniques {
public static void main(String[] args) {
// HashTable-based Method (Synchronized)
System.out.println("HashTable-based Method:");
Hashtable<String, Integer> hashTable = new Hashtable<>();
hashTable.put("apple", 5);
hashTable.put("banana", 8);
hashTable.put("orange", 3);
for (Map.Entry<String, Integer> entry : hashTable.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
// HashMap-based Method (Non-synchronized)
System.out.println("\nHashMap-based Method:");
HashMap<String, Integer> hashMap = new HashMap<>();
hashMap.put("apple", 5);
hashMap.put("banana", 8);
hashMap.put("orange", 3);
for (Map.Entry<String, Integer> entry : hashMap.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
// LinkedHashMap-based Method (Maintains order)
System.out.println("\nLinkedHashMap-based Method:");
LinkedHashMap<String, Integer> linkedHashMap = new LinkedHashMap<>();
linkedHashMap.put("apple", 5);
linkedHashMap.put("banana", 8);
linkedHashMap.put("orange", 3);
for (Map.Entry<String, Integer> entry : linkedHashMap.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
}
}
content_copyCOPY
https://www.programiz.com/java-programming/online-compiler/
Comments