简单示例
package com.grandsun.wxpay.java8NewFeature;
import org.junit.Test;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
public class StreamTest {
public Stream<String> getStreamData(){
String [] strArray = new String[] {"1", "2", "3"};
List<String> dataList = Arrays.asList(strArray);
Stream<String> stream = dataList.stream();
return stream;
}
public Stream<String> getStreamW(){
String [] strArray = new String[] {"a", "b", "c","d","e"};
List<String> dataList = Arrays.asList(strArray);
Stream<String> stream = dataList.stream();
return stream;
}
public class Person{
private int no;
private String name;
private String sex;
private Integer age;
public Person(){
}
public Person(Integer no, String name){
this.no = no;
this.name = name;
}
public Person(Integer no, String name, Integer age){
this.no = no;
this.name = name;
this.age = age;
}
public Person(String name, String sex, String gender){
this.name = name;
this.sex = sex;
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Person{" +
"no=" + no +
", name='" + name + '\'' +
", sex='" + sex + '\'' +
'}';
}
}
@Test
public void demo1(){
Stream stream = Stream.of("a", "b", "c");
String [] strArray = new String[] {"a", "b", "c"};
stream = Stream.of(strArray);
stream = Arrays.stream(strArray);
List<String> list2 = Arrays.asList(strArray);
stream = list2.stream();
}
@Test
public void demo2(){
IntStream.of(new int[]{1, 2, 3}).forEach(System.out::println);
IntStream.range(1, 30).forEach(System.out::println);
IntStream.rangeClosed(1, 13).forEach(System.out::println);
}
@Test
public void demo3(){
Stream<String> stream = getStreamData();
String[] strArray1 = stream.toArray(String[]::new);
stream = getStreamData();
List<String> list1 = stream.collect(Collectors.toList());
stream = getStreamData();
List<String> list2 = stream.collect(Collectors.toCollection(ArrayList::new));
stream = getStreamData();
Set set1 = stream.collect(Collectors.toSet());
stream = getStreamData();
Stack stack1 = stream.collect(Collectors.toCollection(Stack::new));
stream = getStreamData();
String str = stream.collect(Collectors.joining()).toString();
}
@Test
public void demo4(){
List<String> output = getStreamW()
.map(String::toUpperCase)
.collect(Collectors.toList());
System.out.println(output);
}
@Test
public void demo5(){
List<Integer> nums = Arrays.asList(1, 2, 3, 4);
List<Integer> squareNums = nums.stream().
map(n -> n * n).
collect(Collectors.toList());
System.out.println(squareNums);
}
@Test
public void demo6(){
Stream<List<Integer>> inputStream = Stream.of(
Arrays.asList(1),
Arrays.asList(2, 3),
Arrays.asList(4, 5, 6)
);
Stream<Integer> outputStream = inputStream.
flatMap((childList) -> childList.stream());
outputStream.forEach(System.out::println);
}
@Test
public void demo7(){
Integer[] sixNums = {1, 2, 3, 4, 5, 6};
Integer[] evens =
Stream.of(sixNums).filter(n -> n%2 == 0).toArray(Integer[]::new);
Arrays.stream(evens).forEach(System.out::println);
}
@Test
public void demo8() {
String sentence = "if you are luck you can catch a glimpse!" ;
String[] sentences = {"if you are luck you can catch a glimpse!", "hi, how are you!", "same as always"};
Stream<String> stream = Stream.of(sentence);
}
@Test
public void demo9(){
List<Person> personList = new ArrayList<>();
personList.add(new Person("张三", "男", ""));
personList.add(new Person("小花", "女", ""));
personList.add(new Person("王五", "男", ""));
personList.stream()
.filter(p -> p.getSex() == "男")
.forEach(p -> System.out.println(p.getName()));
Object[] array = Stream.of(1, 2, 3, 11, 22, 33, 99, -1, -2, -3)
.toArray();
Stream.of(array)
.forEachOrdered(System.out::println);
for (Person p : personList) {
if (p.getSex() == "女") {
System.out.println(p.getName());
}
}
}
@Test
public void demo10(){
Stream<String> stream = getStreamData();
stream.forEach(element -> System.out.println());
stream.forEach(element -> System.out.println());
}
@Test
public void demo11(){
Stream.of("one", "two", "three", "four")
.filter(e -> e.length() > 3)
.peek(e -> System.out.println("Filtered value: " + e))
.map(String::toUpperCase)
.peek(e -> System.out.println("Mapped value: " + e))
.collect(Collectors.toList());
}
@Test
public void demo12(){
String strA = " abcd ", strB = null;
print(strA);
print("");
print(strB);
getLength(strA);
getLength("");
getLength(strB);
}
public static void print(String text) {
Optional.ofNullable(text).ifPresent(System.out::println);
Optional.ofNullable(new String());
}
public static void getLength(String text) {
System.out.println(Optional.ofNullable(text).map(String::length).orElse(-1));
}
@Test
public void demo13(){
String concat = Stream.of("A", "B", "C", "D").reduce("", String::concat);
System.out.println(concat);
double minValue = Stream.of(-1.5, 1.0, -3.0, -2.0).reduce(Double.MAX_VALUE, Double::min);
System.out.println(minValue);
int sumValue = Stream.of(1, 2, 3, 4).reduce(0, Integer::sum);
System.out.println(sumValue);
sumValue = Stream.of(1, 2, 3, 4).reduce(Integer::sum).get();
System.out.println(sumValue);
concat = Stream.of("a", "B", "c", "D", "e", "F").
filter(x -> x.compareTo("Z") > 0).
reduce("", String::concat);
System.out.println(concat);
}
@Test
public void demo14(){
List<Person> persons = new ArrayList();
for (int i = 1; i <= 10000; i++) {
Person person = new Person(i, "name" + i);
persons.add(person);
}
List<String> personList2 = persons.stream().
map(Person::getName).limit(10).skip(3).collect(Collectors.toList());
System.out.println(personList2);
}
@Test
public void demo15(){
List<Person> persons = new ArrayList();
for (int i = 1; i <= 5; i++) {
Person person = new Person(i, "name" + i);
persons.add(person);
}
List<Person> personList2 = persons.stream()
.limit(2)
.sorted((p1, p2) -> p1.getName().compareTo(p2.getName()))
.collect(Collectors.toList());
System.out.println(personList2);
}
@Test
public void demo16() throws IOException {
BufferedReader br = new BufferedReader(new FileReader("/Users/admin/Desktop/gs_project/kuais_health/log/cleer.2020-09-05.0.log"));
int longest = br.lines().
mapToInt(String::length).
max().
getAsInt();
br.close();
System.out.println(longest);
}
@Test
public void demo17() throws IOException {
BufferedReader br = new BufferedReader(new FileReader("/Users/admin/Desktop/gs_project/kuais_health/sql/kuais_health.lock.md"));
List<String> words = br.lines().
flatMap(line -> Stream.of(line.split(" "))).
filter(word -> word.length() > 0).
map(String::toLowerCase).
distinct().
sorted().
collect(Collectors.toList());
br.close();
System.out.println(words);
}
@Test
public void demo18(){
List<Person> persons = new ArrayList();
persons.add(new Person(1, "name" + 1, 10));
persons.add(new Person(2, "name" + 2, 21));
persons.add(new Person(3, "name" + 3, 34));
persons.add(new Person(4, "name" + 4, 6));
persons.add(new Person(5, "name" + 5, 55));
boolean isAllAdult = persons.stream().
allMatch(p -> p.getAge() > 18);
System.out.println("All are adult? " + isAllAdult);
boolean isThereAnyChild = persons.stream().
anyMatch(p -> p.getAge() < 12);
System.out.println("Any child? " + isThereAnyChild);
boolean isThereNo = persons.stream().
noneMatch(p -> p.getAge() > 80);
System.out.println("Any 80? " + isThereNo);
}
@Test
public void demo19(){
Stream.generate(()->{
double random = Math.random();
DecimalFormat df = new DecimalFormat("0.00");
random = Double.parseDouble(df.format(random));
return random;
}).limit(5).forEach(System.out::println);
Stream.generate(Math::random).limit(5).forEach(System.out::println);
}
}