Map映射数学定义:两个集合之间的元素对应关系

一个输入对应到一个输出

{1,张三},{2.李四},{Key,Value},键值对,K-V对

Java中MapHashtable(同步,慢,数据量小)

HashMap(不支持同步,快,数据量大)

Properties(同步,文件形式,数据量小)

HashtableK-V对,K和V都不允许为null

同步,多线程安全

无序的

适合小数据量

主要方法:

clear;

contains/containsValue;

containsKey;

get;

put;

remove;

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 ht =new Hashtable();

//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"));

System.out.println(ht.containsValue("aaa"));

System.out.println(ht.containsKey(30000));

System.out.println(ht.get(30000));

ht.put(30000, "ddd"); //更新覆盖cccSystem.out.println(ht.get(30000));

ht.remove(2);

System.out.println("size: " + ht.size());

ht.clear();

System.out.println("size: " + ht.size());

//测试遍历方法Hashtable ht2 =new Hashtable();

for(int i=0;i<100000;i++)

{

ht2.put(i, "aaa");

}

traverseByEntry(ht2);

traverseByKeySet(ht2);

traverseByKeyEnumeration(ht2);

}

public static void traverseByEntry(Hashtable ht)

{

long startTime = System.nanoTime();

System.out.println("============Entry迭代器遍历==============");

Integer key;

String value;

Iterator> iter = ht.entrySet().iterator();

while(iter.hasNext()) {

Map.Entry 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 ht)

{

long startTime = System.nanoTime();

System.out.println("============KeySet迭代器遍历==============");

Integer key;

String value;

Iterator 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 ht)

{

long startTime = System.nanoTime();

System.out.println("============KeyEnumeration迭代器遍历==============");

Integer key;

String value;

Enumeration 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迭代器遍历==============

68430800纳秒

============KeySet迭代器遍历==============

57468700纳秒

============KeyEnumeration迭代器遍历==============

11452400纳秒

HashMapK-V对,K和V都允许为null

不同步,多线程不安全,变成同步:

Map m =Collections.synchronizedMap(new HashMap(···));

无序的

主要方法;

clear;

containsValue;

containsKey;

get;

put;

remove;

size;

例程:

import java.util.Enumeration;

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 hm =new HashMap();

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"); //更新覆盖cccSystem.out.println(hm.get(30000));

hm.remove(2);

System.out.println("size: " + hm.size());

hm.clear();

System.out.println("size: " + hm.size());

HashMap hm2 =new HashMap();

for(int i=0;i<100000;i++)

{

hm2.put(i, "aaa");

}

traverseByEntry(hm2);

traverseByKeySet(hm2);

}

public static void traverseByEntry(HashMap ht)

{

long startTime = System.nanoTime();

System.out.println("============Entry迭代器遍历==============");

Integer key;

String value;

Iterator> iter = ht.entrySet().iterator();

while(iter.hasNext()) {

Map.Entry 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 ht)

{

long startTime = System.nanoTime();

System.out.println("============KeySet迭代器遍历==============");

Integer key;

String value;

Iterator 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迭代器遍历==============

34918300纳秒

============KeySet迭代器遍历==============

19260000纳秒

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 hm =new LinkedHashMap();

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"); //更新覆盖cccSystem.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> iter = hm.entrySet().iterator();

while(iter.hasNext()) {

Map.Entry entry = iter.next();

// ��ȡkey key = entry.getKey();

// ��ȡvalue value = entry.getValue();

System.out.println("Key:" + key + ", Value:" + value);

}

System.out.println("遍历结束==================");

LinkedHashMap hm2 =new LinkedHashMap();

for(int i=0;i<100000;i++)

{

hm2.put(i, "aaa");

}

traverseByEntry(hm2);

traverseByKeySet(hm2);

}

public static void traverseByEntry(LinkedHashMap ht)

{

long startTime = System.nanoTime();

System.out.println("============Entry迭代器遍历==============");

Integer key;

String value;

Iterator> iter = ht.entrySet().iterator();

while(iter.hasNext()) {

Map.Entry 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 ht)

{

long startTime = System.nanoTime();

System.out.println("============KeySet迭代器遍历==============");

Integer key;

String value;

Iterator 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迭代器遍历==============

41830000纳秒

============KeySet迭代器遍历==============

12364800纳秒

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 hm =new TreeMap();

hm.put(1, null); //value可以为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"); //更新覆盖cccSystem.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> iter = hm.entrySet().iterator();

while(iter.hasNext()) {

Map.Entry entry = iter.next();

// 获取key key = entry.getKey();

//获取value value = entry.getValue();

System.out.println("Key:" + key + ", Value:" + value);

}

System.out.println("遍历结束==================");

TreeMap hm2 =new TreeMap();

for(int i=0;i<100000;i++)

{

hm2.put(i, "aaa");

}

traverseByEntry(hm2);

traverseByKeySet(hm2);

}

public static void traverseByEntry(TreeMap ht)

{

long startTime = System.nanoTime();

System.out.println("============Entry迭代器遍历==============");

Integer key;

String value;

Iterator> iter = ht.entrySet().iterator();

while(iter.hasNext()) {

Map.Entry 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 ht)

{

long startTime = System.nanoTime();

System.out.println("============KeySet迭代器遍历==============");

Integer key;

String value;

Iterator 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:2, Value:bbb

Key:1000, Value:aaa

Key:30000, Value:ddd

遍历结束==================

============Entry迭代器遍历==============

38940500纳秒

============KeySet迭代器遍历==============

23699800纳秒

Properties继承于Hashtable

可以将K-V保存在文件中(唯一一个)

适用于数据量少的配置文件

继承自Hashtable的方法

contains/containsValue;

clear;

containsKey;

get;

put;

remove;

size;

从文件加载的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();

}

运行结果:

写入Test.properties================

加载Test.properties================

从Test.properties加载================

name is 12345

java 向文件写数据结构_Java Note 数据结构(5)映射相关推荐

  1. java 往文件写值,java文件读写

    Java 对文件进行读写操作的例子很多,让初学者感到十分困惑,我觉得有必要将各种方法进行 一次分析,归类,理清不同方法之间的异同点. 一.在 JDK 1.0 中,通常是用 InputStream &a ...

  2. java遍历文件和归类_java读取文件的两种方法:java.io和java.lang.ClassLoader

    java读取文件的两种方法:java.io和java.lang.ClassLoader 什么时候使用java.io,什么时候使用java.lang.ClassLoader呢? (注:要是之前读xml文 ...

  3. java判断文件写完_Java_判断文件是否写入完成

    /** * 等待文件(非目录)读写完毕,费时的操作,不要放在主线程 * * @param file 文件 */ private void waitForWirtenCompleted(File fil ...

  4. Java读文件写文件操作

    这里,Java的读文件和写文件都是基于字符流的,主要用到下面的几个类: 1.FileReader----读取字符流 2.FileWriter----写入字符流 3.BufferedReader---- ...

  5. java读取文件到字符串_Java读取文件到字符串

    java读取文件到字符串 Sometimes while working with files, we need to read the file to String in Java. Today w ...

  6. java 上文件传示例_Java解压缩文件示例

    java 上文件传示例 Welcome to Java Unzip File Example. In the last post, we learned how to zip file and dir ...

  7. java 读取文件文本内容_Java读取文本文件

    java 读取文件文本内容 There are many ways to read a text file in java. Let's look at java read text file dif ...

  8. java通用文件换行符_java通用文件换行符

    java通用文件换行符 [2021-02-07 00:14:46]  简介: java中的换行符是[\n]和[\r].二者的区别是:[\r]表示回车,[\n]表示新行,但两者都可以实现换行.具体实现方 ...

  9. java判断文件是否图片_java怎么判断文件是否是图片

    java判断文件是否是图片的方法: 1.通过判断文件后缀名String extension = ""; int i = fileName.lastIndexOf('.'); if ...

  10. java 数据结构_Java版-数据结构-队列(数组队列)

    前言 看过笔者前两篇介绍的 Java版数据结构 数组和 栈的盆友,都给予了笔者一致的好评,在这里笔者感谢大家的认可!!! 由于本章介绍的数据结构是 队列,在队列的实现上会基于前面写的 动态数组来实现, ...

最新文章

  1. 银联配置 linux 路径,深圳银联POS支付系统安装手册(LinuxMySQL).doc
  2. iOS---GCD的三种常见用法
  3. 开源大数据查询分析引擎
  4. mysql日期存到oracle_mysql与oracle的日期/时间函数小结
  5. javascript极简时间扩展类
  6. Android6.0的Looper源码分析(1)
  7. java kmp算法_KMP算法java版实现
  8. matlab算概率,用matlab计算概率,再次吐槽某些吧友国战比赛七框选将的建议
  9. Go Python 7: 2-Layer Neural Network
  10. Mongodb查询分析器解析
  11. 104. 二叉树的最大深度 golang
  12. ae toolbarcontrol运行时没有_想办法让AE跑起来
  13. linux 内核任务调度,Linux任务调度
  14. 01 Python基础
  15. WINDOWS XP优化批处理
  16. 杜比专为旧版本Android,杜比音效app(dolby audio) v2.1.0 安卓版
  17. 新概念英语第二册61-96课(转)
  18. 智能语音:好玩的语音控制是怎么实现的?学习笔记01
  19. mysql biginteger java_java.math.BigInteger cannot be cast to java.lang.Integer以及mysql升级的问题...
  20. 基于JavaSpringboot+vue国风汉服文化交流宣传系统

热门文章

  1. OSPF在企业网络中的应用
  2. 行军导航过程中导向箭头
  3. 用python股票_十分钟学会用Python交易股票
  4. flutter html 加载_实操 | 在 Flutter 中创建通信桥
  5. 高通的快充协议_高通发布QC5.0快充技术最高100W+功率!手机厂商私有协议更好...
  6. python入门教程基础语法_python入门教程13-02 (python语法入门之库相关操作)
  7. 属性篇(4)—If you love css …
  8. 自定义docker nginx镜像无容器日志输出
  9. js中去除字符串中所有的html标签
  10. D3D 扎带 小样本