Demo05.java 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package J20250724.demo02_function_program;
  2. import java.util.ArrayList;
  3. import java.util.stream.Stream;
  4. /**
  5. * @author WanJl
  6. * @version 1.0
  7. * @title Demo05
  8. * @description
  9. * @create 2025/7/24
  10. */
  11. public class Demo05 {
  12. public static void main(String[] args) {
  13. ArrayList<String> manList=new ArrayList<>();
  14. manList.add("周润发");
  15. manList.add("周星驰");
  16. manList.add("成龙");
  17. manList.add("周杰伦");
  18. manList.add("周杰");
  19. manList.add("林俊杰");
  20. ArrayList<String> womanList=new ArrayList<>();
  21. womanList.add("林青霞");
  22. womanList.add("张曼玉");
  23. womanList.add("王祖贤");
  24. womanList.add("林心如");
  25. womanList.add("蔡文姬");
  26. womanList.add("张韶涵");
  27. //男演员只要名字为3个字的前三人
  28. Stream<String> manStream = manList.stream().filter(s -> s.length() == 3).limit(3);
  29. //女演员只要姓林的,并且不要第1个
  30. Stream<String> womanStream = womanList.stream().filter(s -> s.startsWith("林")).skip(1);
  31. //把过滤后的男演员姓名和女演员姓名合并到一起
  32. Stream<String> actorStream = Stream.concat(manStream, womanStream);
  33. //把上一步操作后的元素作为构造方法的参数创建演员对象,遍历数据
  34. actorStream.forEach(name->{
  35. Actor actor=new Actor(name);
  36. System.out.println(actor);
  37. });
  38. }
  39. }