Java数据结构映射Map

本文介绍了Java中的Map数据结构,包括Hashtable、HashMap、LinkedHashMap和TreeMap的区别和使用场景。详细讲解了它们的主要方法及特点,如Hashtable的同步安全性、HashMap的高效性、LinkedHashMap的插入顺序保持以及TreeMap的排序功能。此外,还讨论了Properties类在文件存储K-V对中的应用。

Map映射
-数学定义:两个集合之间的元素对应关系
-一个输入对应一个输出
-{key, Value}键值对,K-V对

Java中Map
-Hashtable(同步,慢,数据量小)
-HashMap(不支持同步,快,数据量大)
-Properties(同步,文件形式,数据量小)

Hashtable
-K-V对
-同步,多线程安全,无序的,适合小数据量
-主要方法:clear,contains/containsValue,containsKey,get,put,remove,size
Hashtable<Integer,String> ht = new Hashtable<Integer,String>();
ht.put(1000, “aaaa”);
ht.put(2, “bbbb”);
ht.put(30000, “cccc”);
System.out.println(ht.contains(“aaaa”));//true
System.out.println(ht.containsValue(“aaaa”));//true
System.out.println(ht.containsKey(30000));//true
System.out.println(ht.get(30000));
ht.put(30000, “ffff”);//覆盖cccc
System.out.println(ht.get(30000));//ffff

HashMap
-K-V对,k和v都允许为null
-不同步,多线程不安全
-无序

LinkedHashMap
-基于双向链表的维持插入顺序的HashMap
TreeMap
-基于红黑树的Map,可以根据key的自然排序或者compareTo的方法进行排序输出

Properties
-继承于HashMap
-可以将K-V对保存在文件中
-适用于数据量少的配置文件
-从文件加载的load方法,写入到文件中的store方法
-获取属性getProperty,设置属性setProperty
-load加载原文件中所有的K-V对;store是将所有的K-v对写入到文件中;getProperty是获取某一个Key所对应的Value;getProperty是写入一个K-V对
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;

public class PropertiesTest {
//根据key获取value
public static String GetValueByKey(String filePath,String key) {
// TODO Auto-generated method stub
Properties pps = new Properties();
try {
InputStream in = new BufferedInputStream(new FileInputStream(filePath));
pps.load(in);//所以的K-V都加载了
String value = pps.getProperty(key);
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);
Enumeration en = pps.propertyNames();//得到配置文件的名字
while(en.hasMoreElements()) {
String strKey = (String) en.nextElement();
String strValue = pps.getProperty(strKey);
}
}
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”);
WriteProperties(“Test.properties”,“name”,“123”);
System.out.println(“加载Test”);
GetAllProperties(“Test.properties”);
System.out.println(“从Test加载”);
String value = GetValueByKey(“Test.properties”,“name”);
System.out.println("name is "+value);
}
}
————————————————————————————
写入Test
加载Test
从Test加载
name is 123

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值