Java中HashTable和HashMap之间的区别
以下是Java中HashTable和HashMap类之间的显着差异。
示例
import java.util.HashMap; import java.util.Hashtable; import java.util.Map; public class Tester { public static void main(String args[]) { Map<String, String> map = new HashMap<String, String>(); map.put("1", "One"); map.put("2", "Two"); map.put("3", "Three"); map.put("5", "Five"); map.put("6", "Six"); System.out.println("HashMap: " + map); Map<String, String> map1 = new Hashtable<String, String>(); map1.put("1", "One"); map1.put("2", "Two"); map1.put("3", "Three"); map1.put("5", "Five"); map1.put("6", "Six"); System.out.println("HashTable: " + map1); } }
输出结果
HashMap: {1 = One, 2 = Two, 3 = Three, 5 = Five, 6 = Six} HashTable: {6 = Six, 5 = Five, 3 = Three, 2 = Two, 1 = One}