JAVASE8的流庫 1.1-從迭代到流的操作

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();
    
        
        
        
    }
}
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章