一.Hashtable简介
继承图:
说明:Hashtable是Map接口的实现类,继承于Dictionary。Hashtable中的所有方法都是synchronized的,所以Hashtable是线程安全的。所以并发情况下使用Hashtable。Hashtable的Key和Value值都不可以为null值。
二.常用方法
1.put(K key, V value):以键值对的形式向Hashtable中添加元素
Hashtable<String,String> hashtable=new Hashtable<String, String>();
hashtable.put("1","张三");
2.putAll(Map<? extends K, ? extends V> t):将一个Hashtable全部添加到另一个Hashtable
Hashtable<String,String> hashtable=new Hashtable<String, String>();
hashtable.put("1","张三");
Hashtable<String,String> allhashtable=new Hashtable<String, String>();
allhashtable.putAll(hashtable);
3.size():获取集合长度
Hashtable<String,String> hashtable=new Hashtable<String, String>();
int num=hashtable.size();
4.isEmpty():集合是否为空
Hashtable<String,String> hashtable=new Hashtable<String, String>();
boolean b=hashtable.isEmpty();
5.get(Object key):根据键获取对应的值
Hashtable<String,String> hashtable=new Hashtable<String, String>();
String value=hashtable.get("1");
6.containsKey(Object key):集合中是否包含key
Hashtable<String,String> hashtable=new Hashtable<String, String>();
hashtable.put("1","张三");
hashtable.put("2","李四");
hashtable.put("3","王五");
hashtable.put("4","赵六");
boolean b=hashtable.containsKey("1");
boolean c=hashtable.containsKey("222");
Log.d("TAG","b----:"+b);
Log.d("TAG","c----:"+c);
b----:true
c----:false
7.containsValue(Object value):集合中是否包含value
Hashtable<String,String> hashtable=new Hashtable<String, String>();
hashtable.put("1","张三");
hashtable.put("2","李四");
hashtable.put("3","王五");
hashtable.put("4","赵六");
boolean b=hashtable.containsValue("张三");
boolean c=hashtable.containsValue("222");
Log.d("TAG","b----:"+b);
Log.d("TAG","c----:"+c);
b----:true
c----:false
8.contains(Object value):集合中是否包含value 其实等同于containsValue(Object value)方法
Hashtable<String,String> hashtable=new Hashtable<String, String>();
hashtable.put("1","张三");
hashtable.put("2","李四");
hashtable.put("3","王五");
hashtable.put("4","赵六");
boolean a=hashtable.contains("张三");
boolean b=hashtable.contains("3");
boolean c=hashtable.contains("222");
Log.d("TAG","a----:"+a);
Log.d("TAG","b----:"+b);
Log.d("TAG","c----:"+c);
a----:true
b----:false
c----:false
9.remove(Object key):删除集合中key对应的value
Hashtable<String,String> hashtable=new Hashtable<String, String>();
hashtable.put("1","张三");
hashtable.put("2","李四");
hashtable.put("3","王五");
hashtable.put("4","赵六");
int num1=hashtable.size();
Log.d("TAG","num1----:"+num1);
hashtable.remove("2");
int num2=hashtable.size();
Log.d("TAG","num2----:"+num2);
num1----:4
num2----:3
10.remove(Object key, Object value):删除集合中 key-value的元素(api = Build.VERSION_CODES.N api=24)
Hashtable<String,String> hashtable=new Hashtable<String, String>();
hashtable.put("1","张三");
hashtable.put("2","李四");
hashtable.put("3","王五");
hashtable.put("4","赵六");
int num1=hashtable.size();
Log.d("TAG","num1----:"+num1);
hashtable.remove("2","李四");
int num2=hashtable.size();
Log.d("TAG","num2----:"+num2);
num1----:4
num2----:3
如果:
hashtable.remove("2","李四22222");
结果:
num1----:4
num2----:4
即没有删除 因为不存在key=2 value=李四22222
11.clear():清空集合元素
Hashtable<String,String> hashtable=new Hashtable<String, String>();
hashtable.put("1","张三");
hashtable.put("2","李四");
hashtable.put("3","王五");
hashtable.put("4","赵六");
int num1=hashtable.size();
Log.d("TAG","num1----:"+num1);
hashtable.clear();
int num2=hashtable.size();
Log.d("TAG","num2----:"+num2);
num1----:4
num2----:0
12.keySet():遍历集合 获取key 然后通过获取的key获得相应的value
package com.example.test;
import android.os.Bundle;
import android.util.Log;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Collection;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initArrayList();
}
public void initArrayList() {
Hashtable<String, String> hashtable = new Hashtable<String, String>();
hashtable.put("1", "张三");
hashtable.put("2", "李四");
hashtable.put("3", "王五");
hashtable.put("4", "赵六");
Set<String> set = hashtable.keySet();
Iterator<String> iterator = set.iterator();
while (iterator.hasNext()) {
String key = iterator.next();
String value = hashtable.get(key);
Log.i("TAG", "key----:" + key + "----value----:" + value);
}
}
}
I/TAG: key----:4----value----:赵六
I/TAG: key----:3----value----:王五
I/TAG: key----:2----value----:李四
I/TAG: key----:1----value----:张三
13.values():遍历集合 获取value
package com.example.test;
import android.os.Bundle;
import android.util.Log;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Collection;
import java.util.Hashtable;
import java.util.Iterator;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initArrayList();
}
public void initArrayList() {
Hashtable<String, String> hashtable = new Hashtable<String, String>();
hashtable.put("1", "张三");
hashtable.put("2", "李四");
hashtable.put("3", "王五");
hashtable.put("4", "赵六");
Collection<String> collection = hashtable.values();
Iterator<String> iterator = collection.iterator();
while (iterator.hasNext()) {
String value = iterator.next();
Log.i("TAG", "value----:" + value);
}
}
}
I/TAG: value----:赵六
I/TAG: value----:王五
I/TAG: value----:李四
I/TAG: value----:张三
14.entrySet():遍历集合 直接获取key value
package com.example.test;
import android.os.Bundle;
import android.util.Log;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Collection;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initArrayList();
}
public void initArrayList() {
Hashtable<String, String> hashtable = new Hashtable<String, String>();
hashtable.put("1", "张三");
hashtable.put("2", "李四");
hashtable.put("3", "王五");
hashtable.put("4", "赵六");
Set<Map.Entry<String, String>> entrySet = hashtable.entrySet();
Iterator<Map.Entry<String, String>> iterator = entrySet.iterator();
while (iterator.hasNext()) {
Map.Entry<String, String> map = iterator.next();
String key = map.getKey();
String value = map.getValue();
Log.i("TAG", "key----" + key + "----value----:" + value);
}
}
}
I/TAG: key----4----value----:赵六
I/TAG: key----3----value----:王五
I/TAG: key----2----value----:李四
I/TAG: key----1----value----:张三
由上可知Hashtable遍历的结果和插入顺序不一致,即Hashtable不保证元素的顺序。
15.replace(K key, V oldValue, V newValue):替换集合指定元素(api = Build.VERSION_CODES.N)
package com.example.test;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Collection;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initArrayList();
}
public void initArrayList() {
Hashtable<String, String> hashtable = new Hashtable<String, String>();
hashtable.put("1", "张三");
hashtable.put("2", "李四");
hashtable.put("3", "王五");
hashtable.put("4", "赵六");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
hashtable.replace("3", "王五", "wangwu");
}
Set<Map.Entry<String, String>> entrySet = hashtable.entrySet();
Iterator<Map.Entry<String, String>> iterator = entrySet.iterator();
while (iterator.hasNext()) {
Map.Entry<String, String> map = iterator.next();
String key = map.getKey();
String value = map.getValue();
Log.i("TAG", "key----" + key + "----value----:" + value);
}
}
}
I/TAG: key----4----value----:赵六
I/TAG: key----3----value----:wangwu
I/TAG: key----2----value----:李四
I/TAG: key----1----value----:张三
16.replace(K key, V value):替换集合指定元素(api = Build.VERSION_CODES.N)
package com.example.test;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Collection;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initArrayList();
}
public void initArrayList() {
Hashtable<String, String> hashtable = new Hashtable<String, String>();
hashtable.put("1", "张三");
hashtable.put("2", "李四");
hashtable.put("3", "王五");
hashtable.put("4", "赵六");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
hashtable.replace("4", "zhaoliu");
}
Set<Map.Entry<String, String>> entrySet = hashtable.entrySet();
Iterator<Map.Entry<String, String>> iterator = entrySet.iterator();
while (iterator.hasNext()) {
Map.Entry<String, String> map = iterator.next();
String key = map.getKey();
String value = map.getValue();
Log.i("TAG", "key----" + key + "----value----:" + value);
}
}
}
I/TAG: key----4----value----:zhaoliu
I/TAG: key----3----value----:王五
I/TAG: key----2----value----:李四
I/TAG: key----1----value----:张三
如果
hashtable.replace("33333","王五","wangwu");
hashtable.replace("44444","zhaoliu");
即key是不存在的
结果:
I/TAG: key----4----value----:赵六
I/TAG: key----3----value----:王五
I/TAG: key----2----value----:李四
I/TAG: key----1----value----:张三
即此时没有修改。
三.线程安全
Hashtable是怎样做到线程安全的呢?源码可知,Hashtable是通过方法加同步的方式完成的线程安全。比如
1.获取集合大小
/**
* Returns the number of keys in this hashtable.
*
* @return the number of keys in this hashtable.
*/
public synchronized int size() {
return count;
}
2.判断集合是否为空
/**
* Tests if this hashtable maps no keys to values.
*
* @return <code>true</code> if this hashtable maps no keys to values;
* <code>false</code> otherwise.
*/
public synchronized boolean isEmpty() {
return count == 0;
}
3.获取Key的集合
/**
* Returns an enumeration of the keys in this hashtable.
*
* @return an enumeration of the keys in this hashtable.
* @see Enumeration
* @see #elements()
* @see #keySet()
* @see Map
*/
public synchronized Enumeration<K> keys() {
return this.<K>getEnumeration(KEYS);
}
等等...
也就是Hashtable是暴力的通过每个方法中都加入同步完成的线程安全。这样是非常浪费资源的。