Map集合
Map实现类用于保存具有映射关系的数据。Map保存的每项数据都是key-value对,也就是由key和value两个值组成。Map里的key是不可重复的,key用户标识集合里的每项数据。其中最主要的实现类是HashMap和TreeMap。
Map集合的体系结构:

HashMap
HashMap类实现了Map接口,HashMap是一个无序的key-value集合。
有下面几个特点:
-
HashMap类是基于key来包含value的。
-
HashMap类的key值不能重复。
-
HashMap类可以包含null值,并且可以包含多个。
-
HashMap类构造函数默认的大小是16,装载因子是0.75.
构造函数:
-
HashMap() 创建一个空的HashMap。
-
HashMap(Map<? extends K,? extends V> m) 基于一个map集合来创建一个新的集合。
-
HashMap(int capacity) 创建一个指定大小的map集合。
-
HashMap(int capacity, float loadFactor) 创建一个指定大小和指定装载因子的map集合。
常用方法:
method | Description |
---|---|
void clear() | It is used to remove all of the mappings from this map. |
boolean isEmpty() | It is used to return true if this map contains no key-value mappings. |
Object clone() | It is used to return a shallow copy of this HashMap instance: the keys and values themselves are not cloned. |
Set entrySet() | It is used to return a collection view of the mappings contained in this map. |
Set keySet() | It is used to return a set view of the keys contained in this map. |
V put(Object key, Object value) | It is used to insert an entry in the map. |
void putAll(Map map) | It is used to insert the specified map in the map. |
V remove(Object key) | It is used to delete an entry for the specified key. |
boolean remove(Object key, Object value) | It removes the specified values with the associated specified keys from the map. |
boolean containsValue(Object value) | This method returns true if some value equal to the value exists within the map, else return false. |
boolean containsKey(Object key) | This method returns true if some key equal to the key exists within the map, else return false. |
boolean equals(Object o) | It is used to compare the specified Object with the Map. |
V get(Object key) | This method returns the object that contains the value associated with the key. |
V getOrDefault(Object key, V defaultValue) | It returns the value to which the specified key is mapped, or defaultValue if the map contains no mapping for the key. |
Collection values() | It returns a collection view of the values contained in the map. |
int size() | This method returns the number of entries in the map. |
遍历方式
- for循环遍历
for (Map.Entry entry : mapCollection.entrySet()) {
// other code
}
- Iterator遍历
Iterator iterator = mapCollection.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry entry = (Map.Entry) iterator.next();
// other code
}
举个例子:
import java.util.HashMap;
import java.util.Map;
import java.util.Iterator;
import java.util.Set;
public class Details {
public static void main(String args[]) {
/* This is how to declare HashMap */
HashMap<Integer, String> hmap = new HashMap<Integer, String>();
/*Adding elements to HashMap*/
hmap.put(12, "Chaitanya");
hmap.put(2, "Rahul");
hmap.put(7, "Singh");
hmap.put(49, "Ajeet");
hmap.put(3, "Anuj");
/* Display content using Iterator*/
Set set = hmap.entrySet();
Iterator iterator = set.iterator();
while(iterator.hasNext()) {
Map.Entry mentry = (Map.Entry)iterator.next();
System.out.print("key is: "+ mentry.getKey() + " & Value is: ");
System.out.println(mentry.getValue());
}
/* Get values based on key*/
String var= hmap.get(2);
System.out.println("Value at index 2 is: "+var);
/* Remove values based on key*/
hmap.remove(3);
System.out.println("Map key and values after removal:");
Set set2 = hmap.entrySet();
Iterator iterator2 = set2.iterator();
while(iterator2.hasNext()) {
Map.Entry mentry2 = (Map.Entry)iterator2.next();
System.out.print("Key is: "+mentry2.getKey() + " & Value is: ");
System.out.println(mentry2.getValue());
}
}
}
网友评论