一、综述
Map用来保存具有映射关系的元素,以键值对(key-value)的形式出现。key-value存在意义对应的关系,通过key可以找到唯一的value,所以Map集合中的key不可以重复,key类似于集合中的Set,value值可以重复。Java中Map集合可以类似Set去看,HashMap、LinkedHashMap、TreeMap。Java源码就是在先实现Map 的基础之上,通过让value值为null来实现的Set集合。
二、HashMap常用方法
修饰符与类型 | 方法与描述 |
---|---|
void | clear()
Removes all of the mappings from this map.
|
Object | clone()
Returns a shallow copy of this
HashMap instance: the keys and values themselves are not cloned.
|
boolean | containsKey(Object key)
Returns
true if this map contains a mapping for the specified key.
|
boolean | containsValue(Object value)
Returns
true if this map maps one or more keys to the specified value.
|
Set<Map.Entry<K,V>> | entrySet()
Returns a
Set view of the mappings contained in this map.
|
V | get(Object key)
Returns the value to which the specified key is mapped, or
null if this map contains no mapping for the key.
|
boolean | isEmpty()
Returns
true if this map contains no key-value mappings.
|
Set<K> | keySet()
Returns a
Set view of the keys contained in this map.
|
V | put(K key,V value)
Associates the specified value with the specified key in this map.
|
void | putAll(Map<? extendsK,? extends V> m)
Copies all of the mappings from the specified map to this map.
|
V | remove(Object key)
Removes the mapping for the specified key from this map if present.
|
int | size()
Returns the number of key-value mappings in this map.
|
Collection<V> | values()
Returns a
Collection view of the values contained in this map
|
package ClectionsTest;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
public class MapLearning {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
HashMap<Integer,String> map = new HashMap<Integer,String>();
map.put(1, "a");
map.put(2, "b");
map.put(3, "c");
map.put(4, "d");
System.out.println("map集合:"+map); //map集合:{1=a, 2=b, 3=c, 4=d}
System.out.println("value值:"+map.get(2)); //value值:b
System.out.println("key是否包含2:"+map.containsKey(2));//key是否包含2:true
System.out.println("value是否包含d:"+map.containsValue("d"));//value是否包含d:true
/***
* 输出
* 1:a
2:b
3:c
4:d
*/
Set<Entry<Integer, String>> enSet = map.entrySet();
Iterator<Map.Entry<Integer, String>> it= enSet.iterator();
while(it.hasNext())
{
Map.Entry<Integer, String> mapSet = it.next();
System.out.println(mapSet.getKey()+":"+mapSet.getValue());
}
//输出:1,2,3,4,
Set<Integer> keySet = map.keySet();
Iterator<Integer> itKeys = keySet.iterator();
while(itKeys.hasNext())
{
System.out.print(itKeys.next()+",");
}
System.out.println();
for(int key:keySet)
{
System.out.print(key+":"+map.get(key)+",");
}
System.out.println();
//输出:a,b,c,d,
Collection<String> values = map.values();
Iterator<String> itValues = values.iterator();
while(itValues.hasNext())
{
System.out.print(itValues.next()+",");
}
}
}
三、HashMap应用Properties,方法对一些配置文件进行操作
修饰符与类型 | 方法与描述 |
---|---|
String | getProperty(String key)
Searches for the property with the specified key in this property list.
|
String | getProperty(String key,String defaultValue)
Searches for the property with the specified key in this property list.
|
void | list(PrintStream out)
Prints this property list out to the specified output stream.
|
void | list(PrintWriter out)
Prints this property list out to the specified output stream.
|
void | load(InputStream inStream)
Reads a property list (key and element pairs) from the input byte stream.
|
void | load(Reader reader)
Reads a property list (key and element pairs) from the input character stream in a simple line-oriented format.
|
void | loadFromXML(InputStream in)
Loads all of the properties represented by the XML document on the specified input stream into this properties table.
|
Enumeration<?> | propertyNames()
Returns an enumeration of all the keys in this property list, including distinct keys in the default property list if a key of the same name has not already been found from the main properties list.
|
void | save(OutputStream out,String comments)
Deprecated.
This method does not throw an IOException if an I/O error occurs while saving the property list. The preferred way to save a properties list is via the
store(OutputStream out, String comments) method or thestoreToXML(OutputStream os, String comment) method.
|
Object | setProperty(String key,String value)
Calls the
Hashtable method
put .
|
void | store(OutputStream out,String comments)
Writes this property list (key and element pairs) in this
Properties table to the output stream in a format suitable for loading into a
Properties table using the
load(InputStream) method.
|
void | store(Writer writer,String comments)
Writes this property list (key and element pairs) in this
Properties table to the output character stream in a format suitable for using the
load(Reader) method.
|
void | storeToXML(OutputStream os,String comment)
Emits an XML document representing all of the properties contained in this table.
|
void | storeToXML(OutputStream os,String comment,String encoding)
Emits an XML document representing all of the properties contained in this table, using the specified encoding.
|
Set<String> | stringPropertyNames()
Returns a set of keys in this property list where the key and its corresponding value are strings, including distinct keys in the default property list if a key of the same name has not already been found from the main properties list
|
<span style="font-size:14px;">
</span>
<span style="font-size:14px;"> Properties pop =new Properties();
pop.setProperty("username", "hhp");
pop.setProperty("password", "123");
//存入到properties文件中
pop.store(new FileOutputStream("D:/workspace/Test/src/configure/config.properties"), "configure test by hhp");
Properties pop2= new Properties();
pop2.setProperty("sid", "hhpDB");
//添加到properties文件中
pop2.load(new FileInputStream("D:/workspace/Test/src/configure/config.properties"));
System.out.println(pop2);
//相关信息输入到xml文件中
pop2.storeToXML(new FileOutputStream("D:/workspace/Test/src/configure/config.xml"), "configure test by hhp");</span>