File tree Expand file tree Collapse file tree 4 files changed +65
-2
lines changed
src/main/java/com/crossoverjie/basic Expand file tree Collapse file tree 4 files changed +65
-2
lines changed Original file line number Diff line number Diff line change 2222
2323get 和 put 类似,也是将传入的 Key 计算出 index ,如果该位置上是一个链表就需要遍历整个链表,通过 ` key.equals(k) ` 来找到对应的元素。
2424
25+ ## 遍历方式
26+
27+
28+ ``` java
29+ Iterator<Map .Entry<String , Integer > > entryIterator = map. entrySet(). iterator();
30+ while (entryIterator. hasNext()) {
31+ Map . Entry<String , Integer > next = entryIterator. next();
32+ System . out. println(" key=" + next. getKey() + " value=" + next. getValue());
33+ }
34+ ```
35+
36+ ``` java
37+ Iterator<String > iterator = map. keySet(). iterator();
38+ while (iterator. hasNext()){
39+ String key = iterator. next();
40+ System . out. println(" key=" + key + " value=" + map. get(key));
41+
42+ }
43+ ```
44+
45+ ** 强烈建议** 使用第一种 EntrySet 进行遍历。
46+
47+ 第一种可以把 key value 同时取出,第二种还得需要通过 key 取一次 value,效率较低。
48+
49+
2550## notice
2651
2752在并发环境下使用 ` HashMap ` 容易出现死循环。
Original file line number Diff line number Diff line change 1919
2020## 构造函数
2121
22- ```
22+ ``` java
2323 public HashSet() {
2424 map = new HashMap<> ();
2525 }
Original file line number Diff line number Diff line change @@ -15,6 +15,7 @@ Java 知识点,继续完善中。
1515- [ ArrayList/Vector] ( https://github.com/crossoverJie/Java-Interview/blob/master/MD/ArrayList.md )
1616- [ LinkedList] ( https://github.com/crossoverJie/Java-Interview/blob/master/MD/LinkedList.md )
1717- [ HashMap] ( https://github.com/crossoverJie/Java-Interview/blob/master/MD/HashMap.md )
18+ - [ HashSet] ( https://github.com/crossoverJie/Java-Interview/blob/master/MD/HashMap.md )
1819- [ LinkedHashMap] ( https://github.com/crossoverJie/Java-Interview/blob/master/MD/collection/LinkedHashMap.md )
1920
2021### Java 多线程
@@ -67,7 +68,7 @@ Java 知识点,继续完善中。
6768- [ 是否为快乐数字] ( https://github.com/crossoverJie/Java-Interview/blob/master/src/main/java/com/crossoverjie/algorithm/HappyNum.java#L38-L55 )
6869- [ 链表是否有环] ( https://github.com/crossoverJie/Java-Interview/blob/master/src/main/java/com/crossoverjie/algorithm/LinkLoop.java#L32-L59 )
6970- [ 从一个数组中返回两个值相加等于目标值的下标] ( https://github.com/crossoverJie/Java-Interview/blob/master/src/main/java/com/crossoverjie/algorithm/TwoSum.java#L38-L59 )
70- - [ 一致 Hash 算法] ( https://github.com/crossoverJie/Java-Interview/blob/master/MD/Consistent-Hash.md )
71+ - [ 一致性 Hash 算法] ( https://github.com/crossoverJie/Java-Interview/blob/master/MD/Consistent-Hash.md )
7172- [ 限流算法] ( https://github.com/crossoverJie/Java-Interview/blob/master/MD/Limiting.md )
7273- [ 三种方式反向打印单向链表] ( https://github.com/crossoverJie/Java-Interview/blob/master/src/main/java/com/crossoverjie/algorithm/ReverseNode.java )
7374- [ 合并两个排好序的链表] ( https://github.com/crossoverJie/Java-Interview/blob/master/src/main/java/com/crossoverjie/algorithm/MergeTwoSortedLists.java )
Original file line number Diff line number Diff line change 1+ package com .crossoverjie .basic ;
2+
3+ import java .security .Key ;
4+ import java .util .HashMap ;
5+ import java .util .Iterator ;
6+ import java .util .Map ;
7+
8+ /**
9+ * Function:
10+ *
11+ * @author crossoverJie
12+ * Date: 05/05/2018 12:42
13+ * @since JDK 1.8
14+ */
15+ public class HashMapTest {
16+ public static void main (String [] args ) {
17+ Map <String , Integer > map = new HashMap <>(16 );
18+ map .put ("1" , 1 );
19+ map .put ("2" , 2 );
20+ map .put ("3" , 3 );
21+ map .put ("4" , 4 );
22+
23+ Iterator <Map .Entry <String , Integer >> entryIterator = map .entrySet ().iterator ();
24+ while (entryIterator .hasNext ()) {
25+ Map .Entry <String , Integer > next = entryIterator .next ();
26+ System .out .println ("key=" + next .getKey () + " value=" + next .getValue ());
27+ }
28+ System .out .println ("=============" );
29+
30+ Iterator <String > iterator = map .keySet ().iterator ();
31+ while (iterator .hasNext ()){
32+ String key = iterator .next ();
33+ System .out .println ("key=" + key + " value=" + map .get (key ));
34+
35+ }
36+ }
37+ }
You can’t perform that action at this time.
0 commit comments