[Java基础]Stream流综合练习
Posted Wecccccccc
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Java基础]Stream流综合练习相关的知识,希望对你有一定的参考价值。
代码如下:
package StreamDemoFinal;
public class Actor {
private String name;
public Actor(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package StreamDemoFinal;
import java.util.ArrayList;
import java.util.stream.Stream;
public class StreamTest {
public static void main(String[] args)
{
ArrayList<String> manList = new ArrayList<String>();
manList.add("周润发");
manList.add("成龙");
manList.add("刘德华");
manList.add("吴京");
manList.add("周星驰");
manList.add("李连杰");
ArrayList<String> womanList = new ArrayList<String>();
womanList.add("林心如");
womanList.add("张曼玉");
womanList.add("林青霞");
womanList.add("柳岩");
womanList.add("林志玲");
womanList.add("王祖贤");
//男演员只要名字为3个字的前3人
Stream<String> manStream = manList.stream().filter(s->s.length()==3).limit(3);
Stream<String> womanStream = womanList.stream().filter(s->s.startsWith("林")).skip(1);
Stream<String> stream = Stream.concat(manStream, womanStream);
// stream.map(Actor::new).forEach(System.out::println);
stream.map(Actor::new).forEach(p->System.out.println(p.getName()));
}
}
另外一种写法:
代码如下:
package StreamDemoFinal;
import java.util.ArrayList;
import java.util.stream.Stream;
public class StreamTest {
public static void main(String[] args)
{
ArrayList<String> manList = new ArrayList<String>();
manList.add("周润发");
manList.add("成龙");
manList.add("刘德华");
manList.add("吴京");
manList.add("周星驰");
manList.add("李连杰");
ArrayList<String> womanList = new ArrayList<String>();
womanList.add("林心如");
womanList.add("张曼玉");
womanList.add("林青霞");
womanList.add("柳岩");
womanList.add("林志玲");
womanList.add("王祖贤");
Stream.concat(manList.stream().filter(s->s.length()==3).limit(3),
womanList.stream().filter(s->s.startsWith("林")).skip(1)).map(Actor::new).forEach(p->System.out.println(p.getName()));
}
}
以上是关于[Java基础]Stream流综合练习的主要内容,如果未能解决你的问题,请参考以下文章