package com.hrf.streamAPI;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;
public class StreamAPI {
public static void main(String[] args) {
test01();
test02();
test03();
test04();
test05();
test06();
test07();
test08();
test09();
test091();
test10();
}
public static void test01() {
// 数据的切片
List<Person> list = Arrays.asList(new Person("张三", 12), new Person("李四", 12));
// 创建流
list.stream().filter(x -> x.getAge() > 10)// 过滤
.limit(3)// 获取指定数量的数据
.skip(1)// 跳过指定元素的个数
.distinct()// 去掉重复值
.forEach(el -> System.out.println(el.getName()));
}
public static void test02() {
// 使用map进行类型的转变
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6);
// 创建流
list.stream().map(x -> "hello" + x)// 转变类型
.forEach(System.out::println);
}
public static void test03() {
// 使用flatMap把每一个元素转为另一个流,然后合并一个流
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6);
// 创建流
list.stream().flatMap(StreamAPI::test031).forEach(System.out::println);
}
public static Stream<Integer> test031(int i) {
// 把元素转换成流
return Stream.of(i);
}
public static void test04() {
// 创建集合
List<Person> list = Arrays.asList(new Person("张三", 12), new Person("李四", 12));
// 创建流
list.stream()
.map(Person::getName)//转变类型
.forEach(System.out::println);
}
public static void test05() {
// 排序
// 创建集合
List<Integer> list = Arrays.asList(1, 2, 4, 3, 9, 5);
// 创建流
list.stream()
.sorted()
.forEach(System.out::println);
}
public static void test06() {
// 自定义排序
// 创建集合
List<Integer> list = Arrays.asList(1, 2, 4, 3, 9, 5);
// 创建流
list.stream()
.sorted((x,y)->y-x)
.forEach(System.out::println);
}
public static void test07() {
// 自定义排序按照年龄进行排序
// 创建集合
List<Person> list = Arrays.asList(new Person("张三", 12), new Person("李四", 11), new Person("村花", 14));
// 创建流
list.stream()
.sorted((x,y)->y.getAge()-x.getAge())
.forEach(System.out::println);
}
public static void test08() {
// 自定义排序按照年龄进行排序,如果年龄相同按照姓名进行排序
// 创建集合
List<Person> list = Arrays.asList(new Person("张三", 12), new Person("李四", 11),
new Person("村花", 14), new Person("八戒", 11));
// 创建流
list.stream()
.sorted((x,y)->{
if (x.getAge()==y.getAge()) {
return x.getName().compareTo(y.getName());
} else {
return x.getAge() - y.getAge();
}
})
.forEach(System.out::println);
}
public static void test09() {
// 获取年龄数据的count个数,max最大值,min最小值
// 创建集合
List<Person> list = Arrays.asList(new Person("张三", 12), new Person("李四", 11),
new Person("村花", 14), new Person("八戒", 20));
// 创建流
// Stream<Person> stream = list.stream();
System.out.println("数据的个数:" + list.stream().count());
// 最大值
Optional<Person> max = list.stream().max((x,y)->Integer.compare(x.getAge(), y.getAge()));
System.out.println("最大值:" + max.get().getAge());
// 最小值
Optional<Person> min = list.stream().min((x,y)->Integer.compare(x.getAge(), y.getAge()));
System.out.println("最小值:" + min.get().getAge());
}
public static void test091() {
// 另外一种方式获取年龄数据的count个数,max最大值,min最小值
// 创建集合
List<Person> list = Arrays.asList(new Person("张三", 12), new Person("李四", 11),
new Person("村花", 14), new Person("八戒", 20));
// 创建流
// Stream<Person> stream = list.stream();
System.out.println("数据的个数:" + list.stream().count());
// 最大值
Optional<Integer> max = list.stream()
.map(Person::getAge).max(Integer::compareTo);
System.out.println("最大值:" + max.get());
// 最小值
Optional<Integer> min = list.stream()
.map(Person::getAge).min(Integer::compareTo);
System.out.println("最小值:" + min.get());
}
public static void test10() {
// 比较数字
// 创建集合
List<Integer> list = Arrays.asList(1, 2, 4, 3, 9, 5);
//
System.out.println("max = " + list.stream().max(Integer::compareTo));
System.out.println("min = " + list.stream().min(Integer::compareTo));
}
}
class Person {
String name;
Integer age;
public Person() {
}
public Person(String name, Integer age) {
super();
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 String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
}
新特性 StreamAPI
最新推荐文章于 2024-03-26 15:12:09 发布