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

本文深入探讨了Java中的Map接口实现类,包括Hashtable、HashMap和Properties。详细介绍了它们的特点、使用场景及操作方法,并通过示例代码展示了如何在多线程环境中进行遍历和操作。此外,还特别提到了Properties类的独特之处,即能够将键值对保存到文件中。
摘要由CSDN通过智能技术生成

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值