1.java8中Optional的使用
/**
* 學習Java8中的Optional使用方法
*/
public class OptionalTest {
@Test
public void test() throws Throwable {
/**
* 三種創建Optional對象方式
*/
// 創建空的Optional對象
Optional.empty();
// 使用非null值創建Optional對象
Optional.of("zhangxiaoxi");
// 使用任意值創建Optional對象
Optional optional = Optional.ofNullable("zhangxiaoxi");
/**
* 判斷是否引用缺失的方法(建議不直接使用)
*/
optional.isPresent();
/**
* 當optional引用存在時執行
* 類似的方法:map filter flatMap
*/
optional.ifPresent(System.out::println);
/**
* 當optional引用缺失時執行
*/
optional.orElse("引用缺失");
optional.orElseGet(() -> {
// 自定義引用缺失時的返回值
return "自定義引用缺失";
});
optional.orElseThrow(() -> {
throw new RuntimeException("引用缺失異常");
});
}
public static void stream(List<String> list) {
// list.stream().forEach(System.out::println);
Optional.ofNullable(list)
.map(List::stream)
.orElseGet(Stream::empty)
.forEach(System.out::println);
}
public static void main(String[] args) {
stream(null);
}
}
2.不可變集合的使用
/**
* 不可變集合用法
*/
public class ImmutableTest {
public static void test(List<Integer> list) {
list.remove(0);
}
public static void main(String[] args) {
List<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(2);
list.add(3);
List<Integer> newList =
Collections.unmodifiableList(list);
test(newList);
System.out.println(newList);
}
public void immutable() {
List<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(2);
list.add(3);
/**
* 構造不可變集合對象三種方式
*/
// 通過已經存在的集合創建
ImmutableSet.copyOf(list);
// 通過初始值,直接創建不可變集合
ImmutableSet immutableSet =
ImmutableSet.of(1, 2, 3);
// 以builder方式創建
ImmutableSet.builder()
.add(1)
.addAll(Sets.newHashSet(2, 3))
.add(4)
.build();
}
}
3.新集合類型
Multiset
/**
* 實現:使用Multiset統計一首古詩的文字出現頻率
*/
public class MultisetTest {
private static final String text =
"《南陵別兒童入京》" +
"白酒新熟山中歸,黃雞啄黍秋正肥。" +
"呼童烹雞酌白酒,兒女嬉笑牽人衣。" +
"高歌取醉欲自慰,起舞落日爭光輝。" +
"遊說萬乘苦不早,著鞭跨馬涉遠道。" +
"會稽愚婦輕買臣,餘亦辭家西入秦。" +
"仰天大笑出門去,我輩豈是蓬蒿人。";
@Test
public void handle() {
// multiset創建
Multiset<Character> multiset =
HashMultiset.create();
// string 轉換成 char 數組
char[] chars = text.toCharArray();
// 遍歷數組,添加到multiset中
Chars.asList(chars)
.stream()
.forEach(charItem -> {
multiset.add(charItem);
});
System.out.println
("size : " + multiset.size());
System.out.println
("count : " + multiset.count('人'));
}
}
4.常用集合類型功能增強
/**
* Lists / Sets 使用
*/
public class SetsTest {
/**
* Sets工具類的常用方法
* 並集 / 交集 / 差集 / 分解集合中的所有子集 / 求兩個集合的笛卡爾積
*
* Lists工具類的常用方式
* 反轉 / 拆分
*/
private static final Set set1 =
Sets.newHashSet(1, 2);
private static final Set set2 =
Sets.newHashSet(4);
// 並集
@Test
public void union() {
Set<Integer> set = Sets.union(set1, set2);
System.out.println(set);
}
// 交集
@Test
public void intersection() {
Set<Integer> set = Sets.intersection(set1, set2);
System.out.println(set);
}
// 差集:如果元素屬於A而且不屬於B
@Test
public void difference() {
Set<Integer> set = Sets.difference(set1, set2);
System.out.println(set);
// 相對差集:屬於A而且不屬於B 或者 屬於B而且不屬於A
set = Sets.symmetricDifference(set1, set2);
System.out.println(set);
}
// 拆分所有子集合
@Test
public void powerSet() {
Set<Set<Integer>> powerSet = Sets.powerSet(set1);
System.out.println(JSON.toJSONString(powerSet));
}
// 計算兩個集合笛卡爾積
@Test
public void cartesianProduct() {
Set<List<Integer>> product =
Sets.cartesianProduct(set1, set2);
System.out.println(JSON.toJSONString(product));
}
/**
* 拆分
*/
@Test
public void partition() {
List<Integer> list =
Lists.newArrayList(1, 2, 3, 4, 5, 6, 7);
List<List<Integer>> partition =
Lists.partition(list, 3);
System.out.println(JSON.toJSONString(partition));
}
// 反轉
@Test
public void reverse() {
List<Integer> list = Lists.newLinkedList();
list.add(1);
list.add(2);
list.add(3);
List<Integer> newList = Lists.reverse(list);
System.out.println(newList);
}
}
5.對IO的支持
對源(Source)和匯(Sink)的抽象
源是可讀的:ByteSource/CharSource
匯是可寫的:ByteSink/CharSink
/**
* 演示如何使用流(Source)與匯(Sink)來對文件進行常用操作
*/
public class IOTest {
@Test
public void copyFile() throws IOException {
/**
* 創建對應的Source和Sink
*/
CharSource charSource = Files.asCharSource(
new File("SourceText.txt"),
Charsets.UTF_8);
CharSink charSink = Files.asCharSink(
new File("TargetText.txt"),
Charsets.UTF_8);
/**
* 拷貝
*/
charSource.copyTo(charSink);
}
}