一:映射Map分类

二:Hashtable(同步,慢,数据量小)

–K-V对,K和V都不允许为null
–同步,多线程安全
–无序的
–适合小数据量
–主要方法:clear, contains/containsValue, containsKey, get,
put,remove, size

(一)基本使用方法

        Hashtable<Integer,String> ht=new Hashtable<Integer,String>();

        //ht.put(1, null);    //编译不报错  运行时报错
//ht.put(null, "das"); ht.put(, "adad");
ht.put(, "adsdad");
ht.put(, "adadge"); //进行判断是否含有数据
System.out.println(ht.contains("adad"));
System.out.println(ht.containsValue("adad")); //contains和containsValue一样
System.out.println(ht.containsKey()); //进行数据获取
System.out.println(ht.get()); //进行更新
ht.put(, ""); //按照键,更新值 //进行移除
System.out.println(ht.containsKey());
ht.remove();
System.out.println(ht.containsKey()); //获取大小
System.out.println(ht.size());
true
true
true
adadge
true
false

(二)遍历方法

    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> ent= iter.next();
key = ent.getKey();
value=ent.getValue();
System.out.println(key+":"+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=ht.get(key);
System.out.println(key+":"+value);
}
long endTime = System.nanoTime();
long duration = endTime - startTime;
System.out.println(duration + "纳秒");
}
    //Enumeration只在Hashtable中用到了,后面废弃不用
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(); //一次性获取所有的key值,
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 + "纳秒");
}

(三)性能测试:数据量越大,差距越小

        for(int i=;i<;i++)
{
ht.put(i, "aaa");
} //进行遍历
traverseByEntry(ht);
traverseByKeySet(ht);
traverseByKeyEnumeration(ht);
=======Entry迭代器遍历=======
13470233纳秒
=======KeySet迭代器遍历=======
8841551纳秒
=======KeyEnumeration迭代器遍历=======
4933705纳秒

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

–K-V对,K和V都允许为nul
–不同步,多线程不安全
Map m = Collections.synchronizedMap(new HashMap(...));
–无序的
–主要方法:clear, containsValue, containsKey, get, put,remove, size

(一)基本使用方法一致

        HashMap<Integer,String> hm =new  HashMap<Integer,String>();
hm.put(, null);
hm.put(null, "abc");
hm.put(, "aaa");
hm.put(, "bbb");
hm.put(, "ccc");
System.out.println(hm.containsValue("aaa"));
System.out.println(hm.containsKey());
System.out.println(hm.get()); hm.put(, "ddd"); //更新覆盖ccc
System.out.println(hm.get()); hm.remove();
System.out.println("size: " + hm.size()); hm.clear();
System.out.println("size: " + hm.size());

(二)遍历方法除了没有Enumeration,其他一致

    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 + "纳秒");
}

traverseByEntry

    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 + "纳秒");
}

traverseByKeySet

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

–继承于Hashtable
–可以将K-V对保存在文件
–适用于数据量少的配置文件
–继承自Hashtable的方法:clear, contains/containsValue, 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();
} public static void main(String [] args) throws IOException{
System.out.println("写入Test.properties================");
WriteProperties("Test.properties","name", ""); 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);
}
}

五:LinkedHashMap和TreeMap

LinkedHashMap
–基于双向链表的维持插入顺序的HashMap
TreeMap
基于红黑树的Map,可以根据key的自然排序或者compareTo方法进行排序输出
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(, null);
hm.put(null, "abc");
hm.put(, "aaa");
hm.put(, "bbb");
hm.put(, "ccc");
System.out.println(hm.containsValue("aaa"));
System.out.println(hm.containsKey());
System.out.println(hm.get()); hm.put(, "ddd"); //更新覆盖ccc
System.out.println(hm.get()); hm.remove();
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=;i<;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 + "纳秒");
}
}

LinkedHashMapTest

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(, null);
//hm.put(null, "abc"); 编译没错,运行报空指针异常
hm.put(, "aaa");
hm.put(, "bbb");
hm.put(, "ccc");
System.out.println(hm.containsValue("aaa"));
System.out.println(hm.containsKey());
System.out.println(hm.get()); hm.put(, "ddd"); //更新覆盖ccc
System.out.println(hm.get()); //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=;i<;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 + "纳秒");
}
}

TreeMapTest

六:总结

–HashMap是最常用的映射结构
–如需要排序,考虑LinkedHashMap和TreeMap
–如需要将K-V存储为文件,可采用Properties类

