①男演员只要名字为3个字的前两人
②女演员只要姓杨的,并且不要第一个
③把过滤后的男演员姓名和女演员姓名合并到一起
④遍历所有数据
1,一般写法:
public class Text02 {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
Collections.addAll(list, "迪丽热巴", "宋远桥", "苏星河", "老子", "庄子", "孙子", "洪七公", "乔大峰", "欧阳锋");
ArrayList<String> list1 = new ArrayList<>();
Collections.addAll(list1,"杨颖", "杨洋", "张三丰", "赵丽颖", "张二狗", "杨广武", "杨超越");
//男演员:
//过滤filter
Stream<String> stream = list.stream().filter(new Predicate<String>() {
@Override
public boolean test(String s) {
return s.length()==3;
}
}).limit(2);
//女演员:
//过滤filter
Stream<String> stream1 = list1.stream().filter(new Predicate<String>() {
@Override
public boolean test(String s) {
return s.startsWith("杨");
}
}).skip(2);
//合并两个流;并遍历输出
Stream.concat(stream, stream1).forEach(s -> System.out.println(s));
}
}
2,省略的链式编程写法:(Lambda)
public class Text02 {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
Collections.addAll(list, "迪丽热巴", "宋远桥", "苏星河", "老子", "庄子", "孙子", "洪七公", "乔大峰", "欧阳锋");
ArrayList<String> list1 = new ArrayList<>();
Collections.addAll(list1,"杨颖", "杨洋", "张三丰", "赵丽颖", "张二狗", "杨广武", "杨超越");
//男演员:
//过滤filter
Stream<String> stream1 = list.stream().filter(s -> s.length() == 3).limit(2);
//女演员:
//过滤filter
Stream<String> stream2 = list1.stream().filter(s -> s.startsWith("杨")).skip(2);
//合并两个流;并遍历输出
Stream.concat(stream1, stream2).forEach(s -> System.out.println(s));
}
}