JDK8新特性之Steam流

这里写目录标题

  • 一、Stream流概述
    • 1.1、传统写法
    • 1.2、Stream写法
    • 1.3、Stream流操作分类
  • 二、Stream流获取方式
    • 2.1、根据Collection获取
    • 2.2、通过Stream的of方法
  • 三、Stream常用方法介绍
    • 3.1、forEach
    • 3.2、count
    • 3.3、filter
    • 3.4、limit
    • 3.5、skip
    • 3.6、map
    • 3.7、sorted
    • 3.8、distinct
    • 3.9、match
    • 3.10、find
    • 3.11、max和min
    • 3.12、reduce方法
      • 3.12.1、 map和reduce的组合
    • 3.13、mapToInt
    • 3.14、concat
    • 3.15、综合案例
    • 4.1、结果收集到集合中
  • 四、Stream结果手集
    • 4.1、结果收集到集合中
    • 4.2、结构收集到数组中
    • 4.3、对流中数据做聚合操作
    • 4.4、对流中数据做分组操作
    • 4.5、对流中数据做分区操作
    • 4.6、对流中数据做拼接
  • 五、并行Stream流
    • 5.1、串行的Stream流
    • 5.2、并行流
      • 5.2.1、获取并行流
      • 5.2.3、并行流操作
    • 5.3、并行流和串行流对比
    • 5.4、线程安全

一、Stream流概述

Java 中,Stream 是一个来自java.util.stream包的接口,用于对集合(如List、Set等)或数组等数据源进行操作的一种抽象层。
Stream流(和IO流没有任何关系)主要是对数据进行加工处理的。Stream API能让我们快速完成许多复杂的操作,如筛选、切片、映射、查找、去除重复,统计,匹配和归约。

1.1、传统写法