JAVA核心技术I---JAVA基础知识(映射Map)的更多相关文章

  1. 《Java核心技术·卷Ⅰ:基础知识(原版10》学习笔记 第5章 继承

    <Java核心技术·卷Ⅰ:基础知识(原版10>学习笔记 第5章 继承 目录 <Java核心技术·卷Ⅰ:基础知识(原版10>学习笔记 第5章 继承 5.1 类.超类和子类 5.1 ...

  2. Java核心技术 卷1 基础知识-第一天

    基本数据类型 java是一种强数据类的的语言 共有8种基本数据类型 其中: 整型4种 int(4字节) short(2字节) long(8字节) byte(1字节) java中整型的范围与机器无关 长 ...

  3. [Java面试三]JavaWeb基础知识总结.

    1.web服务器与HTTP协议 Web服务器 l WEB,在英语中web即表示网页的意思,它用于表示Internet主机上供外界访问的资源. l Internet上供外界访问的Web资源分为: • 静 ...

  4. Java中浮点数的基础知识

    偶然查看Math.round的JDK public static int round(float a) { if (a != 0x1.fffffep-2f) // greatest float val ...

  5. JAVA面试题相关基础知识

        1.面向对象的特征有哪些方面 ①抽象: 抽象就是忽略一个主题中与当前目标无关的那些方面,以便更充分地注意与当前目标有关的方面.抽象并不打算了解全部问题,而只是选择其中的一部分,暂时不用部分细节 ...

  6. Java学习之旅基础知识篇:数据类型及流程控制

    经过开篇对Java运行机制及相关环境搭建,本篇主要讨论Java程序开发的基础知识点,我简单的梳理一下.在讲解数据类型之前,我顺便提及一下Java注释:单行注释.多行注释以及文档注释,这里重点强调文档注 ...

  7. java 程序运行的基础知识【Java bytecode】

    聊聊文字,写一篇关于 java 基础知识的博文. JVM 线程栈 到 函数运行 每一个JVM线程来说启动的时候都会创建一个私有的线程栈.一个jvm线程栈用来存储栈帧,jvm线程栈和C语言中的栈很类似, ...

  8. Java学习1——计算机基础知识

    本文包含了一些计算机基础知识:计算机组成:Windows常用快捷键:DOS常用命令:计算机语言发展史.

  9. Java学习之旅基础知识篇:面向对象之封装、继承及多态

    Java是一种面向对象设计的高级语言,支持继承.封装和多态三大基本特征,首先我们从面向对象两大概念:类和对象(也称为实例)谈起.来看看最基本的类定义语法: /*命名规则: *类名(首字母大写,多个单词 ...

  10. Java Script 学习笔记 -- 基础知识

    Java script 概述 java Script 的简介 JavaScript一种直译式脚本语言,是一种动态类型.弱类型.基于原型的语言,内置支持类型.它的解释器被称为JavaScript引擎,为 ...

随机推荐

  1. codechef EBAIT Election Bait【欧几里得算法】

    题目分析: 欧几里得算法来处理一类分数问题,分数问题的形式如下 $\frac{a}{b} < \frac{p}{q} < \frac{c}{d}$ 当a=0时,答案等于$\frac{1}{ ...

  2. python中的replace()方法的使用

    python中的replace()方法的使用 需求是这样的:需要将字符串的某些字符替换成其他字符 str.replace(old,new,max) 第一个参数是要进行更换的旧字符,第二个参数是新的子串 ...

  3. MT【254】值域包含值域

    已知函数$f(x)=x-\dfrac{1}{1+x},g(x)=x^2-2ax+4,$若对任意$x_1\in[0,1]$,存在$x_2\in[1,2]$,使得$f(x_1)=g(x_2)$,则实数$a ...

  4. nowcoder提高集训营第5场

    凉 (比赛链接)[https://www.nowcoder.com/acm/contest/177#question] T1 (题目链接)[https://www.nowcoder.com/acm/c ...

  5. bzoj4671: 异或图

    bzoj4671: 异或图 Description 定义两个结点数相同的图 G1 与图 G2 的异或为一个新的图 G, 其中如果 (u, v) 在 G1 与 G2 中的出现次数之和为 1, 那么边 ( ...

  6. 《Hands-On Machine Learning with Scikit-Learn&TensorFlow》mnist数据集错误及解决方案

    最近在看这本书看到Chapter 3.Classification,是关于mnist数据集的分类,里面有个代码是 from sklearn.datasets import fetch_mldata m ...

  7. 【BZOJ4543】Hotel加强版(长链剖分)

    [BZOJ4543]Hotel加强版(长链剖分) 题面 BZOJ,没有题面 洛谷,只是普通版本 题解 原来我们的\(O(n^2)\)做法是设\(f[i][j]\)表示以\(i\)为根的子树中,距离\( ...

  8. 【CF981D】Bookshelves(贪心,动态规划)

    [CF981D]Bookshelves(贪心,动态规划) 题面 洛谷 Codeforces 给定一个长度为\(n\)的数列,把他们划分成\(k\)段,使得每段的和的结构按位与起来最大. 题解 从高位往 ...

  9. Bug预防体系(上千bug分析后总结的最佳实践)

    Bug预防体系(上千bug分析后总结的最佳实践) 原创 2017-08-16俞美玲 光荣之路 吴老的<selenium webdriver 实战宝典>出版了!  web常见产品问题及预防 ...

  10. 「ZJOI2016」大森林 解题报告

    「ZJOI2016」大森林 神仙题... 很显然线段树搞不了 考虑离线操作 我们只搞一颗树,从位置1一直往后移动,然后维护它的形态试试 显然操作0,1都可以拆成差分的形式,就是加入和删除 因为保证了操 ...