Java 5 新特性

1.自动装箱、拆箱

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/**
* 自动装箱、拆箱
*
* @author GaoHangHang
* @date 2018/06/02 23:29
**/
public class AutoBoxing {

public static void main(String[] args) {

int a = new Integer(66);
Integer b = 18;

Boolean flag = true;
boolean isBug = Boolean.FALSE;

}

}

2.并发库

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.concurrent.*;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
* 并发库
*
* @author biezhi
* @date 2018/2/8
*/
public class Concurrent {

/*
重入锁
*/
public void lock() {
Lock lock = new ReentrantLock();
lock.lock();
try {
System.out.println("hello world");
} finally {
lock.unlock();
}
}

/*
线程池
*/
public void condition() throws InterruptedException {
Lock lock = new ReentrantLock();
Condition condition = lock.newCondition();
// do something
condition.await(10, TimeUnit.SECONDS);
System.out.println("Get result.");
}


public void executorService() {
ExecutorService executorService = Executors.newFixedThreadPool(3);
executorService.submit(new Runnable() {
@Override
public void run() {
System.out.println("Task is running.");
}
});
}

/*
阻塞队列
*/
public void blockingDeque() {
Queue<Integer> blockingDeque = new ArrayBlockingQueue<>(20);
blockingDeque.add(1);
blockingDeque.add(2);
blockingDeque.add(3);

blockingDeque.peek();
}

public void concurrentHashMap() {
Map<String, Integer> concurrentHashMap = new ConcurrentHashMap<>();
concurrentHashMap.put("Hello", 1);
concurrentHashMap.put("World", 2);

System.out.println(concurrentHashMap.get("Hello"));
}

public void copyOnWriteList() {
List<String> copyOnWriteList = new CopyOnWriteArrayList<>();
copyOnWriteList.add("a");
copyOnWriteList.add("b");
copyOnWriteList.add("c");

System.out.println(copyOnWriteList.size());
}

public void semaphore() {
Semaphore semaphore = new Semaphore(3);
try {
semaphore.acquire();
System.out.println(Thread.currentThread().getName() + " is working");
Thread.sleep(1000);
semaphore.release();
System.out.println(Thread.currentThread().getName() + " is over");
} catch (InterruptedException e) {
}
}

}

3.枚举

1
2
3
4
5
6
7
8
9
10
11
/**
* 枚举
*
* @author GaoHangHang
* @date 2018/06/12 0:28
**/
public enum EnumDemo {

RED, GREEN, YELLOW

}

4.for each

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
import java.util.Arrays;
import java.util.List;

/**
* for each
*
* @author GaoHangHang
* @date 2018/06/12 0:30
**/
public class ForEach {

public static void main(String[] args) {

int[] arr = {1, 4, 5, 7};

for (int i : arr) {
System.out.println(i);
}

List<String> names = Arrays.asList("王爵nice", "Gay冰", "A*熊");
for (String name : names) {
System.out.println(name);
}
}

}

5.generic

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
import java.util.HashMap;
import java.util.Map;

/**
* 泛型
*
* 泛型规范数据结构,泛型的本质是参数化类型,所操作的数据被指定为一个参数
*
* @author GaoHangHang
* @date 2018/06/12 0:33
**/
public class Generic<T> {

public T getById() {
return null;
}

public static void main(String[] args) {

Map<String,Integer> map = new HashMap<>();

Generic<Long> generic = new Generic<>();

}
}

6.静态导入

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import static java.lang.System.out;

/**
* 静态导入
*
* @author GaoHangHang
* @date 2018/06/12 0:44
**/
public class StaticImport {

public static void main(String[] args) {
out.println("Hi let learn java 8.");
}

}

7.变长参数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import java.util.Arrays;
import java.util.List;

/**
* 变长参数
*
* @author GaoHangHang
* @date 2018/06/12 0:53
**/
public class VarArgs {

public static List<String> asList(String[] names){
return Arrays.asList(names);
}

public static void main(String[] args) {
List<String> hello = Arrays.asList("王爵nice", "Gay冰", "A*熊");

}

}
打赏
  • 版权声明: 本博客所有文章除特别声明外,著作权归作者所有。转载请注明出处!
  • Copyrights © 2015-2023 高行行
  • 访问人数: | 浏览次数:

请我喝杯咖啡吧~

支付宝
微信