HashTable
原本就存在的
官方已经不再建议使用Hashtable?
*被HashMap取代
eg1
public class HashApp{
public static void main(String args[]){
Hashtable hash=new Hashtable(2,(float)0.8);
//创建了一个哈希表的对象hash,初始容量为2,装载因子为0.8
//将字符串对象“Jiangsu”给定一关键字“Nanjing”,并将它加入hash
hash.put("Zhejiang","Hangzhou");
System.out.println("The hashtable hash1 is: "+hash);
System.out.println("The size of this hash table is "+hash.size());
//打印hash的内容和大小
Enumeration enum1=hash.elements();
System.out.print("The element of hash is: ");
while(enum1.hasMoreElements())
System.out.print(enum1.nextElement()+" ");
//依次打印hash中的内容
if(hash.containsKey("Jiangsu"))
System.out.println("The capatial of Jiangsu is "+hash.get("Jiangsu"));
hash.remove("Beijing");
//删除关键字Beijing对应对象
//是否包含
hash.containsKey(2);
}
}
eg2
public class HashTableDemo {
public static void main(String args[]) {
// Create a hash map
Hashtable balance = new Hashtable();
Enumeration names;
String str;
double bal;
balance.put("Zara", new Double(3434.34));
balance.put("Mahnaz", new Double(123.22));
balance.put("Ayan", new Double(1378.00));
balance.put("Daisy", new Double(99.22));
balance.put("Qadir", new Double(-19.08));
// Show all balances in hash table.
names = balance.keys();
while(names.hasMoreElements()) {
str = (String) names.nextElement();
System.out.println(str + ": " +
balance.get(str));
}
System.out.println();
// Deposit 1,000 into Zara's account
bal = ((Double)balance.get("Zara")).doubleValue();
balance.put("Zara", new Double(bal+1000));
System.out.println("Zara's new balance: " +
balance.get("Zara"));
}
}
遍历
for(Iterator it = hash.keySet().iterator(); it.hasNext(); ) {
//从ht中取
String key = (String) it.next();
Object value = hash.get(key);
//放进hm中
hm.put(key, value);
}
HashMap
eg1
HashMap<Integer, Integer> hashMap = new HashMap<>();
hashMap.put(5, 2);
hashMap.put(9, 2);
hashMap.put(8, 1);
hashMap.put(7, 3);
hashMap.put(16, 1);
hashMap.put(10, 2);
hashMap.put(6, 2);
//其实下面两个键值对是没有存的
hashMap.put(5, 2);
hashMap.put(5, 3);
HashMap的API
void clear()
Object clone()
boolean containsKey(Object key)
boolean containsValue(Object value)
Set<Entry<K, V>> entrySet()
V get(Object key)
boolean isEmpty()
Set keySet()
V put(K key, V value)
void putAll(Map<? extends K, ? extends V> map)
V remove(Object key)
int size()
Collection values()
分割线-----------
当在hashmap中put的key在之前已经存过,则不会重复存储,会覆盖之前key对应的value,详情请参照源码
1.containsKey(Object key)方法,返回值为boolean,用于判断当前hashmap中是否包含key对应的key-value
2.containsValue(Object value)方法,返回值为boolean,用于判断当前hashmap中是否包含value对应的key-value
getOrDefault()
- 相较于原来的写法,新的getOrDefault()方法提供一个快捷的方式获取Map中的值
- 他的代码量更少,并且更具有可读性
- 应该推荐getOrDefault()这种写法,他为我们提供了一个更加可靠的代码体验
3.遍历 hashmap的两种方式:
(1)利用haspmap.entrySet().iterator():利用迭代器,从Entry中取出键、取出值,推荐使用这种方式进行遍历,效率较高
Entry 是Map 中表示键值对的关系的一组对象
Iterator<Map.Entry<Integer, Integer>> iterator = hashMap.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<Integer, Integer> entry = iterator.next();
Integer key = entry.getKey();
Integer value = entry.getValue();
System.out.print(key + "--->" + value);
System.out.println();
}
(2)利用hashmap.keySet().iterator():利用键的迭代器,每次取出一个键,再根据键,从hashmap中取出值,这种方式的效率不高,不推荐使用
Iterator<Integer> iterator2 = hashMap.keySet().iterator();
while (iterator2.hasNext()) {
Integer key = iterator2.next();
Integer value = hashMap.get(key);
System.out.print(key + "---" + value);
System.out.println();
}
hashMap分别获取所有key和value
//获取所有key
Set<Integer> keys=map.keySet();
Iterator<Integer> iterator1=keys.iterator();
while (iterator1.hasNext()){
System.out.print(iterator1.next() +", ");
}
//获取所有value
Collection<Integer> values=map.values();
Iterator<Integer> iterator2=values.iterator();
while (iterator2.hasNext()){
System.out.print(iterator2.next()+", ");
}
System.out.println();
System.out.println("------------------------");
//去除value中重复值,相同值仅仅保留一个
Set<Integer> valuesSimple=new HashSet<Integer>();
for(int i:values){
valuesSimple.add(i);
}
Iterator<Integer> iterator3=valuesSimple.iterator();
while (iterator3.hasNext()){
System.out.print(iterator3.next()+", ");
}
HashMap和Hashtable区别
1.hash数组初始化时机不同,Hashtable是在构造函数初始化,而HashMap是在第一次put()初始化hash数组。
2.在HashTable中,hash数组默认大小是11,增加的方式是old2+1。在HashMap中,hash数组默认大小是16,增加的方式是2old而且一定是2的整数.
3.HashMap允许空(null)键值(key),而HashTable不允许。
4.HashMap把Hashtable的contains()方法去掉了,改成了containsvalue()和containsKey()。
5.Hashtable是同步的.,而HashMap是非同步的
所以:Hashtable的方法是线程安全的,而HashMap不支持线程的同步,不是线程安全的。
6.Hashtable使用Enumeration,HashMap使用Iterator。
7.hash值的使用不同,HashTable直接使用对象的hashCode。