当前位置: 首页 > news >正文

Java8_StreamAPI

Stream

1.创建流

1.1 集合创建流

List<String> list = List.of("a", "b", "c");
Stream<String> stream = list.stream();
stream.forEach(System.out::println);

1.2 数组创建流

String[] array = {"a","b","c"};
Stream<String> stream = Arrays.stream(array);
stream.forEach(System.out::println);

1.3 Stream.of()

Stream<String> stream = Stream.of("a", "b", "c");
stream.forEach(System.out::println);

合并流:

Stream<String> stream1 = Stream.of("a", "b", "c");
Stream<String> stream2 = Stream.of("e", "f", "g");
Stream<String> concat = Stream.concat(stream1, stream2);
concat.forEach(System.out::println);

1.4 创建一个能动态加入元素的流

Stream.Builder<String> stringBuilder = Stream.builder();
stringBuilder.add("a");
stringBuilder.add("b");
if (Math.random() > 0.5) {stringBuilder.add("c");
}
Stream<String> stream = stringBuilder.build();
stream.forEach(System.out::println);

注: build() 以后就不能再往stream中添加元素, 否则会报IllegalStateException

1.5 从文件创建流

Path path = Paths.get("file.txt");
try (Stream<String> lines = Files.lines(path)){lines.forEach(System.out::println);
} catch (IOException e) {throw new RuntimeException(e);
}

每行文本是一个元素

1.6 创建基本类型的流

以IntStream为例

  • IntStream.of
IntStream intStream = IntStream.of(1, 2, 3);
intStream.forEach(System.out::println);
  • 创建指定范围的IntStream
IntStream intStream = IntStream.range(1,4);
intStream.forEach(System.out::println);// 1 2 3
IntStream intStream = IntStream.rangeClosed(1,4);
intStream.forEach(System.out::println);// 1 2 3 4
  • 创建n个包含随机数的流
IntStream intStream = IntStream.rangeClosed(1,4);
new Random().ints(5).forEach(System.out::println);// 1555978888
// 1191117249
// -1175853188
// 249139444
// -1230944054
  • 基本类型的流转为对象流
IntStream intStream = IntStream.rangeClosed(1,4);
Stream<Integer> integerStream = intStream.boxed();

1.7 创建无限流

generate:

  • 生个n个相同元素的流
Stream<String> stream = Stream.generate(() -> "kiddkid").limit(4);
// 防止无限流生成, 限制个数
stream.forEach(System.out::println);
// kiddkid
// kiddkid
// kiddkid
// kiddkid
  • 生成含n个随机数的流
Stream.generate(Math::random).limit(5).forEach(System.out::println);
// 0.9578029464950379
// 0.03290878021143662
// 0.7683317134743166
// 0.7188884787961349
// 0.8739307746551834
  • iterate 生成数学序列或实现迭代算法
// 									// (起始元素, 生成条件)
Stream<Integer> stream = Stream.iterate(0, n -> n + 2).limit(10);
stream.forEach(System.out::println);

0
2
4
6
8

                                    // (起始元素, 结束条件, 生成条件)
Stream<Integer> stream = Stream.iterate(0,n -> n <= 10, n -> n + 2);
stream.forEach(System.out::println);

1.8 创建并行流

  • 在已有流的基础上创建并行流
Stream<Integer> stream = Stream.iterate(0,n -> n <= 10, n -> n + 2);
Stream<Integer> parallel = stream.parallel();
  • 集合直接创建并行流
List.of("a","b","c").parallelStream().forEach(System.out::println);// b c a

2.流的中间操作

2.1 筛选和切片

  • 根据条件筛选
List<Person> people = List.of(new Person("Neo",45, "USA"),new Person("Stan",10,"USA"),new Person("Grace",16, "UK"),new Person("Alex",20,"UK"),new Person("Sebastian",40,"FR")
);
people.stream().filter(person -> person.getAge() > 18).forEach(System.out::println);

Person{name=‘Neo’, age=45, country=‘USA’}
Person{name=‘Alex’, age=20, country=‘UK’}
Person{name=‘Sebastian’, age=40, country=‘FR’}

  • 元素去重
Stream.of("apple","orange","apple","orange","cherry").distinct().forEach(System.out::println);

apple
orange
cherry

对自定义类要重写equals 和 hashCode:

List<Person> people = List.of(new Person("Neo",45, "USA"),new Person("Stan",10,"USA"),new Person("Grace",16, "UK"),new Person("Alex",20,"UK"),new Person("Alex",20,"UK"),new Person("Sebastian",40,"FR"),new Person("Sebastian",40,"FR")
);
people.stream().distinct().forEach(System.out::println);

Person{name=‘Neo’, age=45, country=‘USA’}
Person{name=‘Stan’, age=10, country=‘USA’}
Person{name=‘Grace’, age=16, country=‘UK’}
Person{name=‘Alex’, age=20, country=‘UK’}
Person{name=‘Sebastian’, age=40, country=‘FR’}

  • limit 截取流中指定个数的元素
List<Person> people = List.of(new Person("Neo",45, "USA"),new Person("Stan",10,"USA"),new Person("Grace",16, "UK"),new Person("Alex",20,"UK"),new Person("Sebastian",40,"FR")
);
people.stream().limit(2).forEach(System.out::println);

