Skip to content

Commit

Permalink
📝 Writing docs.
Browse files Browse the repository at this point in the history
  • Loading branch information
crossoverJie committed May 5, 2018
1 parent 74c8179 commit 94b5cda
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 2 deletions.
25 changes: 25 additions & 0 deletions MD/HashMap.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,31 @@

get 和 put 类似,也是将传入的 Key 计算出 index ,如果该位置上是一个链表就需要遍历整个链表,通过 `key.equals(k)` 来找到对应的元素。

## 遍历方式


```java
Iterator<Map.Entry<String, Integer>> entryIterator = map.entrySet().iterator();
while (entryIterator.hasNext()) {
Map.Entry<String, Integer> next = entryIterator.next();
System.out.println("key=" + next.getKey() + " value=" + next.getValue());
}
```

```java
Iterator<String> iterator = map.keySet().iterator();
while (iterator.hasNext()){
String key = iterator.next();
System.out.println("key=" + key + " value=" + map.get(key));

}
```

**强烈建议**使用第一种 EntrySet 进行遍历。

第一种可以把 key value 同时取出,第二种还得需要通过 key 取一次 value,效率较低。


## notice

在并发环境下使用 `HashMap` 容易出现死循环。
Expand Down
2 changes: 1 addition & 1 deletion MD/collection/HashSet.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

## 构造函数

```
```java
public HashSet() {
map = new HashMap<>();
}
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Java 知识点,继续完善中。
- [ArrayList/Vector](https://github.com/crossoverJie/Java-Interview/blob/master/MD/ArrayList.md)
- [LinkedList](https://github.com/crossoverJie/Java-Interview/blob/master/MD/LinkedList.md)
- [HashMap](https://github.com/crossoverJie/Java-Interview/blob/master/MD/HashMap.md)
- [HashSet](https://github.com/crossoverJie/Java-Interview/blob/master/MD/HashMap.md)
- [LinkedHashMap](https://github.com/crossoverJie/Java-Interview/blob/master/MD/collection/LinkedHashMap.md)

### Java 多线程
Expand Down Expand Up @@ -67,7 +68,7 @@ Java 知识点,继续完善中。
- [是否为快乐数字](https://github.com/crossoverJie/Java-Interview/blob/master/src/main/java/com/crossoverjie/algorithm/HappyNum.java#L38-L55)
- [链表是否有环](https://github.com/crossoverJie/Java-Interview/blob/master/src/main/java/com/crossoverjie/algorithm/LinkLoop.java#L32-L59)
- [从一个数组中返回两个值相加等于目标值的下标](https://github.com/crossoverJie/Java-Interview/blob/master/src/main/java/com/crossoverjie/algorithm/TwoSum.java#L38-L59)
- [一致 Hash 算法](https://github.com/crossoverJie/Java-Interview/blob/master/MD/Consistent-Hash.md)
- [一致性 Hash 算法](https://github.com/crossoverJie/Java-Interview/blob/master/MD/Consistent-Hash.md)
- [限流算法](https://github.com/crossoverJie/Java-Interview/blob/master/MD/Limiting.md)
- [三种方式反向打印单向链表](https://github.com/crossoverJie/Java-Interview/blob/master/src/main/java/com/crossoverjie/algorithm/ReverseNode.java)
- [合并两个排好序的链表](https://github.com/crossoverJie/Java-Interview/blob/master/src/main/java/com/crossoverjie/algorithm/MergeTwoSortedLists.java)
Expand Down
37 changes: 37 additions & 0 deletions src/main/java/com/crossoverjie/basic/HashMapTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.crossoverjie.basic;

import java.security.Key;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
* Function:
*
* @author crossoverJie
* Date: 05/05/2018 12:42
* @since JDK 1.8
*/
public class HashMapTest {
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<>(16);
map.put("1", 1);
map.put("2", 2);
map.put("3", 3);
map.put("4", 4);

Iterator<Map.Entry<String, Integer>> entryIterator = map.entrySet().iterator();
while (entryIterator.hasNext()) {
Map.Entry<String, Integer> next = entryIterator.next();
System.out.println("key=" + next.getKey() + " value=" + next.getValue());
}
System.out.println("=============");

Iterator<String> iterator = map.keySet().iterator();
while (iterator.hasNext()){
String key = iterator.next();
System.out.println("key=" + key + " value=" + map.get(key));

}
}
}

0 comments on commit 94b5cda

Please sign in to comment.