Enum
Enum 枚举 可以通过构造函数添加描述信息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| enum Code { INVALID("this is invalid"), WHATEVER("whatever"), GOOD("Good");
String msg;
Code(String msg) { this.msg = msg; } }
public class Main { static void main(String[] args) { Code code = Code.INVALID; System.out.println(code.msg); } }
|
HashMap
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| public class Main { static void main(String[] args) { HashMap<String, String> map = new HashMap<String,String>(); map.put("A", "something A"); map.put("B", "something B"); map.remove("B"); System.out.println(map); System.out.println(map.get("A")); System.out.println(map.size()); System.out.println(map.containsKey("A")); map.replace("A", "new A");
System.out.println(map); } }
|
向上转型与向下转型
向上转型:父类引用指向子类对象
向下转型:子类引用指向父类对象
1 2 3 4 5 6 7 8 9 10 11
| public class Main { static void main(String[] args) { Parent parent = new Child(); System.out.println(parent.getClass()); System.out.println(parent instanceof Child); System.out.println(parent instanceof Parent); } }
public class Parent {} public class Child extends Parent {}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| class Animal { public void display() { System.out.println("Animal"); } }
class Dog extends Animal { public void display() { System.out.println("dog"); }
public void eat() { System.out.println("吃骨头"); } }
public class Main{ public static void main(String[] args) { Animal animal = new Dog(); animal.display();
animal = (Dog)animal; ((Dog) animal).eat(); } }
|
安全性:使用 instanceof 判断
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| public class Main { public static void main(String[] args) { Animal animal = new Dog(); if(animal instanceof Dog) { animal = (Dog)animal; ((Dog) animal).eat(); } else { System.out.println("Animal无法向下转型为Dog"); } } }
|
Lambda
Lambda 表达式
1 2 3 4 5 6 7 8
| public class Main { static void main(String[] args) { Runnable r = () -> { System.out.println("Hello World"); } r.run(); } }
|
FunctionalInterface 函数式接口是 Java 8 引入的一种新特性,它是一个有且仅有一个抽象方法的接口,但可以有多个非抽象方法。函数式接口可以被隐式转换为 lambda 表达式,使得代码更加简洁和易读。
1 2 3 4 5 6 7 8 9 10 11
| @FunctionalInterface interface MyInterface { void DoSome(); }
public class Main{ public static void main(String[] args) { MyInterface face = () -> System.out.println("das"); face.DoSome(); } }
|
泛型 public static <T> void
1 2 3 4 5 6 7 8 9 10 11
| public class Main { public static void main(String[] args) { displayThing("DSDD"); displayThing(1234); }
public static <T> void displayThing(T t) { System.out.println(t); } }
|
泛型类 class MyGenericClass<T>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| public class Main { public static void main(String[] args) { MyGenericClass<Integer> class1 = new MyGenericClass<>(123); class1.display();
}
}
class MyGenericClass<T> { T t;
MyGenericClass(T t) { this.t = t; }
void display() { System.out.println(t); } }
|
带有约束的泛型 <T extends Number>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| public class Main { public static void main(String[] args) { displayThing(123); } public static <T extends Number> void displayThing(T t) { System.out.println(t); } }
## Thread
```java public class Main { public static void main(String[] args) { MyThread thread = new MyThread(); thread.start(); } }
class MyThread extends Thread { @Override public void run() { System.out.println("Thread is running"); } }
|
Thread 和 Runnable 的区别:
Runnable 是接口:代表要执行的任务,里面只有一个 run() 方法,描述线程要做什么。
Thread 是类:代表线程本身,负责创建、启动、管理线程。Thread 类本身也实现了 Runnable。
1 2 3 4 5 6 7 8
| class MyTask implements Runnable { @Override public void run() { System.out.println("线程任务"); } }
new Thread(new MyTask()).start();
|
优点:只是实现接口,不占用继承位;同一个任务对象可以交给多个线程执行。
共享变量例子:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| class Task implements Runnable{ private int num = 10; @Override public void run() { while(num>0){ System.out.println(num--); } } } public class Test{ public static void main(String[] args) { Task task = new Task(); new Thread(task).start(); new Thread(task).start(); } }
|
ThreadPool
线程池:线程复用,避免频繁创建线程,提高性能。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| import java.util.concurrent.*;
public class ThreadPoolDemo { public static void main(String[] args) { ThreadPoolExecutor pool = new ThreadPoolExecutor( 2, 5, 3L, TimeUnit.SECONDS, new ArrayBlockingQueue<>(10), Executors.defaultThreadFactory(), new ThreadPoolExecutor.CallerRunsPolicy() );
for (int i = 0; i < 15; i++) { int idx = i; pool.execute(() -> { System.out.println(Thread.currentThread().getName() + " 执行任务:" + idx); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } }); }
pool.shutdown(); } }
|