Person{name=‘Neo’, age=45, country=‘USA’}
Person{name=‘Stan’, age=10, country=‘USA’}

注: 若指定元素个数大于整个流的长度, 将会返回整个流

  • skip 省略流中指定个数的元素
List<Person> people = List.of(new Person("Neo",45, "USA"),new Person("Stan",10,"USA"),new Person("Grace",16, "UK"),new Person("Alex",20,"UK"),new Person("Sebastian",40,"FR")
);
people.stream().skip(2).forEach(System.out::println);

Person{name=‘Grace’, age=16, country=‘UK’}
Person{name=‘Alex’, age=20, country=‘UK’}
Person{name=‘Sebastian’, age=40, country=‘FR’}

  • map 改变流中元素的类型
List<Person> people = List.of(new Person("Neo",45, "USA"),new Person("Stan",10,"USA"),new Person("Grace",16, "UK"),new Person("Alex",20,"UK"),new Person("Sebastian",40,"FR")
);
Stream<Person> stream = people.stream();
Stream<String> nameStream = stream.map(person -> person.getName());
nameStream.forEach(System.out::println);

Neo
Stan
Grace
Alex
Sebastian

2.2 映射

  • flatMap实现单层流

List<List<Person>> peopleGroup = List.of(List.of(new Person("Neo",45, "USA"),new Person("Stan",10,"USA")),List.of(new Person("Grace",16, "UK"),new Person("Alex",20,"UK")),List.of(new Person("Sebastian",40,"FR"))
);
Stream<List<Person>> peopleGroupStream = peopleGroup.stream();
peopleGroupStream.forEach(System.out::println);

[Person{name=‘Neo’, age=45, country=‘USA’}, Person{name=‘Stan’, age=10, country=‘USA’}]
[Person{name=‘Grace’, age=16, country=‘UK’}, Person{name=‘Alex’, age=20, country=‘UK’}]
[Person{name=‘Sebastian’, age=40, country=‘FR’}]

修改:

peopleGroupStream.flatMap(people -> people.stream()).forEach(System.out::println);

Person{name=‘Neo’, age=45, country=‘USA’}
Person{name=‘Stan’, age=10, country=‘USA’}
Person{name=‘Grace’, age=16, country=‘UK’}
Person{name=‘Alex’, age=20, country=‘UK’}
Person{name=‘Sebastian’, age=40, country=‘FR’}

注: 流的操作应该是链式操作, 不能对同一个流进行多次操作

Stream<List<Person>> peopleGroupStream = peopleGroup.stream();
Stream<Person> personStream = peopleGroupStream.flatMap(Collection::stream);
peopleGroupStream.limit(1);

链式操作如下:

peopleGroup.stream().flatMap(Collection::stream).map(Person::getName).forEach(System.out::println);
// or 两者实现效果相同
peopleGroup.stream().flatMap(people -> people.stream().map(Person::getName)).forEach(System.out::println);

Neo
Stan
Grace
Alex
Sebastian

flatMap可以实现任何map的操作, 同时将流转为单层流

  • 将流映射为指定类型的流
List<Person> people = List.of(new Person("Neo", 45, "USA"),new Person("Stan", 10, "USA"),new Person("Grace", 16, "UK"),new Person("Alex", 20, "UK"),new Person("Sebastian", 40, "FR")
);
IntStream intStream = people.stream().mapToInt(Person::getAge);
intStream.forEach(System.out::println);

45
10
16
20
40

2.3 排序

排序分为自然排序自定义排序

  • 当流中的元素类型实现了Comparable接口的时候,可以直接调用无参数的sorted(), 按照自然顺序进行排序。
Stream.of("blueberry","cherry","apple","pear").sorted().forEach(System.out::println);

apple
blueberry
cherry
pear

  • 可以定义一个比较器去自定义比较规则。
Stream.of("blueberry","cherry","apple","pear").sorted(Comparator.comparingInt(String::length)).forEach(System.out::println);

pear
apple
cherry
blueberry

Stream.of("blueberry","cherry","apple","pear").sorted(Comparator.comparingInt(String::length).reversed()).forEach(System.out::println);

blueberry
cherry
apple
pear

2.4 综合实践

List<List<Person>> peopleGroup = List.of(List.of(new Person("Neo",45, "USA"),new Person("Stan",10,"USA")),List.of(new Person("Grace",16, "UK"),new Person("Alex",20,"UK"),new Person("Alex",20,"UK")),List.of(new Person("Sebastian",40,"FR"),new Person("Sebastian",40,"FR"))
);peopleGroup.stream().flatMap(people -> people.stream()).filter(person -> person.getAge() > 18).distinct().sorted(Comparator.comparingInt(Person::getAge).reversed()).map(Person::getName).limit(3).skip(1).forEach(System.out::println);

Sebastian
Alex

我们通常用变量来保存中间操作的结果,以便在需要的时候触发执行.

3.终端操作

Stream 的终结操作通常会返回单个值,一旦一个 Stream 实例上的终结操作被调用,流内部元素的迭代以及流处理调用链上的中间操作就会开始执行,当迭代结束后,终结操作的返回值将作为整个流处理的返回值被返回。

