标签:java
Hashtable()
Constructs a new, empty hashtable with a default initial capacity (11) and load factor
(0.75).
|
Hashtable(int initialCapacity)
Constructs a new, empty hashtable with the specified initial capacity and default load
factor (0.75).
|
Hashtable(int initialCapacity,
float loadFactor)
Constructs a new, empty hashtable with the specified initial capacity and the specified
load factor.
|
Hashtable(Map<?
extends K,?
extends V> t)
Constructs a new hashtable with the same mappings as the given Map.
|
(2)为何需覆盖Object.hashCode方法和Object.equals方法?<span style="font-size:18px;">Hashtable<String, Integer> numbers=new Hashtable<String, Integer>(); numbers.put("one",new Integer(1)); //向Hashtable数据结构中存储一个数据(为键值对) numbers.put("two",new Integer(2)); numbers.put("three",new Integer(3)); Integer n=(Integer)numbers.get("two");//检索"two"关键字对应的数据,其中 numbers.get("two")返回Object类型值对象 if(n!=null) { System.in.println("two = "+n); }</span>
<span style="font-size:18px;">class MyKey
{
private String name;
private int age;
//1.构造函数:赋初值。其中this.name等价于MyKey的对象.name(调用私有变量并给其赋值)
public MyKey(String name,int age)
{
this.name=name;
this.age=age;
}
//2.重写toString方法:实现指定的功能-返回一个值为非空的String对象
public String toString()
{
return new String(name + ","+age);
}
//3.实现equals方法:当名字和年龄都一致时,可以判定为同一对象(Object obj)或者说要检索的键存在Hashtable存储结构中(如果存在返回true)
public boolean equals(Object obj)
{
MyKey mykey1=this;
MyKey mykey2=(MyKey)obj;
if(mykey1.name.equals(mykey2.name) && mykey2.age==mykey1.age) //其中obj为需要检索的对象
return true;
else
return false;
}
//4.哈希函数:确定Key对象与之对应Value的存储位置,相同对象的哈希值一定相同否者就会出现冲突
public int hashCode()
{
return name.hashCode()+age;
}
} </span><span style="font-size:14px;"> </span><span style="font-size:18px;">import java.util.*;
public class HashtableTest
{
public static void main(String[] args)
{
//1.构造一个Hashtable对象,即建立一个Hashtable并向其添加元素(key-value键值对)
Hashtable<MyKey, Integer> numbers=new Hashtable<MyKey, Integer>();
numbers.put(new MyKey("zhangsan",20),new Integer(1));//添加key-value键值对到结构中
numbers.put(new MyKey("lisi",18),new Integer(2));
numbers.put(new MyKey("wangwu",23),new Integer(3));
//2.使用Enumeration获取所有键对象并进行遍历
Enumeration<MyKey> e=numbers.keys(); //numbers.keys()返回hashtable中所有键对象元素
while(e.hasMoreElements()) //遍历每一元素的键对象
{
MyKey key=(MyKey)e.nextElement(); //e.nextElement返回的是一个Object对象
System.out.print(key.toString()+"="); //打印键对象的值,其中toString作用是打印指定的内容
System.out.println(numbers.get(key).toString());//打印Hashtable中键对象对应值对象的值,其中toSting的作用是将值对象的值转换为字符串输出
}
}
}</span>
Properties() Creates
an empty property list with no default values. |
Properties(Properties defaults) Creates
an empty property list with the specified defaults.
注释:defaults-属性列表包含一些对应于任何键值的默认值,这些键值不存在于属性列表中
|
clear, clone, compute, computeIfAbsent, computeIfPresent, contains, containsKey, containsValue, elements, entrySet, equals, forEach, get,getOrDefault, hashCode, isEmpty, keys, keySet, merge, put, putAll, putIfAbsent, rehash, remove, remove, replace, replace, replaceAll, size,toString, valuesfinalize, getClass, notify, notifyAll, wait, wait, wait<span style="font-size:18px;">import java.util.*;
import java.io.*;
class PropertiesTest {
public static void main(String[] args)
{
Properties settings=new Properties();
//1.加载一个文件流,如果该文件不存在则表示打开的次数count=0
try
{
settings.load(new FileInputStream("C:\\count.txt")); //从文件中读取属性列表(为一系列键值对)到Properties结构中
}
catch(Exception e)
{
settings.setProperty("Count", new Integer(0).toString());//向Properties结构中存储键值对key-value
}
//2.次数累加并将其保存到count.txt文件中
int c=Integer.parseInt(settings.getProperty("Count"))+1; //获取键对象对应值对象的内容,并累加1(程序被打开一次)
System.out.println("这是本程序第"+c+"次被使用");
settings.put("Count", new Integer(c).toString()); //再次向Properties结构存储新的键值对
try
{
settings.store(new FileOutputStream("C:\\count.txt"), "This Program is used:");/*将Properties所有属性列表(所有键值对)保存到文件中*/
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}</span>
HashTable的遍历:Hashtable ht=new Hashtable();ht.put("1", "111");ht.put("3","333");ht.put("2", "222");Enumeration en=ht.keys();while(en.hasMoreElements()){
总结二:HashMap与HashSet的区别?Object key=en.nextElement();Object value=ht.get(key);System.out.println("key="+key+",value="+value);}
private static final Object PRESENT = new Object();public boolean add(E e) {return map.put(e, PRESENT)==null;}public boolean remove(Object o) {return map.remove(o)==PRESENT;}
Java笔记十二.常用API-Hashtable类及其与HashMap、HashSet的区别
标签:java
原文地址:http://blog.csdn.net/u012637501/article/details/43153225