Java集合之Map
来源:互联网 发布:淘宝上评价管理在哪里 编辑:程序博客网 时间:2024/06/04 19:38
一、综述
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 mappackage 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 aProperties
table using theload(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 theload(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>
0 0
- Java集合之-Map集合
- java之Map集合
- Java集合之Map
- Java集合之Map
- Java集合之Map
- Java集合之Map
- JAVA 集合之MAP
- java集合之Map
- java 集合之Map
- Java集合之Map
- Java集合之Map
- Java集合之Map
- Java集合之Map
- java集合之Map
- java集合之Map
- Java集合之Map
- Java集合之Map
- Java-集合之Map
- Adobe的AIR
- OpenCv中cvCaptureFromFile返回NULL的问题
- hexo博客--搭建
- hdu 计算机学院大学生程序设计竞赛(2015’11)游乐场
- Http缓存
- Java集合之Map
- 你造SQL中的 Where 1=1 吗
- Swiftype添加站内搜索
- List集合的clear方法
- 短作业优先(SJF)算法
- 如何判断一个IP地址是私有地址
- 文件的操作2
- hexo-学习写博客内容
- 自定义View