现在有需求:对list中姓张,名字长度为3的信息打印:

	public static void main(String[] args) {//定义一个List集合List<String> list = Arrays.asList("张三","张三丰","张无忌","李四","王五");//获取姓张,名字长度为3的信息,添加到列表中List<String> list1=new ArrayList<>();for (String s : list) {if(s.startsWith("张")&&s.length()==3){list1.add(s);}}for (String s : list1) {System.out.println(s);}}

1.2、Stream写法

    public static void main(String[] args) {//定义一个List集合List<String> list = Arrays.asList("张三","张三丰","张无忌","李四","王五");list.stream().filter(s -> s.startsWith("张")).filter(s -> s.length()== 3).forEach(System.out::println);}

上面的SteamAPI代码的含义:获取流,过滤张,过滤长度,逐一打印。代码相比于上面的案例更加的简洁直观。

1.3、Stream流操作分类

  • 生成操作
    通过数据源(集合、数组等)生成流。
  • 中间操作
    对流进行某种程度的过滤/映射,并返回一个新的流。
  • 终结操作
    执行某个终结操作,一个流只能有一个终结操作。

二、Stream流获取方式

2.1、根据Collection获取

java.util.Collection 接口中加入了default方法 stream,也就是说Collection接口下的所有的实现都可以通过steam方法来获取Stream流。(java集合框架主要包括两种类型的容器,一种是集合,存储一个元素集合(Collection),另一种是图(Map),存储键/值对映射)。

    public static void main(String[] args) {List<String> list=new ArrayList<>();Stream<String> stream=list.stream();Set<String> set=new HashSet<>();Stream<String> stream1=set.stream();Vector vector=new Vector();vector.stream();}

Map接口别没有实现Collection接口,这时我们可以根据Map获取对应的key value的集合。

    public static void main(String[] args) {Map<String,Object> map=new HashMap<>();Stream<String> stream=map.keySet().stream();//keyStream<Object> stream1=map.values().stream();//valueStream<Map.Entry<String,Object>> stream2=map.entrySet().stream();//entry}

2.2、通过Stream的of方法

由于数组对象不可能添加默认方法,所有Stream接口中提供了静态方法of操作到数组中的数据

    public static void main(String[] args) {Stream<String> a1= Stream.of("a1","a2","a3");String[] arr1 = {"aa","bb","cc"};Stream<String> arr11 = Stream.of(arr1);Integer[] arr2 = {1,2,3,4};Stream<Integer> arr21 = Stream.of(arr2);arr21.forEach(System.out::println);// 注意:基本数据类型的数组是不行的int[] arr3 = {1,2,3,4};Stream.of(arr3).forEach(System.out::println);}

三、Stream常用方法介绍

Stream常用方法

方法名方法作用返回值类型防范种类
count统计个数long终结
forEach逐一处理void终结
filter过滤Stream函数拼接
limit取用前几个Stream函数拼接
skip跳过前几个Stream函数拼接
map映射Stream函数拼接
concat组合Stream函数拼接
注意:
  1. 这里把常用的API分为“终结方法”和“非终结方法”
  2. “终结方法”:返回值类型不再是 Stream 类型的方法,不再支持链式调用。
  3. “非终结方法”:返回值类型仍然是 Stream 类型的方法,支持链式调用。
  4. Stream方法返回的是新的流。
  5. Stream不调用终结方法,中间的操作不会执行。

3.1、forEach

forEach用来遍历流中的数据的。

void forEach(Consumer<? super T> action);

该方法接受一个Consumer接口,会将每一个流元素交给函数处理。

    public static void main(String[] args) {Stream<String> a1=Stream.of("aa","bb","cc");a1.forEach(System.out::println);}

3.2、count

Stream流中的count方法用来统计其中的元素个数的。

  long count();

该方法返回一个long值,代表元素的个数。

    public static void main(String[] args) {long count = Stream.of("a1", "a2", "a3").count();System.out.println(count);}

3.3、filter

filter方法的作用是用来过滤数据的。返回符合条件的数据。
可以通过filter方法将一个流转换成另一个子集流。

    public static void main(String[] args) {Stream<String> stream =Stream.of("aa", "ab", "bc","a1","b2","c3").filter(s -> s.contains("a"));stream.forEach(System.out::println);}

该接口接收一个Predicate函数式接口参数作为筛选条件。

3.4、limit

limit方法可以对流进行截取处理,支取前n个数据,

  Stream<T> limit(long maxSize);

参数式一个long类型的数值,如果集合当前长度大于参数就进行截取,否则不操作:

      public static void main(String[] args) {Stream.of("a1", "a2", "a3","bb","cc","aa","dd").limit(3).forEach(System.out::println);}

3.5、skip

如果希望跳过前面几个元素,可以使用skip方法获取一个截取之后的新流:

  Stream<T> skip(long n);

示例:

    public static void main(String[] args) {Stream.of("a1","b2","c3","aa","bb","cc").skip(3).forEach(System.out::println);}

3.6、map

如果需要将流中的元素映射到另一个流中,可以使用map方法:

  <R> Stream<R> map(Function<? super T, ? extends R> mapper);

该接口需要一个Function函数式接口参数,可以将当前流中的T类型数据转换为另一种R类型的数据

    public static void main(String[] args) {Stream.of("1","2","3","4","5")//.map(s -> Integer.parseInt(s)).map(Integer::parseInt).forEach(System.out::println);}

3.7、sorted

如果需要将数据排序,可以使用sorted方法:

  Stream<T> sorted();

在使用的时候可以根据自然规则排序,也可以通过比较强来指定对应的排序规则

      public static void main(String[] args) {Stream.of("1","2","7","9","3","4","6").map(Integer::parseInt).sorted((o1, o2) -> (o1 - o2)).forEach(System.out::println);}

3.8、distinct

如果要去掉重复数据,可以使用distinct方法

	Stream<T> distinct();  

Stream流中的distinct方法对于基本数据类型式可以直接去重的,但是对于自定义类型,我们需要重写hashCode和equals方法来移除重复数据。

    public static void main(String[] args) {Stream.of("1","2","4","5","2","3","4").sorted().distinct().forEach(System.out::println);System.out.println("----------------");Stream.of(new Person("张三",18),new Person("李四",18),new Person("王五",20),new Person("张三",18)).distinct().forEach(System.out::println);}  

3.9、match

如果需要判断数据是否匹配指定的条件,可以使用match相关的方法:

boolean anyMatch(Predicate<? super T> predicate); // 元素是否有任意一个满足条件
boolean allMatch(Predicate<? super T> predicate); // 元素是否都满足条件
boolean noneMatch(Predicate<? super T> predicate); // 元素是否都不满足条件

使用:

    public static void main(String[] args) {boolean b = Stream.of("1","2","3","4","5","7").map(Integer::parseInt)//.anyMatch(s-> s > 4);//.allMatch(s-> s > 4);.noneMatch(s-> s > 4);System.out.println(b);}  

注:match是一个终结方法。

3.10、find

如果我们需要找到某些数据,可以使用find方法来实现

	Optional<T> findFirst();Optional<T> findAny();

使用:

    public static void main(String[] args) {Optional<String> first = Stream.of("a", "b", "c").findFirst();System.out.println(first.get());Optional<String> any = Stream.of("a", "b", "c","d").findAny();System.out.println(any.get());}

注:

3.11、max和min

如果我们想要获取最大值和最小值,那么可以使用max和min方法

    public static void main(String[] args) {Optional<Integer> max= Stream.of(1,2,3,4).max(Integer::compareTo);System.out.println(max.get());Optional<Integer> min= Stream.of(1,2,3,4).min(Integer::compareTo);System.out.println(min.get());}  

3.12、reduce方法

如果需要将所有数据归纳得到一个数据,可以使用reduce方法:

    public static void main(String[] args) {Integer sum = Stream.of(4,5,3,7)//identity默认值//第一次的时候会将默认值赋给x//之后每次将上一次的操作结果赋值给x y,就是每次从数据中获取的元素.reduce(0, (x, y) -> {System.out.println("x="+x+",y="+y);return x + y;});System.out.println("sum = " + sum);// 获取最大值Integer max = Stream.of(4,5,3,9).reduce(0,(x,y) ->{return x>y?x:y;});System.out.println("max = " + max);}  

3.12.1、 map和reduce的组合

在实际开发中我们经常会将map和reduce一块来使用:

    public static void main(String[] args) {//1、求出所有年龄总和Integer sumAge= Stream.of(new Person("张三", 20),new Person("李四", 21),new Person("王五", 22),new Person("赵六", 23)).map(Person::getAge).reduce(0,Integer::sum);System.out.println(sumAge);//2、求出所有年龄最大值Integer maxAge= Stream.of(new Person("张三", 20),new Person("李四", 21),new Person("王五", 22),new Person("赵六", 23)).map(Person::getAge).reduce(Integer::max).get();System.out.println(maxAge);//3、统计字符 a 出现的次数Integer countA= Stream.of("a","b","c","d","e","a","a","a").map(ch -> ch.equals("a")?1:0).reduce(0,Integer::sum);System.out.println(countA);}  

3.13、mapToInt

如果需要将Sream中的Integer类型转换成int类型,可以使用mapToInt方法来实现

    public static void main(String[] args) {//Integer 占用的内存比int多很多,在Stream流操作中会自动装修和拆箱操作Integer arr[] ={1,2,3,4,6,7,8};Stream.of(arr).filter(x -> x > 0).forEach(System.out::println);//为了提高程序代码效率,可以先将流中Integer数据转为为int数据IntStream intStream = Stream.of(arr).mapToInt(Integer::intValue);intStream.forEach(System.out::println);}  

3.14、concat

如果有两个流,希望合并成为一个流,那么可以使用Stream接口防范concat

    public static void main(String[] args) {Stream<String> steam1=Stream.of("a","b","c");Stream<String> steam2=Stream.of("x","y","z");//通过concat方法将两个流合并成一个新的流Stream.concat(steam1,steam2).forEach(System.out::println);}  

3.15、综合案例

4.1、结果收集到集合中

    public static void main(String[] args) {List<String> list1 = Arrays.asList("迪丽热巴", "宋远桥", "苏星河", "老子","庄子", "孙子", "洪七公");List<String> list2 = Arrays.asList("古力娜扎", "张无忌", "张三丰", "赵丽颖","张二狗", "张天爱", "张三");Stream<String> stream1 = list1.stream().filter(s -> s.length() == 3).limit(3);Stream<String> stream2 =list2.stream().filter(s -> s.startsWith("张")).skip(2);Stream.concat(stream1,stream2)//.map(s -> new Person(s)).map(Person::new).forEach(System.out::println);}

四、Stream结果手集

4.1、结果收集到集合中

    @Testpublic void test01(){//收集到list集合中List<String> list= Stream.of("a","b","c").collect(Collectors.toList());System.out.println(list);//收集到set集合中Set<String> set = Stream.of("aa","bb","cc","dd").collect(Collectors.toSet());System.out.println(set);//如果需要获取的类型为具体实现ArrayList<String> arrayList = Stream.of("aaa","bbb","ccc")//.collect(Collectors.toCollection(() -> new ArrayList<>()));.collect(Collectors.toCollection(ArrayList::new));System.out.println(arrayList);}

4.2、结构收集到数组中

Stream中提供了toArray方法来将结果放到一个数组中,返回值类型是Object[]。
如果我们要指定返回的类型,那么可以使用另一个重载的toArray(IntFunction f)方法。

    @Testpublic void test02(){//收集结果到数组中,数据类型是ObjectObject[] objects = Stream.of("aaa","bbb","ccc").toArray();System.out.println(Arrays.toString(objects));//收集结构到数组中,数据类型是StringString[] strings = Stream.of("aaa","bbb","ccc").toArray(String[]::new);System.out.println(Arrays.toString(strings));}

4.3、对流中数据做聚合操作

当我们使用Stream流处理数据后,可以根据某个属性将数据分组

    @Testpublic void test03(){//获取年龄最大值List<Person> personList= Arrays.asList(new Person("张三",20),new Person("李四",25),new Person("王五",30),new Person("赵六",35));Optional<Person> maxAgePerson = personList.stream()//.collect(Collectors.maxBy(Comparator.comparingInt(Person::getAge)));.collect(Collectors.maxBy((p1,p2)->p1.getAge()-p2.getAge()));System.out.println("最大年龄: "+maxAgePerson.get());//获取年龄最小值Optional<Person> minAgePerson = personList.stream().collect(Collectors.minBy((p1,p2)->p1.getAge()-p2.getAge()));System.out.println("最小年龄: "+minAgePerson.get());//求所有人的年龄之和Integer sumAge = personList.stream().collect(Collectors.summingInt(Person::getAge));System.out.println("年龄总和:" + sumAge);//求所有人的年龄平均值Double avgAge = personList.stream().collect(Collectors.averagingInt(Person::getAge));System.out.println("年龄的平均值:" + avgAge);//统计数量Long count = personList.stream().filter(p-> p.getAge() > 20).collect(Collectors.counting());System.out.println("满足条件的记录数:" + count);}

4.4、对流中数据做分组操作

当我们使用Stream流处理数据后,可以根据某个属性将数据分组

    public void test04(){List<Person> personList= Arrays.asList(new Person("张三", 18, 175), new Person("李四", 22, 177), new Person("张三", 14, 165), new Person("李四", 15, 166), new Person("张三", 19, 182));//根据姓名对数据分组Map<String,List<Person>> map1= personList.stream().collect(Collectors.groupingBy(Person::getName));map1.forEach((k,v)-> System.out.println("k=" + k +"\t"+ "v=" + v));System.out.println("-----------");Map<String, List<Person>> map2 = personList.stream().collect(Collectors.groupingBy(p -> p.getAge() >= 18 ? "成年" : "未成年"));map2.forEach((k,v)-> System.out.println("k=" + k +"\t"+ "v=" + v));}

多级分组

    @Testpublic void test05(){List<Person> personList= Arrays.asList(new Person("张三", 16, 175), new Person("李四", 22, 177), new Person("张三", 14, 165), new Person("李四", 15, 166), new Person("张三", 19, 182));//先根据name分组,然后根据age(成年和未成年)分组Map<String,Map<Object,List<Person>>> map= personList.stream().collect(Collectors.groupingBy(Person::getName,Collectors.groupingBy(p-> p.getAge() >= 18 ? "成年" : "未成年")));map.forEach((k,v)->{System.out.println("k=" + k);v.forEach((kk,vv) -> System.out.println("\t"+"kk="+kk+"\tvv="+vv));});

4.5、对流中数据做分区操作

Collectors.partitioningBy会根据值是否为true,把集合中的数据分割为两个列表,一个true列表,一个
false列表

    @Testpublic void test06(){List<Person> personList= Arrays.asList(new Person("张三", 16, 175), new Person("李四", 22, 177), new Person("张三", 14, 165), new Person("李四", 15, 166), new Person("张三", 19, 182));Map<Boolean, List<Person>> map = personList.stream().collect(Collectors.partitioningBy(p -> p.getAge() > 18));map.forEach((k,v)-> System.out.println(k+"\t" + v));}

4.6、对流中数据做拼接

Collectors.joining会根据指定的连接符,将所有的元素连接成一个字符串

    @Testpublic void test07(){List<Person> personList= Arrays.asList(new Person("张三", 18, 175), new Person("李四", 22, 177), new Person("张三", 14, 165), new Person("李四", 15, 166), new Person("张三", 19, 182));String s1 = personList.stream().map(Person::getName).collect(Collectors.joining());// 张三李四张三李四张三System.out.println(s1);String s2 = personList.stream().map(Person::getName).collect(Collectors.joining("_"));// 张三_李四_张三_李四_张三System.out.println(s2);String s3 = personList.stream().map(Person::getName).collect(Collectors.joining("_", "###", "$$$"));// ###张三_李四_张三_李四_张三$$$System.out.println(s3);}

五、并行Stream流

5.1、串行的Stream流

我们前面使用的Stream流都是串行,也就是在一个线程上面执行。

    @Testpublic void test01(){long count = Stream.of(5,6,7,4,2,1,9).filter(s ->{System.out.println(Thread.currentThread().getName() + ":" + s);return s > 3;}).count();System.out.println(count);}

5.2、并行流

parallelStream其实就是一个并行执行的流,它通过默认的ForkJoinPool,可以提高多线程任务的速
度。

5.2.1、获取并行流

    @Testpublic void test02(){List<Integer> list=new ArrayList<>();//通过list接口直接获取并行流Stream<Integer> stram=list.parallelStream();//将已有的串行流转换为并行流Stream<Integer> parallel=Stream.of(1,2,3).parallel();}

5.2.3、并行流操作

    @Testpublic void test03(){long count = Stream.of(5,6,7,4,2,1,9).parallel().filter(s ->{System.out.println(Thread.currentThread().getName() + ":" + s);return s > 3;}).count();System.out.println(count);}

5.3、并行流和串行流对比

我们通过for循环,串行Stream流,并行Stream流来对500000000亿个数字求和。来看消耗时间

public class StresmTest24 {private static long times=500000000;private long start;@Beforepublic void before(){start=System.currentTimeMillis();}@Afterpublic void end(){long end=System.currentTimeMillis();System.out.println("消耗时间:"+(end - start));}@Testpublic void test01(){System.out.println("普通for循坏:");long res= 0;for (int i = 0;i<times;i++){res+=1;}}@Testpublic void test02(){System.out.println("串行流:serialStream");LongStream.rangeClosed(0,times).reduce(0,Long::sum);}@Testpublic void test03(){LongStream.rangeClosed(0,times).parallel().reduce(0,Long::sum);}}

5.4、线程安全

在多线程的处理下,肯定会出现数据安全问题。如下:

    @Testpublic void test01(){List<Integer> list = new ArrayList<>();for (int i = 0; i < 1000; i++) {list.add(i);}System.out.println(list.size());List<Integer> listNew =new ArrayList<>();list.parallelStream().forEach(listNew::add);System.out.println(listNew.size());}

这个会抛出异常

	java.lang.ArrayIndexOutOfBoundsException

针对这个问题,我们的解决方案有哪些呢?

  1. 加同步锁
  2. 使用线程安全的容器
  3. 通过Stream中的toArray/collect操作
    实现:
@Testpublic void test02(){List<Integer> listNew = new ArrayList<>();Object obj = new Object();IntStream.rangeClosed(1,1000).parallel().forEach(i->{synchronized (obj){listNew.add(i);}});System.out.println(listNew.size());}@Testpublic void test03(){Vector v = new Vector();Object obj = new Object();IntStream.rangeClosed(1,1000).parallel().forEach(i->{synchronized (obj){v.add(i);}});System.out.println(v.size());}@Testpublic void test04(){List<Integer> listNew = new ArrayList<>();// 将线程不安全的容器包装为线程安全的容器List<Integer> synchronizedList = Collections.synchronizedList(listNew);Object obj = new Object();IntStream.rangeClosed(1,1000).parallel().forEach(i->{synchronizedList.add(i);});System.out.println(synchronizedList.size());}@Testpublic void test05(){List<Integer> listNew = new ArrayList<>();Object obj = new Object();List<Integer> list = IntStream.rangeClosed(1, 1000).parallel().boxed().collect(Collectors.toList());System.out.println(list.size());}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.pswp.cn/web/82963.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

split方法

在编程中&#xff0c;split 方法通常用于将字符串按照指定的分隔符拆分成多个部分&#xff0c;并返回一个包含拆分结果的列表&#xff08;或数组&#xff09;。不同编程语言中的 split 方法语法略有不同&#xff0c;但核心功能相似。以下是常见语言中的用法&#xff1a; ​1. P…

深入理解 x86 汇编中的符号扩展指令:从 CBW 到 CDQ 的全解析

引入 在汇编语言的世界里&#xff0c;数据宽度的转换是一项基础却至关重要的操作。尤其是在处理有符号数时&#xff0c;符号扩展&#xff08;Sign Extension&#xff09;作为保持数值符号一致性的核心技术&#xff0c;直接影响着运算结果的正确性。本文将聚焦 x86 架构中最常用…

计算机基础知识(第五篇)

计算机基础知识&#xff08;第五篇&#xff09; 架构演化与维护 软件架构的演化和定义 软件架构的演化和维护就是对架构进行修改和完善的过程&#xff0c;目的就是为了使软件能够适应环境的变化而进行的纠错性修改和完善性修改等&#xff0c;是一个不断迭代的过程&#xff0…

前端开发三剑客:HTML5+CSS3+ES6

在前端开发领域&#xff0c;HTML、CSS和JavaScript构成了构建网页与Web应用的核心基础。随着技术标准的不断演进&#xff0c;HTML5、CSS3以及ES6&#xff08;ECMAScript 2015及后续版本&#xff09;带来了诸多新特性与语法优化&#xff0c;极大地提升了开发效率和用户体验。本文…

c++ 头文件

目录 防止头文件重复包含 头文件的作用 如何让程序的多个 .cpp 文件之间共享全局变量&#xff08;可能是 int、结构体、数组、指针、类对象&#xff09;? 防止头文件重复包含 为什么要防止头问件重复包含&#xff1f; 当然一般也不会把变量定义放到头问件&#xff0c;那为…

深入解析 JavaScript 中 var、let、const 的核心区别与实践应用

一、历史背景与语法基础 JavaScript 作为动态弱类型语言&#xff0c;变量声明机制经历了从 ES5 到 ES6 的重大变革。在 ES5 及更早版本中&#xff0c;var 是唯一的变量声明方式&#xff0c;而 ES6&#xff08;2015 年&#xff09;引入了 let 和 const&#xff0c;旨在解决 var…

【Linux庖丁解牛】—自定义shell的编写!

1. 打印命令行提示符 在我们使用系统提供的shell时&#xff0c;每次都会打印出一行字符串&#xff0c;这其实就是命令行提示符&#xff0c;那我们自定义的shell当然也需要这一行字符串。 这一行字符串包含用户名&#xff0c;主机名&#xff0c;当前工作路径&#xff0c;所以&a…

应用案例 | 设备分布广, 现场维护难? 宏集Cogent DataHub助力分布式锅炉远程运维, 让现场变“透明”

在日本&#xff0c;能源利用与环保问题再次成为社会关注的焦点。越来越多的工业用户开始寻求更高效、可持续的方式来运营设备、管理能源。而作为一家专注于节能与自动化系统集成的企业&#xff0c;日本大阪的TESS工程公司给出了一个值得借鉴的答案。 01 锅炉远程监控难题如何破…

【OSG学习笔记】Day 16: 骨骼动画与蒙皮(osgAnimation)

骨骼动画基础 骨骼动画是 3D 计算机图形中常用的技术&#xff0c;它通过以下两个主要组件实现角色动画。 骨骼系统 (Skeleton)&#xff1a;由层级结构的骨头组成&#xff0c;类似于人体骨骼蒙皮 (Mesh Skinning)&#xff1a;将模型网格顶点绑定到骨骼上&#xff0c;使骨骼移动…

jdk同时安装多个版本并自由切换

一、安装不同版本的JDK 二、配置环境变量&#xff08;多版本JDK&#xff09; 1. 新建版本专用环境变量&#xff08;用于切换&#xff09; 操作位置&#xff1a;系统变量 > 新建 变量名&#xff1a;JAVA_HOME_1.8 变量值&#xff1a;JDK 8安装路径变量名&#xff1a;JAVA1…

java中装饰模式

目录 一 装饰模式案例说明 1.1 说明 1.2 代码 1.2.1 定义数据服务接口 1.2.2 定义基础数据库服务实现 1.2.3 日志装饰器 1.2.4 缓存装饰器 1.2.5 主程序调用 1.3 装饰模式的特点 一 装饰模式案例说明 1.1 说明 本案例是&#xff1a;数据查询增加缓存&#xff0c;使用…

【论文阅读】YOLOv8在单目下视多车目标检测中的应用

Application of YOLOv8 in monocular downward multiple Car Target detection​​​​​ 原文真离谱&#xff0c;文章都不全还发上来 引言 自动驾驶技术是21世纪最重要的技术发展之一&#xff0c;有望彻底改变交通安全和效率。任何自动驾驶系统的核心都依赖于通过精确物体检…

在uni-app中如何从Options API迁移到Composition API?

uni-app 从 Options API 迁移到 Composition API 的详细指南 一、迁移前的准备 升级环境&#xff1a; 确保 HBuilderX 版本 ≥ 3.2.0项目 uni-app 版本 ≥ 3.0.0 了解 Composition API 基础&#xff1a; 响应式系统&#xff1a;ref、reactive生命周期钩子&#xff1a;onMount…

408第一季 - 数据结构 - 图

图的概念 完全图 无向图的完全图可以这么想&#xff1a;如果有4个点&#xff0c;每个点都会连向3个点&#xff0c;每个点也都会有来回的边&#xff0c;所以除以2 有向图就不用除以2 连通分量 不多解释 极大连通子图的意思就是让你把所有连起来的都圈出来 强连通图和强连通…

31.2linux中Regmap的API驱动icm20608实验(编程)_csdn

regmap 框架就讲解就是上一个文章&#xff0c;接下来学习编写的 icm20608 驱动改为 regmap 框架。 icm20608 驱动我们在之前的文章就已经编写了&#xff01; 因为之前已经对icm20608的设备树进行了修改&#xff0c;所以大家可以看到之前的文章&#xff01;当然这里我们还是带领…

Vue速查手册

Vue速查手册 CSS deep用法 使用父class进行限定&#xff0c;控制影响范围&#xff1a; <template><el-input class"my-input" /> </template><style scoped> /* Vue 3 推荐写法 */ .my-input :deep(.el-input__inner) {background-color…

振动力学:无阻尼多自由度系统(受迫振动)

本文从频域分析和时域分析揭示系统的运动特性&#xff0c;并给出系统在一般形式激励下的响应。主要讨论如下问题&#xff1a;频域分析、频响函数矩阵、反共振、振型叠加法等。 根据文章1中的式(1.7)&#xff0c;可知无阻尼受迫振动的初值问题为&#xff1a; M u ( t ) K u …

真实案例分享,Augment Code和Cursor那个比较好用?

你有没有遇到过这种情况&#xff1f;明明知道自己想要什么&#xff0c;写出来的提示词却让AI完全理解错了。 让AI翻译一篇文章&#xff0c;结果生成的中文不伦不类&#xff0c;机器僵硬&#xff0c;词汇不同&#xff0c;鸡同鸭讲。中国人看不懂&#xff0c;美国人表示耸肩。就…

zotero及其插件安装

zotero官网&#xff1a;Zotero | Your personal research assistant zotero中文社区&#xff1a;快速开始 | Zotero 中文社区 插件下载镜像地址&#xff1a;Zotero 插件商店 | Zotero 中文社区 翻译&#xff1a;Translate for Zotero 接入腾讯翻译API&#xff1a;总览 - 控制…

【SSM】SpringMVC学习笔记8:拦截器

这篇学习笔记是Spring系列笔记的第8篇&#xff0c;该笔记是笔者在学习黑马程序员SSM框架教程课程期间的笔记&#xff0c;供自己和他人参考。 Spring学习笔记目录 笔记1&#xff1a;【SSM】Spring基础&#xff1a; IoC配置学习笔记-CSDN博客 对应黑马课程P1~P20的内容。 笔记2…