码迷,mamicode.com
首页 > 编程语言 > 详细

Java SE 8 流库

时间:2017-12-15 00:49:23      阅读:374      评论:0      收藏:0      [点我收藏+]

标签:计算   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 流库

标签:计算   dir   ack   attrs   rect   att   方法   复制文件   tar   

原文地址:http://www.cnblogs.com/qlwang/p/8040615.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!