Java数据结构:映射Map
代码、知识点来自中国大学MOOC-陈育良《Java核心技术》
整理如下
Map映射
– 数学定义:两个集合之间的元素对应关系。
– 一个输入对应到一个输出
– {1,张三}, {2,李四}, {Key, Value},键值对, K-V对(1对应张三、2对应李四)
Java中Map
Hashtable | HashMap | LinkedHashMap | TreeMap | Properties |
---|---|---|---|---|
支持同步,性能慢,适合数据量小的 | 不支持同步,性能快,适合数据量大的 | 基于双向链表的维持插入顺序的HashMap | 基于红黑树的Map,可以根据key的自然排序或者compareTo方法进行排序输出 | 支持同步,文件形式存储,适合数据量小的 |
Hashtable
– K-V对, K和V都不允许为null
– 同步,多线程安全
– 无序的(存储、输出时无序)
– 适合小数据量
– 主要方法:
方法 | 内容 |
---|---|
clear | 清空数据 |
contains/containsValue | 是否包含某个值 Value |
containsKey | 是否包含某个值Key |
get | 根据Key获取相应的值 |
put | 增加新的K-V对/更新K-V对 |
remove | 删除某个K-V对 |
size | 返回数据大小 |
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
public class HashtableTest {
public static void main(String[] args) {
Hashtable<Integer,String> ht =new Hashtable<Integer,String>();
//ht.put(1, null); 编译不报错 运行报错
//ht.put(null,1); 编译报错
ht.put(1000, "aaa");
ht.put(2, "bbb");
ht.put(30000, "ccc");
System.out.println(ht.contains("aaa"));//true
System.out.println(ht.containsValue("aaa"));//true
System.out.println(ht.containsKey(30000));//true
System.out.println(ht.get(30000));//ccc
ht.put(30000, "ddd"); //更新覆盖ccc
System.out.println(ht.get(30000));//ddd
ht.remove(2);//删除Key为2的键值对
System.out.println("size: " + ht.size());//2
ht.clear();
System.out.println("size: " + ht.size());//0
//定义一个Hashtable,名为ht2,往其中存入10000个键值对
Hashtable<Integer,String> ht2 =new Hashtable<Integer,String>();
for(int i=0;i<100000;i++)
{
ht2.put(i, "aaa");
}
//调用不同的遍历方法,检测遍历10000个键值对的时间
traverseByEntry(ht2);
traverseByKeySet(ht2);
traverseByKeyEnumeration(ht2);
}
public static void traverseByEntry(Hashtable<Integer,String> ht)
{
long startTime = System.nanoTime();
System.out.println("============Entry迭代器遍历==============");
Integer key;
String value;
Iterator<Entry<Integer, String>> iter = ht.entrySet().iterator();
while(iter.hasNext()) {
Map.Entry<Integer, String> entry = iter.next();
// 获取key
key = entry.getKey();
// 获取value
value = entry.getValue();
//System.out.println("Key:" + key + ", Value:" + value);
}
long endTime = System.nanoTime();
long duration = endTime - startTime;
System.out.println(duration + "纳秒");
}
public static void traverseByKeySet(Hashtable<Integer,String> ht)
{
long startTime = System.nanoTime();
System.out.println("============KeySet迭代器遍历==============");
Integer key;
String value;
Iterator<Integer> iter = ht.keySet().iterator();
while(iter.hasNext()) {
key = iter.next();
// 获取value
value = ht.get(key);
//System.out.println("Key:" + key + ", Value:" + value);
}
long endTime = System.nanoTime();
long duration = endTime - startTime;
System.out.println(duration + "纳秒");
}
public static void traverseByKeyEnumeration(Hashtable<Integer,String> ht)
{
long startTime = System.nanoTime();
System.out.println("============KeyEnumeration迭代器遍历==============");
Integer key;
String value;
Enumeration<Integer> keys = ht.keys();
while(keys.hasMoreElements()) {
key = keys.nextElement();
// 获取value
value = ht.get(key);
//System.out.println("Key:" + key + ", Value:" + value);
}
long endTime = System.nanoTime();
long duration = endTime - startTime;
System.out.println(duration + "纳秒");
}
}
输出:
true
true
true
ccc
ddd
size: 2
size: 0
============Entry迭代器遍历==============
35686317纳秒//直接将键值对遍历
============KeySet迭代器遍历==============
31397530纳秒//通过对Key进行遍历,然后再用get获取Value
============KeyEnumeration迭代器遍历==============
22381374纳秒//通过对Key进行遍历,然后再用get获取Value,(老版,只能读,不能进行删除或其他操作)
HashMap
– K-V对, K和V都允许为null
– 不同步,多线程不安全
– 无序的(存储、输出无序)
– 主要方法:与Hashtable相同
//与Hashtable的代码一般无二,可以直接看输出结果节省时间
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
public class HashMapTest {
public static void main(String[] args) {
HashMap<Integer,String> hm =new HashMap<Integer,String>();
hm.put(1, null);
hm.put(null, "abc");
hm.put(1000, "aaa");
hm.put(2, "bbb");
hm.put(30000, "ccc");
System.out.println(hm.containsValue("aaa"));
System.out.println(hm.containsKey(30000));
System.out.println(hm.get(30000));
hm.put(30000, "ddd"); //更新覆盖ccc
System.out.println(hm.get(30000));
hm.remove(2);
System.out.println("size: " + hm.size());
hm.clear();
System.out.println("size: " + hm.size());
HashMap<Integer,String> hm2 =new HashMap<Integer,String>();
for(int i=0;i<100000;i++)
{
hm2.put(i, "aaa");
}
traverseByEntry(hm2);
traverseByKeySet(hm2);
}
public static void traverseByEntry(HashMap<Integer,String> ht)
{
long startTime = System.nanoTime();
System.out.println("============Entry迭代器遍历==============");
Integer key;
String value;
Iterator<Entry<Integer, String>> iter = ht.entrySet().iterator();
while(iter.hasNext()) {
Map.Entry<Integer, String> entry = iter.next();
// 获取key
key = entry.getKey();
// 获取value
value = entry.getValue();
//System.out.println("Key:" + key + ", Value:" + value);
}
long endTime = System.nanoTime();
long duration = endTime - startTime;
System.out.println(duration + "纳秒");
}
public static void traverseByKeySet(HashMap<Integer,String> ht)
{
long startTime = System.nanoTime();
System.out.println("============KeySet迭代器遍历==============");
Integer key;
String value;
Iterator<Integer> iter = ht.keySet().iterator();
while(iter.hasNext()) {
key = iter.next();
// 获取value
value = ht.get(key);
//System.out.println("Key:" + key + ", Value:" + value);
}
long endTime = System.nanoTime();
long duration = endTime - startTime;
System.out.println(duration + "纳秒");
}
}
输出:
true
true
ccc
ddd
size: 4
size: 0
============Entry迭代器遍历==============
42959258纳秒
============KeySet迭代器遍历==============
21906456纳秒
LinkedHashMap
– 基于双向链表的维持插入顺序的HashMap
import java.util.LinkedHashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
public class LinkedHashMapTest {
public static void main(String[] args) {
LinkedHashMap<Integer,String> hm =new LinkedHashMap<Integer,String>();
hm.put(1, null);
hm.put(null, "abc");
hm.put(1000, "aaa");
hm.put(2, "bbb");
hm.put(30000, "ccc");
System.out.println(hm.containsValue("aaa"));
System.out.println(hm.containsKey(30000));
System.out.println(hm.get(30000));
hm.put(30000, "ddd"); //更新覆盖ccc
System.out.println(hm.get(30000));
hm.remove(2);
System.out.println("size: " + hm.size());
//hm.clear();
//System.out.println("size: " + hm.size());
System.out.println("遍历开始==================");
Integer key;
String value;
Iterator<Entry<Integer, String>> iter = hm.entrySet().iterator();
while(iter.hasNext()) {
Map.Entry<Integer, String> entry = iter.next();
// 获取key
key = entry.getKey();
// 获取value
value = entry.getValue();
System.out.println("Key:" + key + ", Value:" + value);
}
System.out.println("遍历结束==================");
LinkedHashMap<Integer,String> hm2 =new LinkedHashMap<Integer,String>();
for(int i=0;i<100000;i++)
{
hm2.put(i, "aaa");
}
traverseByEntry(hm2);
traverseByKeySet(hm2);
}
public static void traverseByEntry(LinkedHashMap<Integer,String> ht)
{
long startTime = System.nanoTime();
System.out.println("============Entry迭代器遍历==============");
Integer key;
String value;
Iterator<Entry<Integer, String>> iter = ht.entrySet().iterator();
while(iter.hasNext()) {
Map.Entry<Integer, String> entry = iter.next();
// 获取key
key = entry.getKey();
// 获取value
value = entry.getValue();
//System.out.println("Key:" + key + ", Value:" + value);
}
long endTime = System.nanoTime();
long duration = endTime - startTime;
System.out.println(duration + "纳秒");
}
public static void traverseByKeySet(LinkedHashMap<Integer,String> ht)
{
long startTime = System.nanoTime();
System.out.println("============KeySet迭代器遍历==============");
Integer key;
String value;
Iterator<Integer> iter = ht.keySet().iterator();
while(iter.hasNext()) {
key = iter.next();
// 获取value
value = ht.get(key);
//System.out.println("Key:" + key + ", Value:" + value);
}
long endTime = System.nanoTime();
long duration = endTime - startTime;
System.out.println(duration + "纳秒");
}
}
输出:
true
true
ccc
ddd
size: 4
遍历开始==================
Key:1, Value:null//按插入顺序输出
Key:null, Value:abc
Key:1000, Value:aaa
Key:30000, Value:ddd
遍历结束==================
============Entry迭代器遍历==============
26373497纳秒
============KeySet迭代器遍历==============
24825636纳秒
TreeMap
– 基于红黑树的Map,可以根据key的自然排序或者compareTo方法进行排序输出
import java.util.TreeMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
public class TreeMapTest {
public static void main(String[] args) {
TreeMap<Integer,String> hm =new TreeMap<Integer,String>();
hm.put(1, null);
//hm.put(null, "abc"); 编译没错,运行报空指针异常
hm.put(1000, "aaa");
hm.put(2, "bbb");
hm.put(30000, "ccc");
System.out.println(hm.containsValue("aaa"));
System.out.println(hm.containsKey(30000));
System.out.println(hm.get(30000));
hm.put(30000, "ddd"); //更新覆盖ccc
System.out.println(hm.get(30000));
//hm.remove(2);
System.out.println("size: " + hm.size());
//hm.clear();
//System.out.println("size: " + hm.size());
System.out.println("遍历开始==================");
Integer key;
String value;
Iterator<Entry<Integer, String>> iter = hm.entrySet().iterator();
while(iter.hasNext()) {
Map.Entry<Integer, String> entry = iter.next();
// 获取key
key = entry.getKey();
// 获取value
value = entry.getValue();
System.out.println("Key:" + key + ", Value:" + value);
}
System.out.println("遍历结束==================");
TreeMap<Integer,String> hm2 =new TreeMap<Integer,String>();
for(int i=0;i<100000;i++)
{
hm2.put(i, "aaa");
}
traverseByEntry(hm2);
traverseByKeySet(hm2);
}
public static void traverseByEntry(TreeMap<Integer,String> ht)
{
long startTime = System.nanoTime();
System.out.println("============Entry迭代器遍历==============");
Integer key;
String value;
Iterator<Entry<Integer, String>> iter = ht.entrySet().iterator();
while(iter.hasNext()) {
Map.Entry<Integer, String> entry = iter.next();
// 获取key
key = entry.getKey();
// 获取value
value = entry.getValue();
//System.out.println("Key:" + key + ", Value:" + value);
}
long endTime = System.nanoTime();
long duration = endTime - startTime;
System.out.println(duration + "纳秒");
}
public static void traverseByKeySet(TreeMap<Integer,String> ht)
{
long startTime = System.nanoTime();
System.out.println("============KeySet迭代器遍历==============");
Integer key;
String value;
Iterator<Integer> iter = ht.keySet().iterator();
while(iter.hasNext()) {
key = iter.next();
// 获取value
value = ht.get(key);
//System.out.println("Key:" + key + ", Value:" + value);
}
long endTime = System.nanoTime();
long duration = endTime - startTime;
System.out.println(duration + "纳秒");
}
}
输出:
true
true
ccc
ddd
size: 4
遍历开始==================
Key:1, Value:null//根据Key排序后输出
Key:2, Value:bbb
Key:1000, Value:aaa
Key:30000, Value:ddd
遍历结束==================
============Entry迭代器遍历==============
34523604纳秒
============KeySet迭代器遍历==============
31939132纳秒
Properties
– 继承于Hashtable
– 可以将K-V对保存在文件中
– 适用于数据量少的配置文件
– 继承自Hashtable的方法: clear, contains/containsValue, containsKey,get, put,remove, size
在继承Hashtable方法的基础上新增4个方法
load | store | getProperty | setProperty |
---|---|---|---|
从文件加载 | 写入到文件中 | 获取属性 | 设置属性 |
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.Properties;
//关于Properties类常用的操作
public class PropertiesTest {
//根据Key读取Value
public static String GetValueByKey(String filePath, String key) {
Properties pps = new Properties();
try {
InputStream in = new BufferedInputStream (new FileInputStream(filePath));
pps.load(in); //所有的K-V对都加载了
String value = pps.getProperty(key);
//System.out.println(key + " = " + value);
return value;
}catch (IOException e) {
e.printStackTrace();
return null;
}
}
//读取Properties的全部信息
public static void GetAllProperties(String filePath) throws IOException {
Properties pps = new Properties();
InputStream in = new BufferedInputStream(new FileInputStream(filePath));
pps.load(in); //所有的K-V对都加载了
Enumeration en = pps.propertyNames(); //得到配置文件的名字
while(en.hasMoreElements()) {
String strKey = (String) en.nextElement();
String strValue = pps.getProperty(strKey);
//System.out.println(strKey + "=" + strValue);
}
}
//写入Properties信息
public static void WriteProperties (String filePath, String pKey, String pValue) throws IOException {
File file = new File(filePath);
if(!file.exists())
{
file.createNewFile();
}
Properties pps = new Properties();
InputStream in = new FileInputStream(filePath);
//从输入流中读取属性列表(键和元素对)
pps.load(in);
//调用 Hashtable 的方法 put。使用 getProperty 方法提供并行性。
//强制要求为属性的键和值使用字符串。返回值是 Hashtable 调用 put 的结果。
OutputStream out = new FileOutputStream(filePath);
pps.setProperty(pKey, pValue);
//以适合使用 load 方法加载到 Properties 表中的格式,
//将此 Properties 表中的属性列表(键和元素对)写入输出流
pps.store(out, "Update " + pKey + " name");
out.close();
}
public static void main(String [] args) throws IOException{
System.out.println("写入Test.properties================");
WriteProperties("Test.properties","name", "12345");
System.out.println("加载Test.properties================");
GetAllProperties("Test.properties");
System.out.println("从Test.properties加载================");
String value = GetValueByKey("Test.properties", "name");
System.out.println("name is " + value);
}
}
输出:
写入Test.properties================//对应目录下也出现Test.properties
加载Test.properties================
从Test.properties加载================
name is 12345
总结
– HashMap是最常用的映射结构
– 如需要排序,考虑LinkedHashMap和TreeMap
– 如需要将K-V存储为文件,可采用Properties类