package com.example.testutils.service.impl;
import com.example.testutils.domain.LicensePlateInfo;
import com.example.testutils.domain.Person;
import org.apache.commons.compress.utils.Lists;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class DeclarativeFunctionTest {
public List<Person> infos;
public List<Integer> numberList;
@BeforeEach
void init() {
infos = new ArrayList<>();
numberList = new ArrayList<>();
infos.add(new Person("lily", 18));
infos.add(new Person("nick", 20));
infos.add(new Person("bob", 22));
numberList.add(2);
numberList.add(24);
numberList.add(21);
numberList.add(21);
numberList.add(200);
}
@Test
void testDeclarative() {
infos.stream().limit(2).forEach(System.out::println);
List<Person> personList = infos.stream()
.filter(person -> person.getAge() > 18).collect(Collectors.toList());
personList.forEach(person -> System.out.println(person));
}
@Test
void rangeIterate() {
IntStream.range(0, infos.size() - 1).
forEach(index -> {
Person p = infos.get(index);
System.out.println(p);
});
IntStream.rangeClosed(0, infos.size() - 1).forEach(index -> {
System.out.println(infos.get(index));
});
IntStream.iterate(0, operand -> operand + 1).limit(10)
.forEach(System.out::println);
}
@Test
void MinOrMax() {
System.out.println(numberList.stream().min(Integer::compare).get());
System.out.println(numberList.stream().min(Comparator.reverseOrder()).get());
}
@Test
void distinct() {
List<Integer> collect = numberList.stream().distinct()
.sorted(Comparator.reverseOrder())
.collect(Collectors.toList());
collect.forEach(System.out::println);
}
@Test
void filterPredicate() {
Predicate<Person> personPredicate = person -> person.getAge() > 18;
infos.stream()
.filter(personPredicate)
.collect(Collectors.toList())
.forEach(System.out::println);
}
@Test
void reduce() {
String[] strs = {"m", "y", "name", "is"};
List<ArrayList<String>> names = Lists.newArrayList();
ArrayList<String> subname = new ArrayList<>();
subname.add("nick");
names.add(subname);
System.out.println(Arrays.stream(strs).reduce("", (a, b) -> a + b));
System.out.println(Arrays.stream(strs).reduce("", String::concat));
}
@Test
void joinWithStream() {
String[] strs = {"my", "first", "name", "is"};
String join = "";
for (String s : strs) {
join = join + s + ", ";
}
System.out.println(Arrays.stream(strs)
.map(String::toUpperCase)
.collect(Collectors.joining(",")));
}
@Test
void understandCollect() {
List<Integer> nameCollect = infos.stream()
.map(Person::getAge)
.collect(Collectors.toList());
nameCollect.forEach(System.out::println);
infos.stream()
.map(Person::getAge)
.collect(() -> new ArrayList<Integer>(),
(list, element) -> list.add(element),
(list1, list2) -> list1.addAll(list2))
.forEach(System.out::println);
infos.stream()
.map(Person::getAge)
.collect(ArrayList::new,
ArrayList::add,
ArrayList::addAll)
.forEach(System.out::println);
}
@Test
void intermediateAndTerminalOperation() {
System.out.println(infos.stream()
.filter(person -> {
System.out.println(person);
return person.getAge() <= 18;
})
.map(person -> {
System.out.println("mapping person " + person);
return person.getAge();
})
.map(age -> {
System.out.println("mapping age " + age );
return age + 1;
})
.collect(Collectors.toList())
);
}
}
函数式编程视频教程