package com.hanye.cn;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
public class TestClass {
/**
* 從迭代到流的操作
*/
public void test1() {
String contents = null;
//Read file into String
try {
contents = new String(Files.readAllBytes(Paths.get("alice.txt")),StandardCharsets.UTF_8);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
List<String> words =Arrays.asList(contents.split("\\PL+")); //非字母分割
/*long count = 0 ;
for(String w:words) {
if(w.length() > 12) {
count++;
}
}*/
long count = words.stream().filter(w -> w.length() > 12).count();
//以並行方式執行過濾和計數
long count2 =words.parallelStream().filter(w->w.length()>12).count();
}
}