anyMatch

anyMatch() 方法以一个 Predicate (java.util.function.Predicate 接口,它代表一个接收单个参数并返回参数是否匹配的函数)作为参数,启动 Stream 的内部迭代,并将 Predicate 参数应用于每个元素。如果 Predicate 对任何元素返回了 true(表示满足匹配),则 anyMatch() 方法的结果返回 true。如果没有元素匹配 Predicate,anyMatch() 将返回 false。

public class StreamAnyMatchExample {public static void main(String[] args) {List<String> stringList = new ArrayList<String>();stringList.add("One flew over the cuckoo's nest");stringList.add("To kill a muckingbird");stringList.add("Gone with the wind");Stream<String> stream = stringList.stream();boolean anyMatch = stream.anyMatch((value) -> value.startsWith("One"));System.out.println(anyMatch);}
}

上面例程的运行结果是 true , 因为流中第一个元素就是以 “One” 开头的,满足 anyMatch 设置的条件。

allMatch

allMatch() 方法同样以一个 Predicate 作为参数,启动 Stream 中元素的内部迭代,并将 Predicate 参数应用于每个元素。如果 Predicate 为 Stream 中的所有元素都返回 true,则 allMatch() 的返回结果为 true。如果不是所有元素都与 Predicate 匹配,则 allMatch() 方法返回 false。

public class StreamAllMatchExample {public static void main(String[] args) {List<String> stringList = new ArrayList<String>();stringList.add("One flew over the cuckoo's nest");stringList.add("To kill a muckingbird");stringList.add("Gone with the wind");Stream<String> stream = stringList.stream();boolean allMatch = stream.allMatch((value) -> value.startsWith("One"));System.out.println(allMatch);}
}

上面的例程我们把流上用的 anyMatch 换成了 allMatch ,结果可想而知会返回 false,因为并不是所有元素都是以 “One” 开头的。

noneMatch

Match 系列里还有一个 noneMatch 方法,顾名思义,如果流中的所有元素都与作为 noneMatch 方法参数的 Predicate 不匹配,则方法会返回 true,否则返回 false。

public class StreamNoneExample {public static void main(String[] args) {List<String> stringList = new ArrayList<>();stringList.add("abc");stringList.add("def");Stream<String> stream = stringList.stream();boolean noneMatch = stream.noneMatch((element) -> {return "xyz".equals(element);});System.out.println("noneMatch = " + noneMatch); //输出 noneMatch = true}
}

collect

collect() 方法被调用后,会启动元素的内部迭代,并将流中的元素收集到集合或对象中。

public class StreamCollectExample {public static void main(String[] args) {List<String> stringList = new ArrayList<>();stringList.add("One flew over the cuckoo's nest");stringList.add("To kill a muckingbird");stringList.add("Gone with the wind");Stream<String> stream = stringList.stream();List<String> stringsAsUppercaseList = stream.map(value -> value.toUpperCase()).collect(Collectors.toList());System.out.println(stringsAsUppercaseList);}
}

collect() 方法将收集器 – Collector (java.util.stream.Collector) 作为参数。在上面的示例中,使用的是 Collectors.toList() 返回的 Collector 实现。这个收集器把流中的所有元素收集到一个 List 中去。

count

count() 方法调用后,会启动 Stream 中元素的迭代,并对元素进行计数。

public class StreamExamples {public static void main(String[] args) {List<String> stringList = new ArrayList<String>();stringList.add("One flew over the cuckoo's nest");stringList.add("To kill a muckingbird");stringList.add("Gone with the wind");Stream<String> stream = stringList.stream();long count = stream.flatMap((value) -> {String[] split = value.split(" ");return Arrays.asList(split).stream();}).count();System.out.println("count = " + count); // count = 14}
}

上面的例程中,首先创建一个字符串 List ,然后获取该 List 的 Stream,为其添加了 flatMap() 和 count() 操作。 count() 方法调用后,流处理将开始迭代 Stream 中的元素,处理过程中字符串元素在 flatMap() 操作中被拆分为单词、合并成一个由单词组成的 Stream,然后在 count() 中进行计数。所以最终打印出的结果是 count = 14。

findAny

findAny() 方法可以从 Stream 中找到单个元素。找到的元素可以来自 Stream 中的任何位置。且它不提供从流中的哪个位置获取元素的保证。

public class StreamFindAnyExample {public static void main(String[] args) {List<String> stringList = new ArrayList<>();stringList.add("one");stringList.add("two");stringList.add("three");stringList.add("one");Stream<String> stream = stringList.stream();Optional<String> anyElement = stream.findAny();if (anyElement.isPresent()) {System.out.println(anyElement.get());} else {System.out.println("not found");}}
}

findAny() 方法会返回一个 Optional,意味着 Stream 可能为空,因此没有返回任何元素。我们可以通过 Optional 的 isPresent() 方法检查是否找到了元素。

Optional 类是一个可以为 null 的容器对象。如果值存在则 isPresent() 方法会返回true,调用get()方法会返回容器中的对象,否则抛出异常:NoSuchElementException

findFirst

findFirst() 方法将查找 Stream 中的第一个元素,跟 findAny() 方法一样,也是返回一个 Optional,我们可以通过 Optional 的 isPresent() 方法检查是否找到了元素。

public class StreamFindFirstExample {public static void main(String[] args) {List<String> stringList = new ArrayList<>();stringList.add("one");stringList.add("two");stringList.add("three");stringList.add("one");Stream<String> stream = stringList.stream();Optional<String> anyElement = stream.findFirst();if (anyElement.isPresent()) {System.out.println(anyElement.get());} else {System.out.println("not found");}}
}

forEach

forEach() 方法我们在介绍 Collection 的迭代时介绍过,当时主要是拿它来迭代 List 的元素。它会启动 Stream 中元素的内部迭代,并将 Consumer (java.util.function.Consumer, 一个函数式接口,上面介绍过) 应用于 Stream 中的每个元素。 注意 forEach() 方法的返回值是 void。

public class StreamExamples {public static void main(String[] args) {List<String> stringList = new ArrayList<String>();stringList.add("one");stringList.add("two");stringList.add("three");Stream<String> stream = stringList.stream();stream.forEach(System.out::println);}
}

注意,上面例程中 forEach 的参数我们直接用了Lambda 表达式引用方法的简写形式。

min

min() 方法返回 Stream 中的最小元素。哪个元素最小是由传递给 min() 方法的 Comparator 接口实现来确定的。

public class StreamMinExample {public static void main(String[] args) {List<String> stringList = new ArrayList<>();stringList.add("abc");stringList.add("def");Stream<String> stream = stringList.stream();// 作为 min 方法参数的Lambda 表达式可以简写成 String::compareTo// Optional<String> min = stream.min(String::compareTo);Optional<String> min = stream.min((val1, val2) -> {return val1.compareTo(val2);});String minString = min.get();System.out.println(minString); // abc}
}

min() 方法返回的是一个 Optional ,也就是它可能不包含结果。如果为空,直接调用 Optional 的 get() 方法将抛出 异常–NoSuchElementException。比如我们把上面的 List 添加元素的两行代码注释掉后,运行程序就会报

Exception in thread "main" java.util.NoSuchElementException: No value presentat java.util.Optional.get(Optional.java:135)at com.example.StreamMinExample.main(StreamMinExample.java:21)

所以最好先用 Optional 的 ifPresent() 判断一下是否包含结果,再调用 get() 获取结果。

max

与 min() 方法相对应,max() 方法会返回 Stream 中的最大元素,max() 方法的参数和返回值跟 min() 方法的也都一样,这里就不再过多阐述了,只需要把上面求最小值的方法替换成求最大值的方法 max() 即可。

Optional<String> min = stream.max(String::compareTo);

reduce

reduce() 方法,是 Stream 的一个聚合方法,它可以把一个 Stream 的所有元素按照聚合函数聚合成一个结果。reduce()方法接收一个函数式接口 BinaryOperator 的实现,它定义的一个apply()方法,负责把上次累加的结果和本次的元素进行运算,并返回累加的结果。

public class StreamReduceExample {public static void main(String[] args) {List<String> stringList = new ArrayList<>();stringList.add("One flew over the cuckoo's nest");stringList.add("To kill a muckingbird");stringList.add("Gone with the wind");Stream<String> stream = stringList.stream();Optional<String> reduced = stream.reduce((value, combinedValue) -> combinedValue + " + " + value);// 写程序的时候记得别忘了 reduced.ifPresent() 检查结果里是否有值System.out.println(reduced.get());}
}

reduce() 方法的返回值同样是一个 Optional 类的对象,所以在获取值前别忘了使用 ifPresent() 进行检查。

stream 实现了多个版本的reduce() 方法,还有可以直接返回元素类型的版本,比如使用 reduce 实现整型Stream的元素的求和

public class IntegerStreamReduceSum {public static void main(String[] args) {List<Integer> intList = new ArrayList<>();intList.add(10);intList.add(9);intList.add(8);intList.add(7);Integer sum = intList.stream().reduce(0, Integer::sum);System.out.printf("List 求和,总和为%s\n", sum);}
}

toArray

toArray() 方法是一个流的终结操作,它会启动流中元素的内部迭代,并返回一个包含所有元素的 Object 数组。

List<String> stringList = new ArrayList<String>();stringList.add("One flew over the cuckoo's nest");
stringList.add("To kill a muckingbird");
stringList.add("Gone with the wind");Stream<String> stream = stringList.stream();Object[] objects = stream.toArray();

不过 toArray 还有一个重载方法,允许传入指定类型数组的构造方法,比如我们用 toArray 把流中的元素收集到字符串数组中,可以这么写:

String[] strArray = stream.toArray(String[]::new);

相关文章:

Java8_StreamAPI

Stream 1.创建流 1.1 集合创建流 List<String> list List.of("a", "b", "c"); Stream<String> stream list.stream(); stream.forEach(System.out::println);1.2 数组创建流 String[] array {"a","b",&qu…...

[STM32 - 野火] - - - 固件库学习笔记 - - -十二.基本定时器

一、定时器简介 STM32 中的定时器&#xff08;TIM&#xff0c;Timer&#xff09;是其最重要的外设之一&#xff0c;广泛用于时间管理、事件计数和控制等应用。 1.1 基本功能 定时功能&#xff1a;TIM定时器可以对输入的时钟进行计数&#xff0c;并在计数值达到设定值时触发中…...

信息学奥赛一本通 1606:【 例 1】任务安排 1 | 洛谷 P2365 任务安排

【题目链接】 ybt 1606&#xff1a;【 例 1】任务安排 1 洛谷 P2365 任务安排 【题目考点】 1. 动态规划&#xff1a;线性动规 【解题思路】 可以先了解法1&#xff0c;虽然不是正解&#xff0c;但该解法只使用了动规的基本思路&#xff0c;易于理解&#xff0c;有助于理解…...

【解决方案】MuMu模拟器移植系统进度条卡住98%无法打开

之前在Vmware虚拟机里配置了mumu模拟器&#xff0c;现在想要移植到宿主机中 1、虚拟机中的MuMu模拟器12-1是目标系统&#xff0c;对应的目录如下 C:\Program Files\Netease\MuMu Player 12\vms\MuMuPlayer-12.0-1 2、Vmware-虚拟机-设置-选项&#xff0c;启用共享文件夹 3、复…...

【C语言】预处理详解

他们想要逃避工作的压迫&#xff0c;却又被功绩社会深植的价值观绑架。 前言 这是我自己学习C语言的第九篇博客总结。后期我会继续把C语言学习笔记开源至博客上。 上一期笔记是关于C语言的编译链接&#xff0c;没看的同学可以过去看看&#xff1a; 【C语言】编译链接_c 读取一行…...

设计模式Python版 适配器模式

文章目录 前言一、适配器模式二、适配器模式实现三、适配器模式在Django中的应用 前言 GOF设计模式分三大类&#xff1a; 创建型模式&#xff1a;关注对象的创建过程&#xff0c;包括单例模式、简单工厂模式、工厂方法模式、抽象工厂模式、原型模式和建造者模式。结构型模式&…...

系统思考—蝴蝶效应

“个体行为的微小差异&#xff0c;可能在系统中引发巨大且不可预测的结果。” — 诺贝尔经济学得主托马斯谢林 我们常说&#xff0c;小变动带来大影响&#xff0c;这种现象&#xff0c;在复杂系统理论中被称为“蝴蝶效应”&#xff1a;即使极小的变化&#xff0c;也能在动态系…...

使用Edu邮箱申请一年免费的.me域名

所需材料&#xff1a;公立Edu教育邮箱一枚&#xff08;P.S&#xff1a;该服务不支持所有的Edu教育邮箱&#xff0c;仅支持比较知名的院校&#xff09; 说到域名&#xff0c;.me这个后缀可谓是个性十足&#xff0c;适合个人网站、博客等。.me是黑山的国家顶级域名&#xff08;c…...

【开源免费】基于SpringBoot+Vue.JS体育馆管理系统(JAVA毕业设计)

本文项目编号 T 165 &#xff0c;文末自助获取源码 \color{red}{T165&#xff0c;文末自助获取源码} T165&#xff0c;文末自助获取源码 目录 一、系统介绍二、数据库设计三、配套教程3.1 启动教程3.2 讲解视频3.3 二次开发教程 四、功能截图五、文案资料5.1 选题背景5.2 国内…...

C++ ——— 仿函数

目录 何为仿函数 仿函数和模板的配合使用 何为仿函数 代码演示&#xff1a; class Add { public:int operator()(int x, int y){return x y;} }; 这是一个 Add 类&#xff0c;类里面有一个公有成员函数重载&#xff0c;重载的是括号 那么调用的时候&#xff1a; Add ad…...

基于FPGA的BT656解码

概述 BT656全称为“ITU-R BT.656-4”或简称“BT656”,是一种用于数字视频传输的接口标准。它规定了数字视频信号的编码方式、传输格式以及接口电气特性。在物理层面上,BT656接口通常包含10根线(在某些应用中可能略有不同,但标准配置为10根)。这些线分别用于传输视频数据、…...

【Proteus仿真】【51单片机】简易计算器系统设计

目录 一、主要功能 二、使用步骤 三、硬件资源 四、软件设计 五、实验现象 联系作者 一、主要功能 1、LCD1602液晶显示 2、矩阵按键​ 3、可以进行简单的加减乘除运算 4、最大 9999*9999 二、使用步骤 系统运行后&#xff0c;LCD1602显示数据&#xff0c;通过矩阵按键…...

【16届蓝桥杯寒假刷题营】第1期DAY2

1.能选取元素的最多个数 - 蓝桥云课 问题描述 给定一个长度为 n 的数组 a&#xff0c;小蓝希望从数组中选择若干个元素&#xff08;可以不连续&#xff09;&#xff0c;并将它们重新排列&#xff0c;使得这些元素 能够形成一个先严格递增然后严格递减的子序列&#xff08;可以…...

安卓(android)饭堂广播【Android移动开发基础案例教程(第2版)黑马程序员】

一、实验目的&#xff08;如果代码有错漏&#xff0c;可查看源码&#xff09; 1.熟悉广播机制的实现流程。 2.掌握广播接收者的创建方式。 3.掌握广播的类型以及自定义官博的创建。 二、实验条件 熟悉广播机制、广播接收者的概念、广播接收者的创建方式、自定广播实现方式以及有…...

linux的/proc 和 /sys目录差异

/proc 和 /sys 都是Linux系统中用于提供系统信息和进行系统配置的虚拟文件系统&#xff0c;但它们的原理并不完全一样&#xff0c;以下是具体分析&#xff1a; 目的与功能 /proc &#xff1a;主要用于提供系统进程相关信息以及内核运行时的一些参数等&#xff0c;可让用户和程…...

使用Navicat Premium管理数据库时,如何关闭事务默认自动提交功能?

使用Navicat Premium管理数据库时&#xff0c;最糟心的事情莫过于事务默认自动提交&#xff0c;也就是你写完语句运行时&#xff0c;它自动执行commit提交至数据库&#xff0c;此时你就无法进行回滚操作。 建议您尝试取消勾选“选项”中的“自动开始事务”&#xff0c;点击“工…...

HTB:Active[RE-WriteUP]

目录 连接至HTB服务器并启动靶机 信息收集 使用rustscan对靶机TCP端口进行开放扫描 将靶机TCP开放端口号提取并保存 使用nmap对靶机TCP开放端口进行脚本、服务扫描 使用nmap对靶机TCP开放端口进行漏洞、系统扫描 使用nmap对靶机常用UDP端口进行开放扫描 使用nmap对靶机…...

新春登蛇山:告别岁月,启航未来

大年初一&#xff0c;晨曦透过薄雾&#xff0c;温柔地洒在武汉的大街小巷。2025 年的蛇年春节&#xff0c;带着新春的喜气与希望悄然而至。我站在蛇山脚下&#xff0c;心中涌动着复杂的情感&#xff0c;因为今天&#xff0c;我不仅将与家人一起登山揽胜&#xff0c;更将在这一天…...

如何获取小程序的code在uniapp开发中

如何获取小程序的code在uniapp开发中&#xff0c;也就是本地环境&#xff0c;微信开发者工具中获取code&#xff0c;这里的操作是页面一进入就获取code登录&#xff0c;没有登录页面的交互&#xff0c;所以写在了APP.vue中&#xff0c;也就是小程序一打开就获取用户的code APP.…...

LLM评估与优化技术解析

标题&#xff1a;LLM评估与优化技术解析 文章信息摘要&#xff1a; LLM的评估方法主要包括自动化基准测试、人工评估和基于模型的评估&#xff0c;每种方法各有优缺点。自动化测试快速但难以捕捉细微差别&#xff0c;人工评估细致但成本高&#xff0c;基于模型的评估结合了两者…...

SpringBoot 原理分析

SpringBoot 原理分析 依赖管理相关 启动器 starter Spring Boot 的 Starter 是预定义依赖项集合&#xff0c;可简化 Spring 应用配置与构建&#xff0c;启动时自动引入所需库、配置和功能。 Spring Boot 有很多预定义 Starter&#xff0c;如 spring - boot - starter - web 用…...

go入门Windows环境搭建

简介 Go 即 Golang&#xff0c;是 Google 公司 2009 年 11 月正式对外公开的一门编程语言。 根据 Go 语言开发者自述&#xff0c;近 10 多年&#xff0c;从单机时代的 C 语言到现在互联网时代的 Java&#xff0c;都没有令人满意的开发语言&#xff0c;而 C往往给人的感觉是&a…...

拦截器快速入门及详解

拦截器Interceptor 快速入门 什么是拦截器&#xff1f; 是一种动态拦截方法调用的机制&#xff0c;类似于过滤器。 拦截器是Spring框架中提供的&#xff0c;用来动态拦截控制器方法的执行。 拦截器的作用&#xff1a;拦截请求&#xff0c;在指定方法调用前后&#xff0c;根…...

Fort Firewall:全方位守护网络安全

Fort Firewall是一款专为 Windows 操作系统设计的开源防火墙工具&#xff0c;旨在为用户提供全面的网络安全保护。它基于 Windows 过滤平台&#xff08;WFP&#xff09;&#xff0c;能够与系统无缝集成&#xff0c;确保高效的网络流量管理和安全防护。该软件支持实时监控网络流…...

第3章 基于三电平空间矢量的中点电位平衡策略

0 前言 在NPC型三电平逆变器的直流侧串联有两组参数规格完全一致的电解电容,由于三电平特殊的中点钳位结构,在进行SVPWM控制时,在一个完整开关周期内,直流侧电容C1、C2充放电不均匀,各自存储的总电荷不同,电容电压便不均等,存在一定的偏差。在不进行控制的情况下,系统无…...

九大服务构建高效 AIOps 平台,全面解决GenAI落地挑战

最近,DevOps运动的联合创始人Patrick Debois分享了他对AI平台与软件研发关系的深刻见解,让我们一起来探讨这个话题。 在AI的落地过程中,我们面临着两个主要难题: 引入AI编码工具后的开发者角色转变:随着像GitHub Copilot这样的AI工具的普及,工程师的角色正在发生深刻变革…...

Leetcode 131 分割回文串(纯DFS)

131. 分割回文串https://leetcode.cn/problems/palindrome-partitioning/https://leetcode.cn/problems/palindrome-partitioning/ 给你一个字符串 s&#xff0c;请你将 s 分割成一些子串&#xff0c;使每个子串都是 回文串 。返回 s 所有可能的分割方案。 示例 1&#xff1a…...

我的AI工具箱Tauri+Django内容生产介绍和使用

在现代内容生产环境中&#xff0c;高效、自动化的工具能够显著提升生产力&#xff0c;降低人工成本。Tauri 与 Django 结合打造的工作箱&#xff0c;集成了强大的 音频处理、视频剪辑、内容下载 以及 AI 文章撰写 等模块&#xff0c;帮助用户在多媒体内容生产的各个环节实现高效…...

“星门计划对AI未来的意义——以及谁将掌控它”

“星门计划对AI未来的意义——以及谁将掌控它” 图片由DALL-E 3生成 就在几天前&#xff0c;唐纳德特朗普宣布了“星门计划”&#xff0c;OpenAI随即跟进&#xff0c;分享了更多细节。他们明确表示&#xff0c;计划在未来四年内投资5000亿美元&#xff0c;在美国为OpenAI构建一…...

论文阅读(八):结构方程模型用于研究数量遗传学中的因果表型网络

1.论文链接&#xff1a;Structural Equation Models for Studying Causal Phenotype Networks in Quantitative Genetics 摘要&#xff1a; 表型性状可能在它们之间发挥因果作用。例如&#xff0c;农业物种的高产可能会增加某些疾病的易感性&#xff0c;相反&#xff0c;疾病的…...

【Numpy核心编程攻略:Python数据处理、分析详解与科学计算】1.25 视觉风暴:NumPy驱动数据可视化

1.25 视觉风暴&#xff1a;NumPy驱动数据可视化 目录 #mermaid-svg-i3nKPm64ZuQ9UcNI {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-i3nKPm64ZuQ9UcNI .error-icon{fill:#552222;}#mermaid-svg-i3nKPm64ZuQ9UcNI …...

Linux Samba 低版本漏洞(远程控制)复现与剖析

目录 前言 漏洞介绍 漏洞原理 产生条件 漏洞影响 防御措施 复现过程 结语 前言 在网络安全的复杂生态中&#xff0c;系统漏洞的探索与防范始终是保障数字世界安全稳定运行的关键所在。Linux Samba 作为一款在网络共享服务领域应用极为广泛的软件&#xff0c;其低版本中…...

solidity基础 -- 可视范围

在 Solidity 编程语言中&#xff0c;可视范围&#xff08;Visibility&#xff09;用于控制合约中变量和函数的访问权限。这对于确保合约的安全性、模块化以及代码的可维护性至关重要。Solidity 提供了四种可视范围修饰符&#xff1a;public、private、external 和 internal。以…...

Spring Boot - 数据库集成07 - 数据库连接池

数据库连接池 文章目录 数据库连接池一&#xff1a;知识准备1&#xff1a;什么是数据库连接池&#xff1f;2&#xff1a;数据库连接池基本原理 二&#xff1a;HikariCP连接池1&#xff1a;简单使用2&#xff1a;进一步理解2.1&#xff1a;是SpringBoot2.x默认连接池2.2&#xf…...

智云-一个抓取web流量的轻量级蜜罐-k8s快速搭建教程

智云-一个抓取web流量的轻量级蜜罐-k8s快速搭建教程 github地址 https://github.com/xiaoxiaoranxxx/POT-ZHIYUN k8s搭建教程 首先下载代码文件 git clone https://github.com/xiaoxiaoranxxx/POT-ZHIYUN.git cd POT-ZHIYUN编译镜像 代码相关文件在github https://github.com/x…...

MySQL 事件调度器

MySQL 事件调度器确实是一个更方便且内置的解决方案&#xff0c;可以在 MySQL 服务器端自动定期执行表优化操作&#xff0c;无需依赖外部工具或应用程序代码。这种方式也能减少数据库维护的复杂性&#xff0c;尤其适用于在数据库频繁更新或删除时进行自动化优化。 使用 MySQL …...

3 Spark SQL

Spark SQL 1. 数据分析方式2. SparkSQL 前世今生3. Hive 和 SparkSQL4. 数据分类和 SparkSQL 适用场景1) 结构化数据2) 半结构化数据3) 总结 5. Spark SQL 数据抽象1) DataFrame2) DataSet3) RDD、DataFrame、DataSet 的区别4) 总结 6. Spark SQL 应用1) 创建 DataFrame/DataSe…...

