创建测试类,在TestStream中测试Stream类
如何识别是中间操作,还是最终操作?
主要:就看方法有没返回值
测试一:(打印大小写,数据类型的转换)
@Test
void test1(){
List<String> list = Arrays.asList("MySQL", "Java", "HTML", "CSS", "JavaScript");
list.stream().forEach(System.out::println);
list.stream().map(String::toUpperCase).forEach(System.out::println);
list.stream().map(x->x.toUpperCase()).forEach(x->System.out.println(x));
//大写
线程: new Thread(new Runnable() {
@Override
public void run() {
System.out.println("我是最帅的!");
}
});
List<String> list1 = Arrays.asList("MySQL", "Java", "HTML", "CSS", "JavaScript");
list.stream().map(String::length).forEach(System.out::println);
//打印结果:MySQL Java HTML CSS JavaScript
list.stream().mapToInt(String::length).forEach(System.out::println);//5
list.stream().mapToDouble(x->x.codePointAt(0)).forEach(System.out::println);
floatMap与Map的区别:
flatMap会把每个元素映射成新的流
Map会把每个元素映射成新的元素
list.stream().flatMap(x-> Stream.of(x.split(""))).forEach(System.out::println);
// list.stream().mapToDouble()
}
新建女孩类,属性为:名字,性别,年龄
测试二:过滤filter(年龄大于20的如花)
@Test
void test2() {
ArrayList<Cirl>arrayList=new ArrayList<>();
Collections.addAll(arrayList,
new Cirl("如花1","女",16),
new Cirl("如花2","女",30),
new Cirl("如花3","女",49)
);
arrayList.stream().filter(x->x.getAge()<=20).forEach(System.out::println);
}
测试三:distinct(去重)
@Test
void test3() {
ArrayList<Cirl>arrayList=new ArrayList<>();
Collections.addAll(arrayList,
new Cirl("如花1","女",16),
new Cirl("如花2","女",30),
new Cirl("如花3","女",16),
new Cirl("如花4","女",30),
new Cirl("如花5","女",49)
);
arrayList.stream().distinct().forEach(System.out::println);
}
测试四:排序(年龄从小到大,从大到小排序)
(注意:排序涉及到比较,
//方式一:在类中重写equals和hashCode,
//方式二:在测试类中重写equals和hashCode.)
@Test
void test4() {
ArrayList<Cirl>arrayList=new ArrayList<>();
Collections.addAll(arrayList,
new Cirl("如花1","女",86),
new Cirl("如花2","女",30),
new Cirl("如花3","女",49)
);
arrayList.stream().sorted((x1,x2)->{
if (x1.getAge()>x2.getAge()){
return 1;
}else if (x1.getAge()<x2.getAge()){
return -1;
}else {
return 0;
}
}).forEach(System.out::println);
arrayList.stream().sorted((x1,x2)->{
return -Integer.compare(x1.getAge(), x2.getAge());
}).forEach(System.out::println);
arrayList.stream().sorted((x1,x2)-> -Integer.compare(x1.getAge(), x2.getAge())
).forEach(System.out::println);
// arrayList.stream().sorted().forEach(System.out::println);
}
测试五:限制 limit
@Test
void Test5() {
Integer[] numbers={12,34,55,34,12,34};
Arrays.stream(numbers).limit(3).forEach(System.out::println);
//获取三个整形随机数
Random random=new Random();
random.ints().limit(3).forEach(System.out::println);
//random.doubles()
//random.longs()
//在指定范围内获取随机数
random.ints(10,20).limit(5).forEach(System.out::println);
}
测试六:peek在遍历前执行
@Test
void test6() {
List<String>list=Arrays.asList("a1","a2","a3");
list.stream().peek(x-> System.out.println("辉哥最帅!")).forEach(System.out::println);
}
测试七:最大值.最小值
@Test
void test7() {
List<Integer>list=Arrays.asList(11,22,3,333);
//Optional是个容器,存放了结果数据
Optional<Integer>max=list.stream().max(Integer::compareTo);
//可以判断是否为空
max.ifPresent(System.out::println);
//获取容器的值
Integer integer=max.get();
System.out.println(integer);
}
测试八:(在女孩类中写一个吃的方法,把thing用 Optional容器包括起来)
类中:
public void eatPro(String thing){
//把thing用 Optional容器包括起来
Optional<String> thing1= Optional.ofNullable(thing);
thing1.ifPresent(x-> System.out.println("吃"+x));
Optional.ofNullable(thing).ifPresent(x-> System.out.println("吃"+x));
}
测试类中:
@Test
void test8() {
Cirl cirl=new Cirl();
cirl.eat("雪糕");
cirl.eat(null);
cirl.eatPro("雪糕");
cirl.eatPro(null);
}
测试九(Collectors转list,转换为指定集合)
@Test
void test9() {
List<Integer>list=Arrays.asList(11,22,3,333);
list.stream().map(x->x+10).forEach(System.out::println);
System.out.println(list);
List<Integer> list1 = list.stream().map(x -> x + 10).collect(Collectors.toList());
System.out.println(list1);
//转化为指定类型的集合
List<Integer> list2 = list.stream().map(x -> x + 10).collect(Collectors.toCollection(ArrayList::new));
System.out.println(list2);
}