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()); } } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter