不可变的集合长度
jdk 1.8 才有,这里演示不了。
1.知识点:map限制了长度10,但是调用copyof 方法可创建超过10的不可变集合。
2.数据流
toArray:里面需要规定数组类型 如 BBB.toArray(Int[] :: new ).
import java.util.*; import java.util.function.*; import java.util.stream.Collectors; public class ImutableArray { static int count = 0; public static void main(String[] args) { List<String> list = new ArrayList<>(); List<String> list2 = new ArrayList<>(); // list.addAll(list2) 方法不能添加数据,只能用来添加集合 Collections.addAll(list, "a", "b", "c", "d", "e", "f"); //不可变数组版本低了,这里就跳过了 //数据流代码 // 单列集合 list.stream().filter(new Predicate<String>() { @Override public boolean test(String s) { if (s.equals("a")) { return true; } return false; } }).forEach(new Consumer<String>() { @Override public void accept(String s) { System.out.println(s); } }); //双列集合 Map<String, String> map = new TreeMap<>(); map.put("张三", "zhangsan"); map.put("李四", "lisi"); map.put("王五", "wangwu"); map.put("张无忌", "zhangwuji"); Set<Map.Entry<String, String>> entries = map.entrySet(); entries.stream(). filter(stringStringEntry -> stringStringEntry.getKey().startsWith("张")). skip(1). forEach(stringStringEntry -> System.out.println(stringStringEntry)); //数组 //杂乱数据,这两个就不演示了 //切割数据 Set<String> jokers = new TreeSet<>(); jokers.add("凯撒.罗摩洛-男-57"); jokers.add("凯撒.陈方-未知-57"); jokers.add("杰克.-男-37"); jokers.add("希斯.莱杰-男-47"); jokers.add("陈放-男-23"); int[] nums = new int[10]; //splict 切割数据,成为数组 jokers.stream().distinct().forEach(new Consumer<String>() { @Override public void accept(String s) { String[] arr = s.split("-"); int age = Integer.parseInt(arr[2]); nums[count++] = age; return; } }); System.out.println(Arrays.toString(nums)); //统计长度 long jcount = jokers.stream().count(); System.out.println(jcount); //数据流转为数组 String[] strArr = jokers.stream().toArray(new IntFunction<String[]>() { //这里面的泛型要修该成String 类型的,为了记住,我这里就不改为lmabda表达式 @Override public String[] apply(int value) { return new String[value]; } }); System.out.println(Arrays.toString(strArr)); //Collect 集合之 toSet Set<String> setStr = jokers.stream().filter(new Predicate<String>() { @Override public boolean test(String s) { return s.startsWith("陈"); } }).collect(Collectors.toSet()); System.out.println(setStr); //Collect 集合之 toList List<String> newsetStr = jokers.stream().filter(new Predicate<String>() { @Override public boolean test(String s) { return s.split("-")[1].equals("男"); } }).collect(Collectors.toList()); System.out.println(newsetStr); //Collect 集合之 toMap Map<String, Integer> mapStr = jokers.stream().filter(new Predicate<String>() { @Override public boolean test(String s) { return s.split("-")[1].equals("男"); } }).collect(Collectors.toMap(new Function<String, String>() { @Override public String apply(String s) { return s.split("-")[0]; } }, new Function<String, Integer>() { @Override public Integer apply(String s) { return Integer.parseInt(s.split("-")[2]); } }) ); //collect转Map后 我好像输出不来 mapStr.entrySet().forEach(new Consumer<Map.Entry<String, Integer>>() { @Override public void accept(Map.Entry<String, Integer> stringIntegerEntry) { System.out.println(stringIntegerEntry); } }); } }
3.作业:数据流转自定义演员
System.out.println("------------------------------以下是作业---------------------------------------"); //现在有两个ArrayList集合, //第一个集合中:存储6名男演员的名字和年龄。第二个集合中:存储6名女演员的名字和年龄。姓名和年龄中间用逗号隔开。比如:张三,23 //要求完成如下的操作: //1,男演员只要名字为3个字的前两人2,女演员只要姓杨的,并且不要第一个 //3,把过滤后的男演员姓名和女演员姓名合并到一起4,将上一步的演员信息封装成Actor对象。 //5,将所有的演员对象都保存到List集合中。备注:演员类Actor,属性有:name,age List<String> nanAc = new ArrayList<>(); nanAc.add("张三,23"); nanAc.add("李四,24"); nanAc.add("王五,25"); nanAc.add("柴梦波,25"); nanAc.add("方锡源,25"); nanAc.add("徐天宝,25"); Stream<String> streamOne = nanAc.stream().filter(s -> s.split(",")[0].length() == 3).limit(2); List<String> nvAc = new ArrayList<>(); nvAc.add("杨点丰,23"); nvAc.add("杨故惠,24"); nvAc.add("杨菜鸡,24"); nvAc.add("张星,25"); Stream<String> streamTwo = nvAc.stream().filter(s -> s.startsWith("杨")).skip(1); Stream.concat(streamOne, streamTwo).map(new Function<String,Actor>() { //这里要自动补充,否则不会显示,所有的都是有参数的,只是自己没注意 @Override public Actor apply(String str) { return new Actor(str.split(",")[0],Integer.parseInt(str.split(",")[1])); } }).collect(Collectors.toList()).forEach(s-> System.out.println(s));
4.方法引用
方法引用
public static void main(String[] args) { List<String> list =new ArrayList<>(); list.add("1"); list.add("5"); list.add("4"); list.add("2"); list.add("3"); list.add("8"); list.add("9"); list.add("7"); list.add("10"); list.add("6"); //好笨... 大量操作集合应该用 collections Collections.addAll(list,"11"); list.stream().map(Integer::parseInt).forEach(System.out::print); }
调用方法的时候,需要自己在类里写一个方法。this和super不能再静态方法中使用。
//需求: //集合中有一些名字,按照要求过滤数据 //数据:"张无忌","周芷若”,"赵敏","张强","张三丰"要求:只要以张开头,而且名字是3个字的 List<String> listTwo = new ArrayList<>(); Collections.addAll(listTwo,"张哈哈","张无忌","张兮兮","张无敌","张开嘴","张三","张傻逼","张牛马"); listTwo.stream().filter(new FunctionQuote()::returnString).forEach(System.out::println); } public boolean returnString(String s){ boolean flag = s.startsWith("张")&&s.length()==3; return flag; } super和this暂时不写
主程序代码
//需求: //集合中有一些名字,按照要求过滤数据 //数据:"张无忌","周芷若”,"赵敏","张强","张三丰"要求:只要以张开头,而且名字是3个字的 List<String> listTwo = new ArrayList<>(); Collections.addAll(listTwo,"张哈哈","张无忌","张兮兮","张无敌","张开嘴","张三","张傻逼","张牛马"); listTwo.stream().filter(new FunctionQuote()::returnString).forEach(System.out::println); //集合里面存储姓名和年龄,比如:张无忌,15 //要求:将数据封装成Student对象并收集到List集合中 List<String> listThree = new ArrayList<>(); Collections.addAll(listThree,"张哈哈,12","张无忌,13","张兮兮,14","张无敌,15","张开嘴,16","张三,17","张傻逼,18","张牛马,19"); listThree.stream().map(Student::new).collect(Collectors.toList()).forEach(System.out::println); } 构造器代码 public Student(String s) { this.name = s.split(",")[0]; this.age = Integer.parseInt(s.split(",")[1]); }