①男演员只要名字为3个字的前两人

②女演员只要姓杨的,并且不要第一个

③把过滤后的男演员姓名和女演员姓名合并到一起

④遍历所有数据

1,一般写法:

  1. public class Text02 {
  2. public static void main(String[] args) {
  3. ArrayList<String> list = new ArrayList<>();
  4. Collections.addAll(list, "迪丽热巴", "宋远桥", "苏星河", "老子", "庄子", "孙子", "洪七公", "乔大峰", "欧阳锋");
  5. ArrayList<String> list1 = new ArrayList<>();
  6. Collections.addAll(list1,"杨颖", "杨洋", "张三丰", "赵丽颖", "张二狗", "杨广武", "杨超越");
  7. //男演员:
  8. //过滤filter
  9. Stream<String> stream = list.stream().filter(new Predicate<String>() {
  10. @Override
  11. public boolean test(String s) {
  12. return s.length()==3;
  13. }
  14. }).limit(2);
  15. //女演员:
  16. //过滤filter
  17. Stream<String> stream1 = list1.stream().filter(new Predicate<String>() {
  18. @Override
  19. public boolean test(String s) {
  20. return s.startsWith("杨");
  21. }
  22. }).skip(2);
  23. //合并两个流;并遍历输出
  24. Stream.concat(stream, stream1).forEach(s -> System.out.println(s));
  25. }
  26. }

2,省略的链式编程写法:(Lambda)

  1. public class Text02 {
  2. public static void main(String[] args) {
  3. ArrayList<String> list = new ArrayList<>();
  4. Collections.addAll(list, "迪丽热巴", "宋远桥", "苏星河", "老子", "庄子", "孙子", "洪七公", "乔大峰", "欧阳锋");
  5. ArrayList<String> list1 = new ArrayList<>();
  6. Collections.addAll(list1,"杨颖", "杨洋", "张三丰", "赵丽颖", "张二狗", "杨广武", "杨超越");
  7. //男演员:
  8. //过滤filter
  9. Stream<String> stream1 = list.stream().filter(s -> s.length() == 3).limit(2);
  10. //女演员:
  11. //过滤filter
  12. Stream<String> stream2 = list1.stream().filter(s -> s.startsWith("杨")).skip(2);
  13. //合并两个流;并遍历输出
  14. Stream.concat(stream1, stream2).forEach(s -> System.out.println(s));
  15. }
  16. }