javaBean
**
* @version 1.0
* @Author shenghao.xiao
* @Date 2019/9/19
**/
public class Student implements Comparable<Student> {
private String name;
private Integer age;
public Student(Integer age, String name) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public int compareTo(Student student) {
return this.age - student.getAge();
}
使用场景
public class StreamTest extends BaseTest {
final static List<Integer> dataList = new ArrayList<>();
final static List<Student> studentList = new ArrayList<>();
final static List<String> strList = new ArrayList<>();
{
dataList.add(1);
dataList.add(1);
dataList.add(3);
dataList.add(4);
dataList.add(2);
Student student1 = new Student(18, "jocy");
Student student2 = new Student(16, "cloudy");
Student student3 = new Student(22, "jack");
Student student4 = new Student(53, "tony");
studentList.add(student1);
studentList.add(student2);
studentList.add(student3);
studentList.add(student4);
strList.add("a");
strList.add("b");
strList.add("c");
strList.add("d");
}
@Test
public void test1() {
System.out.println(dataList.stream().count());
}
@Test
public void test2() {
dataList.stream().distinct().forEach(System.out::println);
}
@Test
public void test3() {
dataList.stream().filter(integer -> integer > 1).forEach(System.out::println);
}
@Test
public void test4() {
dataList.removeIf(integer -> integer <= 1);
dataList.forEach(System.out::println);
}
@Test
public void test5() {
Stream.concat(dataList.stream(), strList.stream()).forEach(System.out::println);
}
@Test
public void test6() {
strList.stream().map(String::toUpperCase).forEach(System.out::println);
}
@Test
public void test7() {
strList.stream().flatMap(item -> Stream.of(item.toUpperCase())).forEach(System.out::println);
}
@Test
public void test8() {
dataList.stream().skip(2).forEach(System.out::println);
}
@Test
public void test9() {
dataList.stream().sorted().forEach(System.out::println);
}
@Test
public void test10() {
dataList.stream().sorted(Comparator.reverseOrder()).forEach(System.out::println);
}
@Test
public void test11() {
studentList.stream().sorted(Comparator.comparing(Student::getAge)).forEach(student -> System.out.println(student.getName()));
}
@Test
public void test12() {
Optional<Integer> max = dataList.stream().max((o1, o2) -> o2 - o1);
System.out.println(max.get());
}
@Test
public void test13() {
Optional<Integer> min = dataList.stream().min((o1, o2) -> o2 - o1);
System.out.println(min.get());
}
@Test
public void test14() {
boolean match = dataList.stream().allMatch(integer -> integer > 2);
System.out.println(match);
}
@Test
public void test15() {
boolean match = dataList.stream().anyMatch(integer -> integer > 2);
System.out.println(match);
}
@Test
public void test16() {
Optional<Integer> any = dataList.stream().findAny();
System.out.println(any.get());
}
@Test
public void test17() {
Optional<Integer> any = dataList.stream().findFirst();
System.out.println(any.get());
}
@Test
public void test18() {
dataList.stream().limit(5).forEach(System.out::println);
}
@Test
public void test19() {
boolean match = dataList.stream().noneMatch(integer -> integer > 2);
System.out.println(match);
}
@Test
public void test20() {
String collect1 = studentList.stream().map(Student::getName).collect(Collectors.joining());
System.out.println(collect1);
String collect2 = studentList.stream().map(Student::getName).collect(Collectors.joining(","));
System.out.println(collect2);
studentList.stream().map(Student::getName).collect(Collectors.toList()).forEach(System.out::println);
studentList.stream().map(Student::getName).collect(Collectors.toSet()).forEach(System.out::println);
}
@Test
public void test21() {
int sum = studentList.stream().mapToInt(Student::getAge).sum();
System.out.println(sum);
}