概述
此篇为实践操作
代码地址
https://github.com/viakiba/viakiba/tree/master/JavaStreamDemo
参考:
https://www.ibm.com/developerworks/cn/java/j-lo-java8streamapi/
http://javarevisited.blogspot.sg/2013/11/java-8-tutorials-resources-and-examples-lambda-expression-stream-api-functional-interfaces.html
http://www.drdobbs.com/jvm/lambdas-and-streams-in-java-8-libraries/240166818
接上篇
https://blog.viakiba.cn/2018/11/24/Java8-Stream-1/
流的操作
- Intermediate:
map (mapToInt, flatMap 等)、 filter、 distinct、 sorted、 peek、 limit、 skip、 parallel、 sequential、 unordered
- Terminal:
forEach、 forEachOrdered、 toArray、 reduce、 collect、 min、 max、 count、 anyMatch、 allMatch、 noneMatch、 findFirst、 findAny、 iterator
- Short-circuiting:
anyMatch、 allMatch、 noneMatch、 findFirst、 findAny、 limit
获取流的方式
从 Collection 和数组
/**
* 从 Collection 和数组
* @param:
* @return:
* @author: viakiba
* @date: 2018/12/25 16:53
*/
@Test
public void test1(){
List<Integer> integers = Arrays.asList(1, 2, 3);
// integers.stream()
// integers.parallelStream()
// Arrays.stream(new int[])
// Stream<Integer> integerStream = Stream.of(1, 2, 3, 4, 5);
// Stream<String> lines = BufferedReader.lines();
}
静态工厂
/**
* 静态工厂
* @param:
* @return:
* @author: viakiba
* @date: 2018/12/25 16:57
*/
@Test
public void test2(){
// IntStream range = IntStream.range();
// Stream<Path> walk = Files.walk();
}
自己构建
/**
* 自己构建
* @param:
* @return:
* @author: viakiba
* @date: 2018/12/25 16:59
*/
@Test
public void test3(){
// java.util.Spliterator
}
其他方式
/**
* 其他方式
* @param:
* @return:
* @author: viakiba
* @date: 2018/12/25 17:00
*/
@Test
public void test4(){
// IntStream ints = Random.ints();
// IntStream stream = BitSet.stream();
// Stream<JarEntry> stream = JarFile.stream();
}
处理流
在这里以从 Collecton 中获取流进行操作延是,详细见代码 附 JAVA 中的继承关系
skip / toArray
/**
* List ==> skip / toArray
*/
@Test(testName = "skip")
public void testCollection1(){
List<String> list = new ArrayList<String>();
list = Arrays.asList("a","b","c","d");
Stream<String> stream = list.stream();
//skip 操作 跳过指定索引前的内容
Object[] objects = stream.skip(2).toArray();
System.out.println(objects.length+""+objects[0] +""+objects[1]);
//Stream 不可重复使用 即 不可逆
// objects = stream.skip(2).toArray();
// System.out.println(objects.length+""+objects[0] +""+objects[1]);
}
filter / sorted 等
/**
* List ==> filter / sorted / min /max / toArray
*/
@Test(testName = "filter")
public void testCollection2(){
List<Integer> list = new ArrayList<Integer>();
list = Arrays.asList(1,2,8,6,3);
Integer[] integers = list.stream().filter(n -> n % 2 == 0).toArray(Integer[]::new);
System.out.println(integers.length+":"+integers[0] +""+integers[1]);
long count = list.stream().filter(x -> x / 2 == 1).count();
System.out.println(count);
List<Student> studentList = new ArrayList<Student>();
studentList.add(new Student("s1",1));
studentList.add(new Student("s3",3));
studentList.add(new Student("s2",2));
studentList.add(new Student("s5",5));
studentList.add(new Student("s4",4));
Student[] students = studentList.stream().filter(n -> n.getAge() < 2).toArray(Student[]::new);
System.out.println(students[0]);
for(Student s:students){
System.out.println(s);
}
System.out.println("=====================");
Student[] students1 = studentList.stream().sorted().toArray(Student[]::new);
for(Student s:students1){
System.out.println(s);
}
System.out.println("=====================");
Student students2 = studentList.stream().min(Student::compareTo).get();
System.out.println(students2);
System.out.println("=====================");
Student students3 = studentList.stream().min((x,y) -> {
if(x.getAge() > y.getAge()){
return 1;
}else if(x.getAge() == y.getAge()){
return 0;
}else {
return -1;
}
}).get();
System.out.println(students3);
}
略……….
见 https://github.com/viakiba/viakiba/blob/master/JavaStreamDemo/src/main/java/org/viakiba/stream/Stream1.java