多头潜在注意力(MLA):让大模型“轻装上阵”的技术革新——从DeepSeek看下一代语言模型的高效之路

多头潜在注意力&#xff08;MLA&#xff09;&#xff1a;让大模型“轻装上阵”的技术革新 ——从DeepSeek看下一代语言模型的高效之路 大模型的“内存焦虑” 当ChatGPT等大语言模型&#xff08;LLM&#xff09;惊艳世界时&#xff0c;很少有人意识到它们背后隐藏的“内存焦虑”…...

Spring AI 在微服务中的应用:支持分布式 AI 推理

1. 引言 在现代企业中&#xff0c;微服务架构 已成为开发复杂系统的主流方式&#xff0c;而 AI 模型推理 也越来越多地被集成到业务流程中。如何在分布式微服务架构下高效地集成 Spring AI&#xff0c;使多个服务可以协同完成 AI 任务&#xff0c;并支持分布式 AI 推理&#x…...

赛博算卦之周易六十四卦JAVA实现:六幺算尽天下事,梅花化解天下苦。

佬们过年好呀~新年第一篇博客让我们来场赛博算命吧&#xff01; 更多文章&#xff1a;个人主页 系列文章&#xff1a;JAVA专栏 欢迎各位大佬来访哦~互三必回&#xff01;&#xff01;&#xff01; 文章目录 #一、文化背景概述1.文化起源2.起卦步骤 #二、卦象解读#三、just do i…...

