Java集合之Map

一、综述

      Map用来保存具有映射关系的元素,以键值对(key-value)的形式出现。key-value存在意义对应的关系,通过key可以找到唯一的value,所以Map集合中的key不可以重复,key类似于集合中的Set,value值可以重复。Java中Map集合可以类似Set去看,HashMap、LinkedHashMap、TreeMap。Java源码就是在先实现Map 的基础之上,通过让value值为null来实现的Set集合。

二、HashMap常用方法

修饰符与类型方法与描述
voidclear()
Removes all of the mappings from this map.
Objectclone()
Returns a shallow copy of this HashMap instance: the keys and values themselves are not cloned.
booleancontainsKey(Object key)
Returns true if this map contains a mapping for the specified key.
booleancontainsValue(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.
Vget(Object key)
Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.
booleanisEmpty()
Returns true if this map contains no key-value mappings.
Set<K>keySet()
Returns a Set view of the keys contained in this map.
Vput(K key,V value)
Associates the specified value with the specified key in this map.
voidputAll(Map<? extendsK,? extends V> m)
Copies all of the mappings from the specified map to this map.
Vremove(Object key)
Removes the mapping for the specified key from this map if present.
intsize()
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,方法对一些配置文件进行操作


修饰符与类型方法与描述
StringgetProperty(String key)
Searches for the property with the specified key in this property list.
StringgetProperty(String key,String defaultValue)
Searches for the property with the specified key in this property list.
voidlist(PrintStream out)
Prints this property list out to the specified output stream.
voidlist(PrintWriter out)
Prints this property list out to the specified output stream.
voidload(InputStream inStream)
Reads a property list (key and element pairs) from the input byte stream.
voidload(Reader reader)
Reads a property list (key and element pairs) from the input character stream in a simple line-oriented format.
voidloadFromXML(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.
voidsave(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 thestore(OutputStream out, String comments) method or thestoreToXML(OutputStream os, String comment) method.
ObjectsetProperty(String key,String value)
Calls the Hashtable method put.
voidstore(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.
voidstore(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.
voidstoreToXML(OutputStream os,String comment)
Emits an XML document representing all of the properties contained in this table.
voidstoreToXML(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>
   


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值