forked from dunwu/javacore
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
55 changed files
with
3,035 additions
and
8,739 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
codes/javacore-basics/src/main/java/io/github/dunwu/javacore/datatype/Integer判等.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package io.github.dunwu.javacore.datatype; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
|
||
/** | ||
* @author <a href="mailto:[email protected]">Zhang Peng</a> | ||
* @since 2020-08-06 | ||
*/ | ||
@Slf4j | ||
public class Integer判等 { | ||
|
||
public static void main(String[] args) { | ||
Integer a = 127; //Integer.valueOf(127) | ||
Integer b = 127; //Integer.valueOf(127) | ||
log.info("\nInteger a = 127;\nInteger b = 127;\na == b ? {}", a == b); // true | ||
|
||
Integer c = 128; //Integer.valueOf(128) | ||
Integer d = 128; //Integer.valueOf(128) | ||
log.info("\nInteger c = 128;\nInteger d = 128;\nc == d ? {}", c == d); //false | ||
//设置-XX:AutoBoxCacheMax=1000再试试 | ||
|
||
Integer e = 127; //Integer.valueOf(127) | ||
Integer f = new Integer(127); //new instance | ||
log.info("\nInteger e = 127;\nInteger f = new Integer(127);\ne == f ? {}", e == f); //false | ||
|
||
Integer g = new Integer(127); //new instance | ||
Integer h = new Integer(127); //new instance | ||
log.info("\nInteger g = new Integer(127);\nInteger h = new Integer(127);\ng == h ? {}", g == h); //false | ||
|
||
Integer i = 128; //unbox | ||
int j = 128; | ||
log.info("\nInteger i = 128;\nint j = 128;\ni == j ? {}", i == j); //true | ||
} | ||
|
||
} |
58 changes: 58 additions & 0 deletions
58
codes/javacore-basics/src/main/java/io/github/dunwu/javacore/datatype/Lombok生成Equals的问题.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package io.github.dunwu.javacore.datatype; | ||
|
||
import lombok.Data; | ||
import lombok.EqualsAndHashCode; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
/** | ||
* @author <a href="mailto:[email protected]">Zhang Peng</a> | ||
* @since 2020-08-06 | ||
*/ | ||
@Slf4j | ||
public class Lombok生成Equals的问题 { | ||
|
||
public static void main(String[] args) { | ||
test1(); | ||
test2(); | ||
} | ||
|
||
public static void test1() { | ||
Person person1 = new Person("zhuye", "001"); | ||
Person person2 = new Person("Joseph", "001"); | ||
log.info("person1.equals(person2) ? {}", person1.equals(person2)); | ||
} | ||
|
||
public static void test2() { | ||
Employee employee1 = new Employee("zhuye", "001", "bkjk.com"); | ||
Employee employee2 = new Employee("Joseph", "002", "bkjk.com"); | ||
log.info("employee1.equals(employee2) ? {}", employee1.equals(employee2)); | ||
} | ||
|
||
@Data | ||
static class Person { | ||
|
||
@EqualsAndHashCode.Exclude | ||
private String name; | ||
private String identity; | ||
|
||
public Person(String name, String identity) { | ||
this.name = name; | ||
this.identity = identity; | ||
} | ||
|
||
} | ||
|
||
@Data | ||
@EqualsAndHashCode(callSuper = true) | ||
static class Employee extends Person { | ||
|
||
private String company; | ||
|
||
public Employee(String name, String identity, String company) { | ||
super(name, identity); | ||
this.company = company; | ||
} | ||
|
||
} | ||
|
||
} |
28 changes: 28 additions & 0 deletions
28
codes/javacore-basics/src/main/java/io/github/dunwu/javacore/datatype/StringIntern性能测试.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package io.github.dunwu.javacore.datatype; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.IntStream; | ||
|
||
/** | ||
* 没事别轻易用 {@link String#intern},如果要用一定要注意控制驻留的字符串的数量,并留意常量表的各项指标 | ||
* | ||
* @author <a href="mailto:[email protected]">Zhang Peng</a> | ||
* @since 2020-08-06 | ||
*/ | ||
public class StringIntern性能测试 { | ||
|
||
public static void main(String[] args) { | ||
//-XX:+PrintStringTableStatistics,可以打印出字符串常量表的统计信息 | ||
//设置 -XX:StringTableSize=10000000 后,执行速度会快很多 | ||
List<String> list = new ArrayList<>(); | ||
long begin = System.currentTimeMillis(); | ||
list = IntStream.rangeClosed(1, 10000000) | ||
.mapToObj(i -> String.valueOf(i).intern()) | ||
.collect(Collectors.toList()); | ||
System.out.println("size:" + list.size()); | ||
System.out.println("time:" + (System.currentTimeMillis() - begin)); | ||
} | ||
|
||
} |
30 changes: 30 additions & 0 deletions
30
codes/javacore-basics/src/main/java/io/github/dunwu/javacore/datatype/String判等.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package io.github.dunwu.javacore.datatype; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
|
||
/** | ||
* @author <a href="mailto:[email protected]">Zhang Peng</a> | ||
* @since 2020-08-06 | ||
*/ | ||
@Slf4j | ||
public class String判等 { | ||
|
||
public static void main(String[] args) { | ||
String a = "1"; | ||
String b = "1"; | ||
log.info("\nString a = \"1\";\nString b = \"1\";\na == b ? {}", a == b); //true | ||
|
||
String c = new String("2"); | ||
String d = new String("2"); | ||
log.info("\nString c = new String(\"2\");\nString d = new String(\"2\");\nc == d ? {}", c == d); //false | ||
|
||
String e = new String("3").intern(); | ||
String f = new String("3").intern(); | ||
log.info("\nString e = new String(\"3\").intern();\nString f = new String(\"3\").intern();\ne == f ? {}", e == f); //true | ||
|
||
String g = new String("4"); | ||
String h = new String("4"); | ||
log.info("\nString g = new String(\"4\");\nString h = new String(\"4\");\ng == h ? {}", g.equals(h)); //true | ||
} | ||
|
||
} |
90 changes: 90 additions & 0 deletions
90
codes/javacore-basics/src/main/java/io/github/dunwu/javacore/datatype/equals和CompareTo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package io.github.dunwu.javacore.datatype; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.Comparator; | ||
import java.util.List; | ||
|
||
/** | ||
* @author <a href="mailto:[email protected]">Zhang Peng</a> | ||
* @since 2020-08-06 | ||
*/ | ||
@Slf4j | ||
public class equals和CompareTo { | ||
|
||
public static void main(String[] args) { | ||
wrong(); | ||
right(); | ||
} | ||
|
||
public static void wrong() { | ||
|
||
List<Student> list = new ArrayList<>(); | ||
list.add(new Student(1, "zhang")); | ||
list.add(new Student(2, "wang")); | ||
Student student = new Student(2, "li"); | ||
|
||
log.info("ArrayList.indexOf"); | ||
int index1 = list.indexOf(student); | ||
Collections.sort(list); | ||
log.info("Collections.binarySearch"); | ||
int index2 = Collections.binarySearch(list, student); | ||
|
||
log.info("index1 = " + index1); | ||
log.info("index2 = " + index2); | ||
} | ||
|
||
public static void right() { | ||
|
||
List<StudentRight> list = new ArrayList<>(); | ||
list.add(new StudentRight(1, "zhang")); | ||
list.add(new StudentRight(2, "wang")); | ||
StudentRight student = new StudentRight(2, "li"); | ||
|
||
log.info("ArrayList.indexOf"); | ||
int index1 = list.indexOf(student); | ||
Collections.sort(list); | ||
log.info("Collections.binarySearch"); | ||
int index2 = Collections.binarySearch(list, student); | ||
|
||
log.info("index1 = " + index1); | ||
log.info("index2 = " + index2); | ||
} | ||
|
||
@Data | ||
@AllArgsConstructor | ||
static class Student implements Comparable<Student> { | ||
|
||
private int id; | ||
private String name; | ||
|
||
@Override | ||
public int compareTo(Student other) { | ||
int result = Integer.compare(other.id, id); | ||
if (result == 0) { log.info("this {} == other {}", this, other); } | ||
return result; | ||
} | ||
|
||
} | ||
|
||
@Data | ||
@AllArgsConstructor | ||
static class StudentRight implements Comparable<StudentRight> { | ||
|
||
private int id; | ||
private String name; | ||
|
||
@Override | ||
public int compareTo(StudentRight other) { | ||
return Comparator.comparing(StudentRight::getName) | ||
.thenComparingInt(StudentRight::getId) | ||
.compare(this, other); | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ | |
* @author <a href="mailto:[email protected]">Zhang Peng</a> | ||
* @since 2019-03-26 | ||
*/ | ||
public class DataTypeDemo01 { | ||
public class 值类型使用示例 { | ||
|
||
public static void demo1() { | ||
// compile error | ||
|
@@ -95,7 +95,7 @@ public static void demo9() { | |
public static void main(String[] args) | ||
throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { | ||
for (int i = 1; i <= 10; i++) { | ||
Method method = DataTypeDemo01.class.getMethod("demo" + i); | ||
Method method = 值类型使用示例.class.getMethod("demo" + i); | ||
method.invoke(null); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ | |
* @author <a href="mailto:[email protected]">Zhang Peng</a> | ||
* @since 2019-03-26 | ||
*/ | ||
public class DataTypeDemo02 { | ||
public class 包装类型使用示例 { | ||
|
||
public static void main(String[] args) { | ||
printByte(); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
* @author <a href="mailto:[email protected]">Zhang Peng</a> | ||
* @since 2019-03-10 | ||
*/ | ||
public class BoxingDemo { | ||
public class 包装类装箱拆箱 { | ||
|
||
public static void main(String[] args) { | ||
Integer i1 = 10; // 自动装箱 | ||
|
45 changes: 45 additions & 0 deletions
45
codes/javacore-basics/src/main/java/io/github/dunwu/javacore/datatype/枚举判等.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package io.github.dunwu.javacore.datatype; | ||
|
||
import lombok.Data; | ||
|
||
/** | ||
* @author <a href="mailto:[email protected]">Zhang Peng</a> | ||
* @since 2020-08-06 | ||
*/ | ||
public class 枚举判等 { | ||
|
||
public static void main(String[] args) { | ||
StatusEnum statusEnum = StatusEnum.DELIVERED; | ||
OrderQuery orderQuery = new OrderQuery(StatusEnum.DELIVERED.status, StatusEnum.DELIVERED.desc); | ||
System.out.println(statusEnum.status == orderQuery.getStatus()); | ||
} | ||
|
||
@Data | ||
static class OrderQuery { | ||
|
||
private Integer status; | ||
private String name; | ||
|
||
public OrderQuery(Integer status, String name) { | ||
this.status = status; | ||
this.name = name; | ||
} | ||
|
||
} | ||
|
||
enum StatusEnum { | ||
CREATED(1000, "已创建"), | ||
PAID(1001, "已支付"), | ||
DELIVERED(1002, "已送到"), | ||
FINISHED(1003, "已完成"); | ||
|
||
private final Integer status; | ||
private final String desc; | ||
|
||
StatusEnum(Integer status, String desc) { | ||
this.status = status; | ||
this.desc = desc; | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.