目录
获取Stream流
/**
* filter过滤:你设置的是什么条件 满足的留下 不满足的过滤
* startsWith开头:满足开头条件的
* @param args
*/
public static void main(String[] args) {
List<String> list1 = new ArrayList<>();
list1.add("张无忌");
list1.add("周芷若");
list1.add("赵敏");
list1.add("张强");
list1.add("张三丰");
list1.stream().filter(name -> name.startsWith("张")).filter(name -> name.length() == 3).forEach(name -> System.out.println(name));
System.out.println("======================================");
/**
* 单列集合遍历 forEach
*/
List<String>list = new ArrayList<>();
Collections.addAll(list,"a","b","c","d");
list.stream().forEach(x -> System.out.println(x));
System.out.println("======================================");
/**
* 双列集合遍历需要先转成单列集合才可操作不能直接使用Stream
* 1.keySet().stream()获取键值数据
* 2.entrySet().stream()获取键值对数据
*/
Map<String,Integer> map = new HashMap<>();
map.put("aaa",111);
map.put("bbb",222);
map.put("ccc",333);
map.put("ddd",444);
//第一种方法获取键的数据
map.keySet().stream().forEach(x -> System.out.println(x));
//第二种方法获取键值对数据
map.entrySet().stream().forEach(x -> System.out.println(x));
System.out.println("==========================================");
/**
* 数组通过Arrays.stream来操作
*/
int[] a = {1,2,3};
Arrays.stream(a).forEach(x -> System.out.println(x));
String [] a1 = {"aa","bb","cc"};
Arrays.stream(a1).forEach(x -> System.out.println(x));
System.out.println("===========================================");
/**
* 零散数据获取Stream流
*/
Stream.of(1,2,3,1).forEach(x -> System.out.println(x));
Stream流中间方法
/**
* @author admin
* @since 2023/8/8 23:40
*/
public class StreamDemo2 {
public static void main(String[] args) {
/**
* limit:想要前几个就写几个
* skip:不想要前几个就写几个
* distinct 过滤
* Stream.concat 合并两个流为一个流
* map类型转换
* split分割 从那个开始分割 然后就是从0开始的索引
*/
ArrayList<String> list = new ArrayList<>();
Collections.addAll(list,"张无忌","周芷若","赵敏","张强","张三丰","张翠山","张良","王二麻子","谢广坤");
list.stream().filter(x -> x.startsWith("张")).filter(x -> x.length() == 3).forEach(x -> System.out.println(x));
System.out.println("--------------------------------------");
//只要前三个
list.stream().limit(3).forEach(x -> System.out.println(x));
System.out.println("--------------------------------------");
//跳过前三个
list.stream().skip(3).forEach(x -> System.out.println(x));
System.out.println("---------------------------------------");
//需求:要张良
list.stream().skip(6).limit(1).forEach(x -> System.out.println(x));
// distinct 过滤
list.stream().distinct().forEach(x -> System.out.println(x));
System.out.println("---------------------------------------");
//Stream.concat 合并两个流为一个流
Stream.concat(list.stream(),list1.stream()).distinct().forEach(x -> System.out.println(x));
//map类型转换
list.stream().map(x -> Integer.parseInt(x.split("-")[1])).distinct().forEach(x -> System.out.println(x));
}
}
Stream流终结方法
/**
*forEach 打印流里的数据
*count 统计流里的数据个数
*Stream.toArray(x -> new String[x])将流里的数据放到数组中 也就是List集合转数组
*Arrays.toString(数组)将数组转为字符串
**/
List<String> list = new ArrayList<>();
Collections.addAll(list,"张无忌-15","张无忌-14","张无忌-13","周芷若-20","赵敏-63","张强-36","张三丰-21","张翠山-62","张良-4","王二麻子-9","谢广坤-6");
//forEach打印流里数据 为终极方法
list.stream().forEach(x -> System.out.println(x));
//stream().count()打印流里数据数量 为终结方法
System.out.println(list.stream().count());
System.out.println("====================================");
//stream().toArray 将流里的数据放到数组
String[] strings = list.stream().toArray(x -> new String[x]);
String s = Arrays.toString(strings);
System.out.println(s);
Stream收集方法
ArrayList<String> list =new ArrayList<>();
Collections.addAll(list,"张无忌-男-15","赵敏-女-13","张强-男-20","张三丰-男-100","张翠山-男-40","张良-男-35","王二麻子-男-37","谢广坤-男-41");
1 //将男性数据收集到List集合中 collect(Collectors.toList())
List<String> collect = list.stream().filter(x -> "男".equals(x.split("-")[1])).collect(Collectors.toList());
System.out.println(collect);
2 //将集合中满足条件的变成Set集合 collect(Collectors.toSet()
Set<String> collect1 = list.stream().filter(x -> "男".equals(x.split("-")[1])).collect(Collectors.toSet());
System.out.println(collect1);
System.out.println("========================");
3 //将集合中满足条件的数据拆分然后变成Map集合 collect(Collectors.toMap
Map<String, Integer> collect2 = list.stream().filter(x -> "男".equals(x.split("-")[1])).collect(Collectors.toMap(
x -> x.split("-")[0],
x -> Integer.parseInt(x.split("-")[2])
));
System.out.println(collect2);
Strem流案例
//1
List<Integer> list = new ArrayList<>();
Collections.addAll(list,1,2,3,4,5,6,7,8,9);
list.stream.filter(x -> x % == 0).forEach(system.out.printl(x))
//2
List<Integer> collect = list.stream().filter(x -> x % 2 == 0).collect(Collectors.toList());
System.out.println(collect);
List<String> list1 = new ArrayList<>();
Collections.addAll(list1,"zhangsan,23","lisi,24","wangwu,25");
Map<String, Integer> collect1 = list1.stream().filter(x -> Integer.parseInt(x.split(",")[1]) >= 24).collect(Collectors.toMap(
x -> x.split(",")[0],
x -> Integer.parseInt(x.split(",")[1])
));
System.out.println(collect1);
//3
List<String> list1 = new ArrayList<>();
Collections.addAll(list1,"张无忌,15","周芷若,20","赵敏,63","张强,36","张三丰,21","张翠山,62");
List<String> list2 = new ArrayList<>();
Collections.addAll(list2,"杨超越,23","范冰冰,19","吴秋妮,23","夏思凝,26","杨老师,36","苍老师,32");
//男演员只要名字为为三个字的前两人
List<String> stream1 = list1.stream().filter(x -> x.split(",")[0].length() == 3).limit(2).collect(Collectors.toList());
//女演员只要姓杨的,并且不要第一个
List<String> stream2 = list2.stream().filter(x -> x.split(",")[0].startsWith("杨")).skip(1).collect(Collectors.toList());
//把过滤后的男演员姓名和女演员姓名合并到一起
List<Actor> collect = Stream.concat(stream1.stream(), stream2.stream())
.map(s -> new Actor(s.split(",")[0], Integer.parseInt(s.split(",")[1]))).collect(Collectors.toList());
//将上一步的演员信息封装成Actor对象
System.out.println(collect);