Skip to content

Commit 54e5a45

Browse files
committedApr 13, 2019
update
1 parent e2063e9 commit 54e5a45

File tree

6 files changed

+155
-2
lines changed

6 files changed

+155
-2
lines changed
 
+8-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
1+
### 一、集合
2+
3+
https://github.com/Snailclimb/JavaGuide/blob/master/docs/java/Java%E9%9B%86%E5%90%88%E6%A1%86%E6%9E%B6%E5%B8%B8%E8%A7%81%E9%9D%A2%E8%AF%95%E9%A2%98%E6%80%BB%E7%BB%93.md
14

25

3-
https://zhuanlan.zhihu.com/p/21673805
46

7+
### 二、HashMap底层
58

9+
https://zhuanlan.zhihu.com/p/21673805
610

711
https://javadoop.com/post/hashmap
812

913

1014

11-
https://github.com/Snailclimb/JavaGuide/blob/master/docs/java/Java%E9%9B%86%E5%90%88%E6%A1%86%E6%9E%B6%E5%B8%B8%E8%A7%81%E9%9D%A2%E8%AF%95%E9%A2%98%E6%80%BB%E7%BB%93.md
15+
### 三、Fail-fast机制
16+
17+
<https://blog.csdn.net/chenssy/article/details/38151189>
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,51 @@
11
# 集合相关题目
22

3+
## 一、HasdMap和HashTable的区别
4+
5+
### 1、关于null值
6+
7+
#### a)、HashMap
8+
9+
* HashMap中,`null`可以作为键,这样的键只有一个。
10+
* 可以有一个或多个键所对应的值为`null`
11+
*`get()`方法返回`null`值时,可能是`HashMap`中没有该键,也可能是键所对应的值为`null`
12+
* 因此,在`HashMap`中不能由`get()`方法来判断`HashMap`中是否存在某个键,而应该用`containsKey()`方法来判断。
13+
14+
#### b)、HashTable
15+
16+
* 既不支持`Null Key`,也不支持`Null Value`,如果`put(null, ...)`,就会抛出空指针异常;
17+
18+
### 2、迭代
19+
20+
另一个区别是HashMap的迭代器(`Iterator`)是`fail-fast`迭代器。
21+
22+
而Hashtable的`enumerator`迭代器不是fail-fast的。
23+
24+
所以当有其它线程改变了HashMap的结构(增加或者移除元素),将会抛出`ConcurrentModificationException`,但迭代器本身的`remove()`方法移除元素则不会抛出`ConcurrentModificationException`异常。
25+
26+
但这并不是一个一定发生的行为,要看JVM。这条同样也是Enumeration和Iterator的区别。
27+
28+
> “快速失败”也就是`fail-fast`,它是Java集合的一种错误检测机制。
29+
>
30+
> 当多个线程对集合进行结构上的改变的操作时,有可能会产生fail-fast机制。
31+
>
32+
> 记住是有可能,而不是一定。
33+
>
34+
> 例如:假设存在两个线程(线程1、线程2),线程1通过Iterator在遍历集合A中的元素,在某个时候线程2修改了集合A的结构(**是结构上面的修改,而不是简单的修改集合元素的内容**),那么这个时候程序就会抛出 ConcurrentModificationException 异常,从而产生fail-fast机制。
35+
36+
fail-fast底层: 维护`modCount == expectModCount`:
37+
38+
迭代器在调用`next()、remove()`方法时都是调用checkForComodification()方法。
39+
40+
该方法主要就是检测`modCount == expectedModCount` 。 若不等则抛出ConcurrentModificationException 异常,从而产生fail-fast机制。所以要弄清楚为什么会产生fail-fast机制我们就必须要用弄明白为什么modCount != expectedModCount ,他们的值在什么时候发生改变的。
41+
42+
比如:
43+
44+
有两个线程(线程A,线程B),其中线程A负责遍历list、线程B修改list。线程A在遍历list过程的某个时候(此时`expectedModCount = modCount=N`,线程启动,同时线程B增加一个元素,这是modCount的值发生改变\(`modCount + 1 = N + 1`)。线程A继续遍历执行next方法时,通告checkForComodification方法发现expectedModCount  = N  ,而modCount = N + 1,两者不等,这时就抛出ConcurrentModificationException 异常,从而产生fail-fast机制。
45+
46+
47+
48+
49+
50+
<https://www.cnblogs.com/heyonggang/p/9112731.html>
51+

‎Java基础/面试题系列.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<http://www.importnew.com/27326.html#comment-764309>

‎刷题/Other/.txt

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# 京东-寻找子串
2+
3+
#### 题目
4+
5+
给出`m`个字符串`S1、S2、S3...Sm`和一个单独的字符串`T`,请在T中选出尽可能多的子串同时满足:
6+
7+
* 1)、这些子串在`T`中互不相交;
8+
* 这些子串都是`S1、S2、S3...Sm`中的某个串;
9+
10+
问你最多能选出多少个子串。
11+
12+
#### 输入输出
13+
14+
给出m个字符串,接下来m行,每行一个串,最后一行输入一个串`T`。输出最多能选出多少个串。
15+
16+
```java
17+
输入:
18+
3
19+
aa
20+
b
21+
ac
22+
bbaac
23+
输出:
24+
3
25+
```
26+
27+
### 解析
28+
29+
DP。
30+
31+
代码:
32+
33+
```java
34+
import java.io.*;
35+
import java.util.*;
36+
37+
public class Main {
38+
39+
static void solve(InputStream stream, PrintWriter out) {
40+
FR in = new FR(stream);
41+
int m = in.nextInt();
42+
String[] S = new String[m];
43+
for (int i = 0; i < m; i++) S[i] = in.next();
44+
String T = in.next();
45+
int[] dp = new int[T.length() + 1];
46+
int tLen = T.length();
47+
for (int i = 1; i < tLen + 1; i++) {
48+
dp[i] = dp[i - 1];
49+
for (int j = 0; j < m; j++) {
50+
if (i - S[j].length() >= 0)
51+
if (T.substring(i - S[j].length(), i).equals(S[j]))
52+
dp[i] = Math.max(dp[i], dp[i - S[j].length()] + 1);
53+
}
54+
}
55+
System.out.println(dp[tLen]);
56+
}
57+
58+
/*--------------------------------------------------------------------------------------*/
59+
public static void main(String[] args) {
60+
OutputStream os = System.out;
61+
InputStream is = System.in;
62+
PrintWriter out = new PrintWriter(os);
63+
solve(is, out);
64+
out.close();
65+
}
66+
67+
static class FR {
68+
BufferedReader br;
69+
StringTokenizer tk;
70+
71+
FR(InputStream stream) {
72+
br = new BufferedReader(new InputStreamReader(stream), 32768);
73+
tk = null;
74+
}
75+
76+
String next() {
77+
while (tk == null || !tk.hasMoreElements()) {
78+
try {
79+
tk = new StringTokenizer(br.readLine());
80+
} catch (IOException e) {
81+
e.printStackTrace();
82+
}
83+
}
84+
return tk.nextToken();
85+
}
86+
87+
int nextInt() {
88+
return Integer.parseInt(next());
89+
}
90+
}
91+
}
92+
```
93+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# 好文章
2+
3+
* https://blog.csdn.net/qzcsu/article/details/72861891
4+
*

0 commit comments

Comments
 (0)
Please sign in to comment.