參考:
一:Java的Map中的map.keySet()方法
該方法返回map中所有key值的列表。
今天再代碼中看到了Map集合中的HashMap的map.keySet()方法,首先看一下這個方法的定義
/**
* Returns a {@link Set} view of the keys contained in this map.
* The set is backed by the map, so changes to the map are
* reflected in the set, and vice-versa. If the map is modified
* while an iteration over the set is in progress (except through
* the iterator's own <tt>remove</tt> operation), the results of
* the iteration are undefined. The set supports element removal,
* which removes the corresponding mapping from the map, via the
* <tt>Iterator.remove</tt>, <tt>Set.remove</tt>,
* <tt>removeAll</tt>, <tt>retainAll</tt>, and <tt>clear</tt>
* operations. It does not support the <tt>add</tt> or <tt>addAll</tt>
* operations.
*
* @return a set view of the keys contained in this map
*/
Set<K> keySet();
大致的意思描述如下:
1)返回此映射中包含的鍵的 Set視圖。
2)這個 set 受到映射支持,所以對映射的更改可在此 set 中反映出來,反之亦然。
3)如果對該 set 進行迭代的同時修改了映射(通過迭代器自己的 remove 操作除外),則迭代結果是不確定的。
4)set 支持元素移除,通過 Iterator.remove、 Set.remove、 removeAll、retainAll 和 clear 操作可從映射中移除(刪除)相應的映射關係。
5)set不支持 add 或 addAll 兩種添加操作。
6)返回值是:map包含的鍵的 set 視圖
代碼的使用:
Map<Integer, String> map = new HashMap<>();
//下面可以使用map.keySet()方法
map.keySet();
測試代碼:
public class SourceCode {
public static void main(String[] args) {
Map<String,String> map = new HashMap<String, String>();
map.put("xiaocui1","gongchen");
map.put("xiaocui2","daima");
map.put("xiaocui3","xuexi");
map.put("xiaocui4","dagong");
System.out.println(map.keySet());
System.out.println("-----分割線-----");
for(String map1 : map.keySet()){
String string = map.keySet().toString();
System.out.println(string);
}
}
}
輸出結果:
[xiaocui4, xiaocui1, xiaocui2, xiaocui3]
-----分割線-----
[xiaocui4, xiaocui1, xiaocui2, xiaocui3]
[xiaocui4, xiaocui1, xiaocui2, xiaocui3]
[xiaocui4, xiaocui1, xiaocui2, xiaocui3]
[xiaocui4, xiaocui1, xiaocui2, xiaocui3]
二:keyset()
三:Map對象中的keyset()、entryset()和Map.Entry
1. keySet()
public static void main(String[] args) {
Map<String, String> map = new HashMap<String, String>();
map.put("01", "qwe");
map.put("02", "asd");
map.put("03", "zxc");
// 先獲取map集合的所有鍵的set集合,即爲map中所有key值的集合
Set<String> keySet = map.keySet();
// 有了set集合,就可以獲取其迭代器
Iterator<String> it = keySet.iterator();
while (it.hasNext()) {
String key = it.next();
// 有了鍵可以通過map集合的get方法獲取其對應的值
String value = map.get(key);
// 獲得key和value值
System.out.println("key:" + key + "-->value:" + value);
}
}
keySet()返回的是map對象的key值的set集合
2. entrySet()
public static void main(String[] args) {
Map<String, String> map = new HashMap<String, String>();
map.put("01", "qwe");
map.put("02", "asd");
map.put("03", "zxc");
// 通過entrySet()方法將map集合中的映射關係取出(這個關係就是Map.Entry類型)
Set<Map.Entry<String, String>> entrySet = map.entrySet();
// 將關係集合entryset進行迭代,存放到迭代器中
Iterator<Map.Entry<String, String>> it2 = entrySet.iterator();
while (it2.hasNext()) {
// 獲取Map.Entry關係對象me
Map.Entry<String, String> me = it2.next();
// 通過關係對像獲取key
String key2 = me.getKey();
// 通過關係對像獲取value
String value2 = me.getValue();
System.out.println("key:" + key2 + "-->value:" + value2);
}
}
entrySet()返回映射所包含的映射關係的Set集合(一個關係就是一個鍵-值對),就是把(key-value)作爲一個整體一對一對地存放到Set集合當中的。
3.總結
雖然使用keyset及entryset來進行遍歷能取得相同的結果,但兩者的遍歷速度是有差別的。
keySet():迭代後只能通過get()取key;再根據key值取value。
entrySet():迭代後可以e.getKey(),e.getValue()取key和value。
同時,keySet()的速度比entrySet()慢了很多,也就是keySet方式遍歷Map的性能不如entrySet性能好
爲了提高性能,以後多考慮用entrySet()方式來進行遍歷。
二 Map.Entry
Map是java中的接口,Map.Entry是Map的一個內部接口。
Map提供了一些常用方法,如keySet()、entrySet()等方法,keySet()方法返回值是Map中key值的集合;entrySet()的返回值也是返回一個Set集合,此集合的類型爲Map.Entry。
Map.Entry是Map聲明的一個內部接口,此接口爲泛型,定義爲Entry<K,V>。它表示Map中的一個實體(一個key-value對)。接口中有getKey(),getValue方法。
遍歷Map對象的常用方法除了以上兩種外,還有一種是單純的遍歷value值。Map有一個values方法,返回的是value的Collection集合。通過遍歷Collection也可以遍歷value。
public static void main(String[] args) {
Map<String, String> map = new HashMap<String, String>();
map.put("01", "qwe");
map.put("02", "asd");
map.put("03", "zxc");
// 創建一個Collection集合,存放map的value值
Collection<String> c = map.values();
// 通過遍歷Collection也可以遍歷value
Iterator<String> it = c.iterator();
// 該方法只能遍歷value值,不能遍歷key值
while (it.hasNext()) {
Object value = it.next();
System.out.println("value:" + value);
}
}
在遍歷Map對象時,先從Map對象中取出key值之後,還必須每次重複返回到Map中取得相對的值,這是很繁瑣和費時的。
幸運的是,Map類提供了一個稱爲entrySet()的方法,這個方法返回一個Map.Entry實例化後的對象集。 接着,Map.Entry類提供了一個getKey()方法和一個getValue()方法。
Set entries = map.entrySet( );
if(entries != null) {
Iterator iterator = entries.iterator( );
while(iterator.hasNext( )) {
Map.Entry entry =iterator.next( );
Object key = entry.getKey( );
Object value = entry.getValue();
}
}
儘管增加了一行代碼,我們卻省略了許多對Map不必要的“get”調用。同時,提供給開發人員一個同時保持了關鍵字和其對應的值的類。Map.Entry同時也提供了一個setValue()方法,程序員可以使用它修改map裏面的值。
四:entrySet用法 以及遍歷map的用法
keySet是鍵的集合,Set裏面的類型即key的類型
entrySet是 鍵-值 對的集合,Set裏面的類型是Map.Entry
五:Map集合中value()方法與keySet()、entrySet()區別
在Map集合中
values():方法是獲取集合中的所有的值----沒有鍵,沒有對應關係,
KeySet():將Map中所有的鍵存入到set集合中。因爲set具備迭代器。所有可以迭代方式取出所有的鍵,再根據get方法。獲取每一個鍵對應的值。 keySet():迭代後只能通過get()取key
entrySet():
Set<Map.Entry<K,V>> entrySet() //返回此映射中包含的映射關係的 Set 視圖。 Map.Entry表示映射關係。entrySet():迭代後可以e.getKey(),e.getValue()取key和value。返回的是Entry接口 。
下面通過例子看看:
Map<String,String> map = new HashMap<String,String>();
map.put("01", "zhangsan");
map.put("02", "lisi");
map.put("03", "wangwu");
Collection<String> collection = map.values();//返回值是個值的Collection集合
System.out.println(collection);
打印結果:
[zhangsan, lisi, wangwu]
Set<K> keySet() //返回值是個只存放key值的Set集合(集合中無序存放的)
Set<Map.Entry<K,V>> entrySet() //返回映射所包含的映射關係的Set集合(一個關係就是一個鍵-值對),就是把(key-value)作爲一個整體一對一對地存放到Set集合當中的。
一. keySet()方式。
Map<String,String> map = new HashMap<String,String>();
map.put(“01”, “zhangsan”);
map.put(“02”, “lisi”);
map.put(“03”, “wangwu”);
Set<String> keySet = map.keySet();//先獲取map集合的所有鍵的Set集合
Iterator<String> it = keySet.iterator();//有了Set集合,就可以獲取其迭代器。
while(it.hasNext()){
String key = it.next();
String value = map.get(key);//有了鍵可以通過map集合的get方法獲取其對應的值。
System.out.println("key: “+key+”–>value: "+value);//獲得key和value值
}
二. entrySet()方式:
Map<String,String> map = new HashMap<String,String>();
map.put(“01”, “zhangsan”);
map.put(“02”, “lisi”);
map.put(“03”, “wangwu”);
//通過entrySet()方法將map集合中的映射關係取出(這個關係就是Map.Entry類型)
Set<Map.Entry<String, String>> entrySet = map.entrySet();
//將關係集合entrySet進行迭代,存放到迭代器中
Iterator<Map.Entry<String, String>> it2 = entrySet.iterator();
while(it2.hasNext()){
Map.Entry<String, String> me = it2.next();//獲取Map.Entry關係對象me
String key2 = me.getKey();//通過關係對象獲取key
String value2 = me.getValue();//通過關係對象獲取value
System.out.println("key: “+key2+”–>value: "+value2);
}
雖然使用keyset及entryset來進行遍歷能取得相同的結果
但兩者的遍歷速度是有差別的
keySet():迭代後只能通過get()取key。
entrySet():迭代後可以e.getKey(),e.getValue()取key和value。返回的是Entry接口
說明:keySet()的速度比entrySet()慢了很多,也就是keySet方式遍歷Map的性能不如entrySet性能好
爲了提高性能,以後多考慮用entrySet()方式來進行遍歷。
六:Java中Map的 entrySet() 詳解以及用法(四種遍歷map的方式)
由於Map中存放的元素均爲鍵值對,故每一個鍵值對必然存在一個映射關係。
Map中採用Entry內部類來表示一個映射項,映射項包含Key和Value (我們總說鍵值對鍵值對, 每一個鍵值對也就是一個Entry)
Map.Entry裏面包含getKey()和getValue()方法
Iterator<Map.Entry<Integer, Integer>> it=map.entrySet().iterator();
while(it.hasNext()) {
Map.Entry<Integer,Integer> entry=it.next();
int key=entry.getKey();
int value=entry.getValue();
System.out.println(key+" "+value);
}
2. entrySet
entrySet是 java中 鍵-值 對的集合,Set裏面的類型是Map.Entry,一般可以通過map.entrySet()得到。
- entrySet實現了Set接口,裏面存放的是鍵值對。一個K對應一個V。
用來遍歷map的一種方法。
Set<Map.Entry<String, String>> entryseSet=map.entrySet();
for (Map.Entry<String, String> entry:entryseSet) {
System.out.println(entry.getKey()+","+entry.getValue());
}
即通過getKey()得到K,getValue()得到V。
3. keySet
還有一種是keySet, keySet是鍵的集合,Set裏面的類型即key的類型
Set<String> set = map.keySet();
for (String s:set) {
System.out.println(s+","+map.get(s));
}
4. 四種遍歷Map方式:
public static void main(String[] args) {
Map<String, String> map = new HashMap<String, String>();
map.put("1", "value1");
map.put("2", "value2");
map.put("3", "value3");
//第一種:普遍使用,二次取值
System.out.println("通過Map.keySet遍歷key和value:");
for (String key : map.keySet()) {
System.out.println("key= "+ key + " and value= " + map.get(key));
}
//第二種
System.out.println("通過Map.entrySet使用iterator遍歷key和value:");
Iterator<Map.Entry<String, String>> it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, String> entry = it.next();
System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
}
//第三種:推薦,尤其是容量大時
System.out.println("通過Map.entrySet遍歷key和value");
for (Map.Entry<String, String> entry : map.entrySet()) {
System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
}
//第四種
System.out.println("通過Map.values()遍歷所有的value,但不能遍歷key");
for (String v : map.values()) {
System.out.println("value= " + v);
}
}