数据分析系列--⑤RapidMiner进行关联分析(中文数据案例)

一、数据集 二、数据预处理 1.读取数据、拆分、重命名 2.数据预处理 三、关联分析 四、结论 一、数据集 点击下载数据集shopping_basket.xlsx ,这个数据集专门使用中文数据来进行分析. 二、数据预处理 1.读取数据、拆分、重命名 2.数据预处理 三、关联分析 四、结论 Ok…...

fpga系列 HDL:XILINX Vivado Vitis 高层次综合(HLS) 实现 EBAZ板LED控制(下)

补充代码&#xff0c;将clk之外的输入都设置能使其运行的默认值 timescale 1ns / 1ps module HLSLED(input wire clk ,input wire rst_n ,output wire led);wire led_o_i 0;reg rst 0;wire led_o_o_ap_vld;hlsv1_0 your_instance_name (.led_o_o_ap_vld(led_o_o_ap_vld), /…...

设计模式面试题

一、工厂方法模式: 1.简单工厂模式: (1).抽象产品:定义了产品的规范&#xff0c;描述了产品的主要特性和功能 (2).具体产品:实现或继承抽象产品的子类 (3).具体工厂:提供了创建产品的方法&#xff0c;调用者通过该方法来获取产品 所有产品都共有一个工厂&#xff0c;如果新…...

