1.1.4. 使用流处理
java.util.Collection:
default Stream stream() ----- 产生当前集合中所有元素的顺序流
default Stream parallelStream() ----- 产生当前集合中所有元素的并行流
1 importjava.nio.charset.StandardCharsets;2 importjava.nio.file.Files;3 importjava.nio.file.Paths;4 importjava.util.Arrays;5 importjava.util.List;6
7 /**
8 * Created by Lenovo on 2017/12/14.9 * 使用流对文档中的长单词进行计数10 *11 */
12 public classDemo02 {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) throwsException {17
18 String contents = newString(Files.readAllBytes(Paths.get(filePath)), StandardCharsets.UTF_8);19
20 String[] ws = contents.split("\\PL+");21 //将数组转化为集合
22 List words =Arrays.asList(ws);23 //使用流24 //Stream 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:应用终止操作,从而产生结果
1.2. 流的创建
static Stream of(T... values) 返回一个元素为给定值的流
static Stream empty() 返回一个不包含任何元素的流
Optional findFirst() 返回描述此流的第一个元素的可选项,如果流为空,则返回一个空的可选项。 如果流没有遇到命令,则可以返回任何元素。
static Stream generate(Supplier s) 返回无限顺序无序流,其中每个元素由提供的供应商生成。 这适用于生成恒定流,随机元素流等。
static Stream iterate(T seed,UnaryOperator f) 返回一个由函数f迭代应用到初始元素种子产生的无限序列有序流,产生由种子,f(种子),f(f(种子))等组成的流。Stream中的第一个元素(位置0)将是提供的种子。 对于n> 0,位置n处的元素将是对位置n-1处的元素应用函数f的结果。
java.util.regex.Pattern
public static Pattern compile(String regex) 将给定的正则表达式编译成模式。
public Stream splitAsStream(CharSequence input) 根据给定的输入序列,围绕此模式的匹配创建一个流。
Java.util.File.Files
public static Stream lines(Path path,Charset cs) throws IOException 从文件中读取所有行作为Stream。 与readAllLines不同,此方法不会将所有行读取到List中,而是在流被消耗时延迟地填充。
1 /**
2 * Created by Lenovo on 2017/12/16.3 * static Stream of(T... values)4 * 产生一个元素为给定值的流5 */
6 public classDemo03 {7
8 private static final String filePath = "G:\\Idea\\src\\com\\itheima05\\Test_JavaSE\\Test_20171214\\word.txt";9
10 public static void main(String[] args) throwsException {11
12 String contents = newString(Files.readAllBytes(Paths.get(filePath)), StandardCharsets.UTF_8);13 //创建流(传入一个数组)
14 Stream words = Stream.of(contents.split("\\PL+"));15 long count = words.filter(w -> w.length() > 6)16 .count();17 System.out.println(count);18
19 //of方法具有可变长参数
20 Stream song = Stream.of("gently", "down", "the", "stream");21 //取流中的第一个元素
22 String word =song.findFirst().get();23 System.out.println(word);24
25 //创建不包含任何元素的流
26 Stream empty =Stream.empty();27 List list = empty.limit(1).collect(Collectors.toList());28 if(list.isEmpty()) {29 System.out.println("list为空");30 }31
32 //生成恒定流
33 Stream sg = Stream.generate(() -> "Echo");34 String s =sg.findFirst().get();35 System.out.println("恒定流:"+s);36
37 //生成一个随机流
38 Stream sgr =Stream.generate(Math::random);39 Double dr =sgr.findFirst().get();40 System.out.println("随机流"+dr);41
42 //产生无限序列
43 final int SIZE = 10;44 Stream si = Stream.iterate(BigInteger.ZERO, n ->n.add(BigInteger.ONE));45 List NumList = si.limit(SIZE + 1).collect(Collectors.toList());46 for(int i = 0;i
1.2.1. 实例
1.2.1.1. 问题1
public void show(T t),void前面的泛型T是什么作用
public 这个T是个修饰符的功能,表示是个泛型方法,就像有static修饰的方法是个静态方法一样。
不是返回值,表示传入参数有泛型
public static list aslist(T...a)
第一个表示是泛型方法,第二个表示返回值是list类型,而这个list有泛型,只能存t类型的数据
1.2.1.1. 具体实例
1 public classDem04 {2 private static final String filePath = "G:\\Idea\\src\\com\\itheima05\\Test_JavaSE\\Test_20171214\\word.txt";3 public static void show(String title,Streamstream){4
5 final int SIZE = 10;6 List tList = stream.limit(SIZE + 1)7 .collect(Collectors.toList());8 System.out.print(title+":");9 for(int i=0;i0){11 System.out.print(",");12 }13 if(i
23 String contents = newString(Files.readAllBytes(Paths.get(filePath)), StandardCharsets.UTF_8);24
25 Stream words = Stream.of(contents.split("\\PL+"));26 show("words",words);27
28 Stream song = Stream.of("gently", "down", "the", "stream");29 show("song",song);30
31 Stream silence =Stream.empty();32 show("silence",silence);33
34 Stream generate = Stream.generate(() -> "Eche");35 show("generate",generate);36
37 Stream randomNum =Stream.generate(Math::random);38 show("randomNum",randomNum);39
40 Stream iterate = Stream.iterate(BigInteger.ZERO, n ->n.add(BigInteger.ONE));41 show("iterate",iterate);42
43 Stream ss = Pattern.compile("\\PL+").splitAsStream(contents);44 show("ss",ss);45
46 Stream linesStream =Files.lines(Paths.get(filePath), StandardCharsets.UTF_8);47 show("linesStream",linesStream);48 }49 }
输出结果:
1 words:In,my,dual,profession,as,an,educator,and,health,care,.....2 song:gently,down,the,stream3 silence:4 generate:Eche,Eche,Eche,Eche,Eche,Eche,Eche,Eche,Eche,Eche,.....5 randomNum:0.8545074280087089,0.05768740015609908,0.34845089835617316,0.9804483156381134,0.7893443687111327,0.5534594948929666,0.10749904759731743,0.32867498853912414,0.6739155442072872,0.6897019997934322,.....6 iterate:0,1,2,3,4,5,6,7,8,9,.....7 ss:In,my,dual,profession,as,an,educator,and,health,care,.....8 linesStream:In my dual profession as an educator and health care provider,,I have worked with numerous children infected with the virus,that causes AIDS. The relationships that I have had with these,special kids have been gifts in my life. They have taught me so,many things, but I have especially learned that great courage,can be found in the smallest of packages. Let me tell you about Tyler.