Skip to content

Commit 655cd7e

Browse files
committed
update codes and docs
1 parent 99aa391 commit 655cd7e

File tree

11 files changed

+696
-44
lines changed

11 files changed

+696
-44
lines changed

assets/JVM/Java故障排查.eddx

-35.6 KB
Binary file not shown.

assets/JVM/Java故障排查.xmind

-218 KB
Binary file not shown.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package io.github.dunwu.javacore.datatype;
2+
3+
import java.math.BigDecimal;
4+
import java.util.HashSet;
5+
import java.util.Set;
6+
import java.util.TreeSet;
7+
8+
/**
9+
* @author <a href="mailto:forbreak@163.com">Zhang Peng</a>
10+
* @since 2020-08-11
11+
*/
12+
public class BigDecimal判等 {
13+
14+
public static void main(String[] args) {
15+
System.out.println("====================== wrong ======================");
16+
wrong();
17+
System.out.println("====================== right ======================");
18+
right();
19+
System.out.println("====================== set ======================");
20+
set();
21+
}
22+
23+
private static void wrong() {
24+
System.out.println(new BigDecimal("1.0").equals(new BigDecimal("1")));
25+
}
26+
27+
private static void right() {
28+
System.out.println(new BigDecimal("1.0").compareTo(new BigDecimal("1")) == 0);
29+
}
30+
31+
private static void set() {
32+
Set<BigDecimal> hashSet1 = new HashSet<>();
33+
hashSet1.add(new BigDecimal("1.0"));
34+
System.out.println(hashSet1.contains(new BigDecimal("1")));//返回false
35+
36+
Set<BigDecimal> hashSet2 = new HashSet<>();
37+
hashSet2.add(new BigDecimal("1.0").stripTrailingZeros());
38+
System.out.println(hashSet2.contains(new BigDecimal("1.000").stripTrailingZeros()));//返回true
39+
40+
Set<BigDecimal> treeSet = new TreeSet<>();
41+
treeSet.add(new BigDecimal("1.0"));
42+
System.out.println(treeSet.contains(new BigDecimal("1")));//返回true
43+
}
44+
45+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package io.github.dunwu.javacore.datatype;
2+
3+
import java.math.BigInteger;
4+
5+
/**
6+
* @author <a href="mailto:forbreak@163.com">Zhang Peng</a>
7+
* @since 2020-08-11
8+
*/
9+
public class 数值溢出 {
10+
11+
public static void main(String[] args) {
12+
System.out.println("====================== wrong1 ======================");
13+
wrong();
14+
System.out.println("====================== right1 ======================");
15+
right1();
16+
System.out.println("====================== right2 ======================");
17+
right2();
18+
}
19+
20+
private static void wrong() {
21+
long l = Long.MAX_VALUE;
22+
System.out.println(l + 1); // -9223372036854775808
23+
System.out.println(l + 1 == Long.MIN_VALUE); // true
24+
}
25+
26+
private static void right1() {
27+
try {
28+
long l = Long.MAX_VALUE;
29+
System.out.println(Math.addExact(l, 1));
30+
} catch (Exception ex) {
31+
ex.printStackTrace();
32+
}
33+
}
34+
35+
private static void right2() {
36+
BigInteger i = new BigInteger(String.valueOf(Long.MAX_VALUE));
37+
System.out.println(i.add(BigInteger.ONE).toString());
38+
39+
try {
40+
long l = i.add(BigInteger.ONE).longValueExact();
41+
} catch (Exception ex) {
42+
ex.printStackTrace();
43+
}
44+
}
45+
46+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package io.github.dunwu.javacore.datatype;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
5+
import java.math.BigDecimal;
6+
7+
/**
8+
* 使用 BigDecimal 表示和计算浮点数,且务必使用字符串的构造方法来初始化 BigDecimal
9+
*
10+
* @author <a href="mailto:forbreak@163.com">Zhang Peng</a>
11+
* @since 2020-08-11
12+
*/
13+
@Slf4j
14+
public class 数值计算示例 {
15+
16+
public static void main(String[] args) {
17+
System.out.println("====================== wrong1 ======================");
18+
wrong1();
19+
System.out.println("====================== wrong2 ======================");
20+
wrong2();
21+
System.out.println("====================== right ======================");
22+
right();
23+
System.out.println("====================== testScale ======================");
24+
testScale();
25+
}
26+
27+
/**
28+
* 浮点数计算结果不符合预期的场景
29+
*/
30+
private static void wrong1() {
31+
System.out.println(0.1 + 0.2); // 0.30000000000000004
32+
System.out.println(1.0 - 0.8); // 0.19999999999999996
33+
System.out.println(4.015 * 100); // 401.49999999999994
34+
System.out.println(123.3 / 100); // 1.2329999999999999
35+
double amount1 = 2.15;
36+
double amount2 = 1.10;
37+
System.out.println(amount1 - amount2); // 1.0499999999999998
38+
}
39+
40+
/**
41+
* BigDecimal 不正确的初始化方式
42+
*/
43+
private static void wrong2() {
44+
System.out.println(new BigDecimal(0.1).add(new BigDecimal(0.2)));
45+
// Output: 0.3000000000000000166533453693773481063544750213623046875
46+
47+
System.out.println(new BigDecimal(1.0).subtract(new BigDecimal(0.8)));
48+
// Output: 0.1999999999999999555910790149937383830547332763671875
49+
50+
System.out.println(new BigDecimal(4.015).multiply(new BigDecimal(100)));
51+
// Output: 401.49999999999996802557689079549163579940795898437500
52+
53+
System.out.println(new BigDecimal(123.3).divide(new BigDecimal(100)));
54+
// Output: 1.232999999999999971578290569595992565155029296875
55+
}
56+
57+
/**
58+
* BigDecimal 正确的初始化方式
59+
*/
60+
private static void right() {
61+
System.out.println(new BigDecimal("0.1").add(new BigDecimal("0.2")));
62+
System.out.println(new BigDecimal("1.0").subtract(new BigDecimal("0.8")));
63+
System.out.println(new BigDecimal("4.015").multiply(new BigDecimal("100")));
64+
System.out.println(new BigDecimal("123.3").divide(new BigDecimal("100")));
65+
}
66+
67+
/**
68+
* BigDecimal 浮点数精度测试
69+
*/
70+
private static void testScale() {
71+
BigDecimal bigDecimal1 = new BigDecimal("100");
72+
BigDecimal bigDecimal2 = new BigDecimal(String.valueOf(100d));
73+
BigDecimal bigDecimal3 = new BigDecimal(String.valueOf(100));
74+
BigDecimal bigDecimal4 = BigDecimal.valueOf(100d);
75+
BigDecimal bigDecimal5 = new BigDecimal(Double.toString(100));
76+
77+
print(bigDecimal1); //scale 0 precision 3 result 401.500
78+
print(bigDecimal2); //scale 1 precision 4 result 401.5000
79+
print(bigDecimal3); //scale 0 precision 3 result 401.500
80+
print(bigDecimal4); //scale 1 precision 4 result 401.5000
81+
print(bigDecimal5); //scale 1 precision 4 result 401.5000
82+
}
83+
84+
private static void print(BigDecimal bigDecimal) {
85+
log.info("scale {} precision {} result {}", bigDecimal.scale(), bigDecimal.precision(),
86+
bigDecimal.multiply(new BigDecimal("4.015")));
87+
}
88+
89+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package io.github.dunwu.javacore.datatype;
2+
3+
import java.math.BigDecimal;
4+
import java.math.RoundingMode;
5+
import java.text.DecimalFormat;
6+
7+
/**
8+
* @author <a href="mailto:forbreak@163.com">Zhang Peng</a>
9+
* @since 2020-08-11
10+
*/
11+
public class 浮点数舍入 {
12+
13+
public static void main(String[] args) {
14+
System.out.println("====================== wrong1 ======================");
15+
wrong1();
16+
System.out.println("====================== wrong2 ======================");
17+
wrong2();
18+
System.out.println("====================== right ======================");
19+
right();
20+
}
21+
22+
private static void wrong1() {
23+
double num1 = 3.35;
24+
float num2 = 3.35f;
25+
System.out.println(String.format("%.1f", num1)); // 3.4
26+
System.out.println(String.format("%.1f", num2)); // 3.3
27+
}
28+
29+
private static void wrong2() {
30+
double num1 = 3.35;
31+
float num2 = 3.35f;
32+
DecimalFormat format = new DecimalFormat("#.##");
33+
format.setRoundingMode(RoundingMode.DOWN);
34+
System.out.println(format.format(num1)); // 3.35
35+
format.setRoundingMode(RoundingMode.DOWN);
36+
System.out.println(format.format(num2)); // 3.34
37+
}
38+
39+
private static void right() {
40+
BigDecimal num1 = new BigDecimal("3.35");
41+
BigDecimal num2 = num1.setScale(1, BigDecimal.ROUND_DOWN);
42+
System.out.println(num2); // 3.3
43+
BigDecimal num3 = num1.setScale(1, BigDecimal.ROUND_HALF_UP);
44+
System.out.println(num3); // 3.4
45+
}
46+
47+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package io.github.dunwu.javacore.container.list;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
5+
import java.util.ArrayList;
6+
import java.util.Arrays;
7+
import java.util.List;
8+
import java.util.stream.Collectors;
9+
10+
/**
11+
* @author <a href="mailto:forbreak@163.com">Zhang Peng</a>
12+
* @since 2020-08-11
13+
*/
14+
@Slf4j
15+
public class AsList示例 {
16+
17+
public static void main(String[] args) {
18+
System.out.println("====================== wrong1 ======================");
19+
wrong1();
20+
System.out.println("====================== right1 ======================");
21+
right1();
22+
System.out.println("====================== wrong2 ======================");
23+
wrong2();
24+
System.out.println("====================== right2 ======================");
25+
right2();
26+
}
27+
28+
private static void wrong1() {
29+
int[] arr = { 1, 2, 3 };
30+
List list = Arrays.asList(arr);
31+
log.info("list:{} size:{} class:{}", list, list.size(), list.get(0).getClass());
32+
}
33+
34+
private static void right1() {
35+
int[] arr1 = { 1, 2, 3 };
36+
List list1 = Arrays.stream(arr1).boxed().collect(Collectors.toList());
37+
log.info("list:{} size:{} class:{}", list1, list1.size(), list1.get(0).getClass());
38+
39+
Integer[] arr2 = { 1, 2, 3 };
40+
List list2 = Arrays.asList(arr2);
41+
log.info("list:{} size:{} class:{}", list2, list2.size(), list2.get(0).getClass());
42+
}
43+
44+
private static void wrong2() {
45+
String[] arr = { "1", "2", "3" };
46+
List list = Arrays.asList(arr);
47+
arr[1] = "4";
48+
try {
49+
list.add("5");
50+
} catch (Exception ex) {
51+
ex.printStackTrace();
52+
}
53+
log.info("arr:{} list:{}", Arrays.toString(arr), list);
54+
}
55+
56+
private static void right2() {
57+
String[] arr = { "1", "2", "3" };
58+
List list = new ArrayList(Arrays.asList(arr));
59+
arr[1] = "4";
60+
try {
61+
list.add("5");
62+
} catch (Exception ex) {
63+
ex.printStackTrace();
64+
}
65+
log.info("arr:{} list:{}", Arrays.toString(arr), list);
66+
}
67+
68+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package io.github.dunwu.javacore.container.list;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.stream.Collectors;
6+
import java.util.stream.IntStream;
7+
8+
/**
9+
* @author <a href="mailto:forbreak@163.com">Zhang Peng</a>
10+
* @since 2020-08-11
11+
*/
12+
public class SubList示例 {
13+
14+
public static void main(String[] args) throws InterruptedException {
15+
// oom();
16+
// oomfix();
17+
wrong();
18+
// right1();
19+
// right2();
20+
}
21+
22+
private static List<List<Integer>> data = new ArrayList<>();
23+
24+
private static void oom() {
25+
for (int i = 0; i < 1000; i++) {
26+
List<Integer> rawList = IntStream.rangeClosed(1, 100000).boxed().collect(Collectors.toList());
27+
data.add(rawList.subList(0, 1));
28+
}
29+
}
30+
31+
private static void oomfix() {
32+
for (int i = 0; i < 1000; i++) {
33+
List<Integer> rawList = IntStream.rangeClosed(1, 100000).boxed().collect(Collectors.toList());
34+
data.add(new ArrayList<>(rawList.subList(0, 1)));
35+
}
36+
}
37+
38+
private static void wrong() {
39+
List<Integer> list = IntStream.rangeClosed(1, 10).boxed().collect(Collectors.toList());
40+
List<Integer> subList = list.subList(1, 4);
41+
System.out.println(subList);
42+
subList.remove(1);
43+
System.out.println(list);
44+
list.add(0);
45+
try {
46+
subList.forEach(System.out::println);
47+
} catch (Exception ex) {
48+
ex.printStackTrace();
49+
}
50+
}
51+
52+
private static void right1() {
53+
List<Integer> list = IntStream.rangeClosed(1, 10).boxed().collect(Collectors.toList());
54+
List<Integer> subList = new ArrayList<>(list.subList(1, 4));
55+
System.out.println(subList);
56+
subList.remove(1);
57+
System.out.println(list);
58+
list.add(0);
59+
subList.forEach(System.out::println);
60+
}
61+
62+
private static void right2() {
63+
List<Integer> list = IntStream.rangeClosed(1, 10).boxed().collect(Collectors.toList());
64+
List<Integer> subList = list.stream().skip(1).limit(3).collect(Collectors.toList());
65+
System.out.println(subList);
66+
subList.remove(1);
67+
System.out.println(list);
68+
list.add(0);
69+
subList.forEach(System.out::println);
70+
}
71+
72+
}

0 commit comments

Comments
 (0)