7层还是4层?网络模型又为什么要分层?

~犬&#x1f4f0;余~ “我欲贱而贵&#xff0c;愚而智&#xff0c;贫而富&#xff0c;可乎&#xff1f; 曰&#xff1a;其唯学乎” 一、为什么要分层 \quad 网络通信的复杂性促使我们需要一种分层的方法来理解和管理网络。就像建筑一样&#xff0c;我们不会把所有功能都混在一起…...

12.udp

12.udp **1. UDP特性****2. UDP编程框架&#xff08;C/S模式&#xff09;****3. UDP发送接收函数****4. UDP编程练习** 1. UDP特性 连接特性&#xff1a;无链接&#xff0c;通信前无需像TCP那样建立连接。可靠性&#xff1a;不可靠&#xff0c;不保证数据按序到达、不保证数据…...

【Leetcode 热题 100】32. 最长有效括号

问题背景 给你一个只包含 ‘(’ 和 ‘)’ 的字符串&#xff0c;找出最长有效&#xff08;格式正确且连续&#xff09;括号 子串 的长度。 数据约束 0 ≤ s . l e n g t h ≤ 3 1 0 4 0 \le s.length \le 3 \times 10 ^ 4 0≤s.length≤3104 s [ i ] s[i] s[i] 为 ‘(’ 或 ‘…...

