Java SE 8 流庫

來源:互聯網
上載者:User

標籤:計算   dir   ack   attrs   rect   att   方法   複製檔案   tar   

1. 流的作用

通過使用流,說明想要完成什麼任務,而不是說明如何去實現它,將操作的調度留給具體實現去解決;

執行個體:假如我們想要計算某個屬性的平均值,那麼我們就可以指定資料來源和屬性,然後,流庫就可以對計算進行最佳化;

1.1. 從迭代到流的操作1.1.1. java.nio.file深度剖析

從java.nio.file提供的功能不難看出已經可以替換java.io.file所提供的功能;

1.1.1.1. java.nio.file的主要功能

1:對檔案系統本身的操作,例如檔案的複製,移除,刪除,建立功能,建立軟串連。

2:對檔案系統的屬性的進行操作,例如查看或修改 檔案屬性、操作許可權、所屬使用者或使用者組、最後修改時間,查看檔案是否隱藏、檔案的長度。

3:對檔案系統進行遍曆。

4:使用nio的方式查看和改變檔案內容。

5:對檔案或檔案夾的建立,刪除,修改事件進行監控。

1.1.1.2. java.nio.file提供常用方法

1:複製檔案

copy(Path source,Path target,CopyOption... options) throws IOException

2:建立目錄

createDirectories(Path dir,FileAttribute<?>... attrs) throws IOException

3:建立檔案,path代表檔案路徑

createFile(Path path,FileAttribute<?>... attrs) throws IOException

4:建立串連,link代表目標串連,existing代表一個存在的檔案

createLink(Path link,Path existing)throws IOException

5:刪除檔案

delete(Path path); deleteIfExists(Path path)

6:擷取檔案的BufferReader,BufferWriter

newBufferedReader(Path path, Charset cs), newBufferedWriter(Path path, Charset cs, OpenOption... options)

7:擷取檔案的InputStream,OutputStream

newInputStream(Path path, OpenOption... options),newOutputStream(Path path, OpenOption... options)

8:以位元組和字串形式讀取檔案

readAllBytes(Path path),readAllLines(Path path, Charset cs)

1.1.2. 執行個體

需求:對文檔中的長單詞進行計數

1.1.3. 傳統方法
 1 import java.nio.charset.StandardCharsets; 2 import java.nio.file.Files; 3 import java.nio.file.Paths; 4 import java.util.Arrays; 5 import java.util.List; 6  7 /** 8  * Created by Lenovo on 2017/12/14. 9  * 對檔案中的長單詞進行計數10  */11 public class Demo01 {12 13     private static final String filePath = "G:\\Idea\\src\\com\\itheima05\\Test_JavaSE\\Test_20171214\\word.txt";14 15     public static void main(String[] args) throws Exception {16 17         //使用集合的方法實現18         String contens = new String(Files.readAllBytes(Paths.get(filePath)), StandardCharsets.UTF_8);19         String[] ws = contens.split("\\PL+");20         //將數組轉化為List集合21         List<String> words = Arrays.asList(ws);22         int count = 0;23         for(String word:words){24             if(word.length()>6){25                 count ++;26             }27         }28         System.out.println(count);29     }30 }
1.1.4. 使用流處理

java.util.Collection<E>:

default Stream<E> stream()   -----  產生當前集合中所有元素的順序流

default Stream<E> parallelStream()   -----  產生當前集合中所有元素的並行流

 1 import java.nio.charset.StandardCharsets; 2 import java.nio.file.Files; 3 import java.nio.file.Paths; 4 import java.util.Arrays; 5 import java.util.List; 6  7 /** 8  * Created by Lenovo on 2017/12/14. 9  * 使用流對文檔中的長單詞進行計數10  *11  */12 public class Demo02 {13 14     private static final String filePath = "G:\\Idea\\src\\com\\itheima05\\Test_JavaSE\\Test_20171214\\word.txt";15 16     public static void main(String[] args) throws Exception {17 18         String contents = new String(Files.readAllBytes(Paths.get(filePath)), StandardCharsets.UTF_8);19 20         String[] ws = contents.split("\\PL+");21         //將數組轉化為集合22         List<String> words = Arrays.asList(ws);23         //使用流24         //Stream<T> filter(Predicate<? super T> predicate) 產生一個流,其中包含當前流中滿足P的所有元素25         //long count()   產生當前流中元素的數量,這個是一個終止操作26         long count = words.stream()27                 .filter(w -> w.length() > 6)28                 .count();29         System.out.println("順序流輸出:"+count);30 31         long count02 = words.parallelStream()32                 .filter(w -> w.length()>6)33                 .count();34         System.out.println("並行流輸出:"+count02);35 36     }37 }

流的主要思想是:做什麼而非怎麼做;

以上執行個體:需要統計文檔中的長度為6的單詞

1.1.5. 流和集合的區別

1:流並不儲存其元素;

2:流的操作不會修改其資料來源

3:流的操作是儘可能惰性執行的

1.1.6. 流的操作流程

1:建立一個流

2:指定將初始流轉化為其他流的中間操作,可能包含多個步驟(filter,產生新的流);

3:應用終止操作,從而產生結果

Java SE 8 流庫

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.