Python NumPy(8):NumPy 位运算、NumPy 字符串函数

1 NumPy 位运算 位运算是一种在二进制数字的位级别上进行操作的一类运算&#xff0c;它们直接操作二进制数字的各个位&#xff0c;而不考虑数字的整体值。NumPy 提供了一系列位运算函数&#xff0c;允许对数组中的元素进行逐位操作&#xff0c;这些操作与 Python 的位运算符类似…...

Fork/Join框架_任务分解与并行执行

1 概述 Fork/Join框架是Java 7引入的一个用于并行执行任务的框架。它特别适用于可以递归分解为多个子任务的工作,每个子任务可以独立执行,并且结果可以合并以获得最终结果。Fork/Join框架通过工作窃取(work-stealing)算法提高了多核处理器上的任务执行效率。 2 核心组件 …...

【大厂AI实践】OPPO:大规模知识图谱及其在小布助手中的应用

导读&#xff1a;OPPO知识图谱是OPPO数智工程系统小布助手团队主导、多团队协作建设的自研大规模通用知识图谱&#xff0c;目前已达到数亿实体和数十亿三元组的规模&#xff0c;主要落地在小布助手知识问答、电商搜索等场景。 本文主要分享OPPO知识图谱建设过程中算法相关的技…...

SOME/IP--协议英文原文讲解2

前言 SOME/IP协议越来越多的用于汽车电子行业中&#xff0c;关于协议详细完全的中文资料却没有&#xff0c;所以我将结合工作经验并对照英文原版协议做一系列的文章。基本分三大块&#xff1a; 1. SOME/IP协议讲解 2. SOME/IP-SD协议讲解 3. python/C举例调试讲解 4.1 Speci…...