20220808 第五组 罗大禹 学习笔记

20220808 第五小组 罗大禹 学习笔记

Java 单元测试、流及IO流前言

学习重点

1.JUnit单元测试
2.流式编程(Stream编程)
3.IO流前言

学习内容

JUnit单元测试

JUnit单元测试

JUnit是一个Java语言单元测试框架。

JUnit单元测试的好处:
  1. 可以书写一些列的测试方法,对项目的所有的接口或者方法进行单元测试。

  2. 启动后,自动化的测试。

  3. 只需要查看最后的结果。

  4. 每个单元测试的用例相对独立,由JUnit启动。

  5. 添加,删除,屏蔽测试方法。

jar包

​ 如果要引入第三方的插件,xxx.jar的文件。

​ 首先要把这个文件导入到我们的工程目录下

​ 其次,要添加到工程的依赖目录中

JUnit注解

是JUnit提供的一个单元测试注解

如果你的工程没有导入JUnit的jar包,idea是不认识JUnit的注解的

1.Test注解:
  1. Test:目的是为了测试此方法,通过@Test注解,我们就能使相应的方法不放在main方法中就可以直接运行,起到代码的测试作用

  2. Before:此注解的目的是为了将@Before 作为首先执行的代码。用于资源的申请在测试方法执行之前执行的方法

  3. After:此注解的目的是为了让每次释放资源之后都会执行此注解下的代码,即使程序出现了异常,依然还会执行这个。用于资源的释放

命名规范:

​ 单元测试类的命名:被测试类的类名 + Test

​ 测试方法的命名:test + 被测试方法的方法名

测试方法:
  1. 不能有返回值
  2. 不能有参数列表
  3. 必须有Test注解
举例说明:
import org.junit.After;
import org.junit.Before;
import org.junit.Test;


public class Ch03 {

    @Test
    public void test01() {
        System.out.println("test01方法执行...");
    }
    @Test
    public void test02() {
        System.out.println("test02方法执行...");
    }

    @Before
    public void testBefore(){
        System.out.println("before方法执行...");
    }

    @After
    public void testAfter() {
        System.out.println("after方法执行...");
    }
}

JUnit断言

JUnit的所有的断言都包含Assert类中,这个类提供了很多有用的断言来编写测试用例。只有失败的断言才会被记录。

注意:

断言不成功会抛异常,即使程序正常运行但是结果不正确,也会以失败结束。

部分断言
  1. assertEquals:检查两个变量或等式是否平衡

  2. assertTrue:检查条件是否为真

  3. assertFalse:检查条件是否为假

    1. assertNotNull:检查对象是否不为空
  4. assertNull:检查对象是否为空

举例说明:
import org.junit.Assert;
import org.junit.Test;

public class Ch02 {
	// 会抛异常
    @Test
    public void test01() {
        Assert.assertTrue(false);
    }
}

集合的面试题:

  1. Hashtable和ConcurrentHashMap性能测试

  2. ArrayList和LinkedList性能测试

数组查询快,插入慢,链表插入快,查询慢

  1. 尾插数组快,链表慢

  2. 遍历,数组快

  3. 头插,链表快,数组慢

  4. 随机删除,如果要过滤,建议用LinkedList

开发中还是以ArrayList为主

流式编程(Stream编程)

Stream编程

JDK8新增

对容器对象功能的增强

我们可以将流看做流水线,这个流水线是处理数据的流水线

当我们使用一个流的时候,通常包括三个步骤:
  1. 获取一个数据源

  2. 执行操作获取想要的结果

  3. 每次操作,原有的流对象不改变,返回一个新的Stream对象

Stream有几个特性:
  1. Stream不存储数据,一般会输出结果

  2. Stream不会改变数据源,通常情况下会生成一个新的集合

  3. Stream具有延迟执行的特性,只有调用终端操作时,中间操作才会执行。

举例说明
import org.junit.Test;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;

public class Ch05 {

    @Test
    public void test01() {
        List<String> list = Arrays.asList("a","b","c");
        // 创建一个顺序流
        Stream<String> stream = list.stream();
        // 创建一个并行流
        Stream<String> parallelStream = list.parallelStream();
		// 创建一个专门操作1 2 3 4 5 6的流
        Stream<Integer> stream1 = Stream.of(1, 2, 3, 4, 5, 6);
		 // iterate迭代 (T seed,接口)	limit(4)长度为4	
        Stream<Integer> stream2 = Stream.iterate(0, (x) -> x + 3).limit(4);


    }
}
Stream方法的大致使用:
对象类:
public class Person {

    private String name;
    private Integer salary;
    private Integer age;
    private String gender;
    private String area;

    public Person() {
    }

    public Person(String name, Integer salary, Integer age, String gender, String area) {
        this.name = name;
        this.salary = salary;
        this.age = age;
        this.gender = gender;
        this.area = area;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getSalary() {
        return salary;
    }

    public void setSalary(Integer salary) {
        this.salary = salary;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getArea() {
        return area;
    }

    public void setArea(String area) {
        this.area = area;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", salary=" + salary +
                ", age=" + age +
                ", gender='" + gender + '\'' +
                ", area='" + area + '\'' +
                '}';
    }
}
应用类:
import org.junit.Before;
import org.junit.Test;

import java.lang.invoke.VarHandle;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Ch06 {

    // 创建一个复杂的集合
    List<Person> personList = new ArrayList<>();
    // 创建一个简单的集合
    List<Integer> simpleList = Arrays.asList(15,22,15,11,33,52,22,14,33,52);

    @Before
    public void before() {
        personList.add(new Person("张三",3000,23,"男","长春"));
        personList.add(new Person("李四",7000,34,"男","西安"));
        personList.add(new Person("王五",5000,22,"女","长春"));
        personList.add(new Person("小明",1000,33,"女","上海"));
        personList.add(new Person("小红",8000,44,"女","北京"));
        personList.add(new Person("小黑",6000,36,"女","南京"));
    }

    @Test
    public void test01(){
        // 打印集合元素
        // 双冒号语法,方法引用
        simpleList.stream().forEach(System.out::println);
        // 其实还可以简化操作
        simpleList.forEach(System.out::println);
    }

    @Test
    public void test02() {
        // 找到第一个元素
        Optional<Integer> first = simpleList.stream().findFirst();

        // 随便找一个
        // 如果没有并行,any也是第一个
        Optional<Integer> any = simpleList.stream().findAny();

        System.out.println("第一个:" + first.get());
        System.out.println("任意一个:" + any.get());
    }

    @Test
    public void test03() {
        // 判断有没有任意一个人年龄大于35岁
        // 任意匹配
        boolean b = personList.stream()
                .anyMatch(item -> item.getAge() > 35);
        System.out.println(b);

        // 判断是不是所有人年龄都大于35岁
        b = personList.stream().allMatch(item -> item.getAge() > 35);
        System.out.println(b);
    }

    @Test
    public void Ch07() {

        List<Integer> collect = simpleList.stream().collect(Collectors.toList());
        System.out.println(collect);
        Set<Integer> collect1 = simpleList.stream().collect(Collectors.toSet());
        System.out.println(collect1);
        Map<Integer, Integer> map = simpleList.stream()
                .collect(Collectors
                        .toMap(item -> item, item -> item + 1));
        System.out.println(map);
    }

    @Test
    public void Ch08() {
        // 统计
        long count = new Random().ints().limit(50).count();
        System.out.println(count);
        OptionalDouble average = new Random().ints().limit(50).average();
        average.ifPresent(System.out::println);
        int sum = new Random().ints().limit(50).sum();
        System.out.println(sum);
    }

    /*
        归约(reduce)缩减,把一个流缩减成一个值,
        可以实现对集合的求和,求乘积,求最值
     */
    @Test
    public void test09(){
        Integer result = simpleList.stream().reduce(1, (n1, n2) -> n1 - n2);
        System.out.println(result);
    }

    @Test
    public void test10(){
        List<String> list = Arrays.asList("A","B","C");
        String string = list.stream().collect(Collectors.joining("-"));
        System.out.println("拼接后的字符串:" + string);
    }

    /*
        分组将集合分为多个map,
        比如员工按性别分组
        员工按薪资是否高于8000分组
     */
    @Test
    public void test11() {
        // 根据工资分组
        Map<Boolean, List<Person>> collect = personList.stream().collect(Collectors.groupingBy(x -> x.getSalary() > 5000));
        System.out.println(collect);
        // 根据性别分组
        Map<String, List<Person>> collect1 = personList.stream().collect(Collectors.groupingBy(Person::getGender));
        System.out.println(collect1);

        // 将员工根据性别分组,再按地区分组
        Map<String, Map<String, List<Person>>> collect2 = personList.stream()
                .collect(Collectors.groupingBy(Person::getGender, Collectors.groupingBy(Person::getArea)));
        System.out.println(collect2);
    }

    /**
     * 筛选
     */
    @Test
    public void test12() {
//        simpleList.stream().filter(item -> item > 17).forEach(System.out::println);

        // 筛选员工中工资大于8000的人,并形成新的集合
        List<Person> collect = personList
                .stream()
                .filter(item -> item.getSalary() > 5000)
                .collect(Collectors.toList());
        System.out.println(collect);
    }

    /**
     * 映射
     * 将一个流的元素按照一定的映射规则映射到另一个流中。
     */
    @Test
    public void test13(){
        // 将员工的工资全部增加1000
//        personList
//                .stream().map(item -> {
//            item.setSalary(item.getSalary() + 1000);
//            return item;
//        }).forEach(System.out::println);
        List<StringBuilder> collect = simpleList.stream().map(item -> {
            StringBuilder strb = new StringBuilder();
            strb.append(item);
            return strb;
        }).collect(Collectors.toList());
        System.out.println(collect);
    }

    /**
     * 排序:sorted
     * 自然排序:
     * 临时排序:
     */
    @Test
    public void test14() {
        // 将员工按工资由低到高排序(自然排序--升序)
        List<String> collect = personList.stream()
                .sorted(Comparator.comparing(Person::getSalary))
                .map(Person::getName)
                .collect(Collectors.toList());
        System.out.println(collect);
        // 按照员工工资的降序
        List<String> collect1 = personList
                .stream()
                .sorted(Comparator.comparing(Person::getSalary).reversed())
                .map(Person::getName)
                .collect(Collectors.toList());
        System.out.println(collect1);
    }

    /**
     * peek操作,调试
     */
    @Test
    public void test15(){
        // 在stream中调试,stream不支持debug
        List<Person> collect = personList.stream()
                .filter(item -> item.getSalary() > 5000)
                .peek(System.out::println)
                .collect(Collectors.toList());
        System.out.println(collect);
    }

    /**
     * 其他操作:合并、去重、限制、跳过。。。。
     */
    @Test
    public void test16() {
        /*
            distinct:去重
            skip:跳过几个数据
            limit:限制使用几个数据
         */
        simpleList
                .stream()
                .distinct()
                .skip(2)
                .limit(6)
                .forEach(System.out::println);


    }

}
JDK8函数式接口

Consumer:消费者 void accept(T t)

Supplier:供应商 T get()

Function: R apply(T t),将一个数据转化成另一个数据

Predicate:断言,boolean test(T t),判断返回值是boolean

JDK8新增:
  1. 函数式接口

  2. 箭头函数(lambda表达式),不是java特有的。阉割版

  3. Optional类:java8为了解决null问题

  4. 断言

  5. Stream编程

optional举例:
package com.jsoft.morning;

import org.junit.Test;

import java.util.Optional;


public class Ch08 {

    @Test
    public void test01() {
        String str = null;
        // 返回一个对象值为空的Optional对象
//        Optional<String> empty = Optional.empty();
//        Optional<String> hello = Optional.of(str);
        Optional<String> o = Optional.ofNullable(null);
        System.out.println(o.get());
    }
}

IO流前言

Java IO流---对于文件的操作

​ Input:把数据从物理内存加载到运行内存。(读文件)

​ Output:把数据从运行内存写到物理内存。(写文件)

java.io包下的类

计算机的输入输出都是通过二进制完成。0和1

工具类:File

操作文件的类

  1. 文件的路径

    正斜杠:左斜杠,撇,/

    反斜杠:右斜杠,捺,\

    在Unix/Linux,路径的分隔采用正斜杠/,

    在windows中,路径分隔采用反斜杠\。

    在java中,\代表转义,因此在写路径时用双反斜杠

    在File类中,定义了路径分隔符的常量,自动识别操作系统。

import java.io.File;

public class Ch01 {

    public static void main(String[] args) {
//        System.out.println("\t");
//        System.out.println("E:\\");
        // 打印输出路径
        System.out.println("E:/workspace");
        // File.separator为\
        System.out.println(File.separator);
        System.out.println("e:" + File.separator + "workspace");
        // File.pathSeparator为;
        System.out.println(File.pathSeparator);
    }
}
File类的构造器
举例说明:
import java.io.File;

public class Ch02 {

    public static void main(String[] args) {
        // file就代表了当前的目录
        File file1 = new File("E:\\workspace\\idea");
        System.out.println("file1 = " + file1);
       	// 父级目录,子目录
        File file2 = new File("E:\\workspace\\idea","aaa");
        System.out.println("file2 = " + file2);
        // 父级目录,子目录
        File file3 = new File(file1,"aaa");
        System.out.println("file3 = " + file3);
    }
}
文件的操作
举例说明:
import org.junit.Test;

import java.io.File;
import java.io.FileFilter;
import java.io.FilenameFilter;
import java.io.IOException;
import java.time.Instant;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Date;


public class Ch03 {

    @Test
    public void test07() {
        File file = new File("E:\\workspace");
        String[] fileNames = file.list();
//        System.out.println(Arrays.toString(fileNames));
        File[] files = file.listFiles();
        System.out.println(Arrays.toString(files));
    }

    @Test
    public void test06() {
        File file = new File("d:/123.txt");
        File file1 = new File("bbb.txt");
        // 返回此抽象路径名的绝对形式。
        File absoluteFile = file1.getAbsoluteFile();
//        System.out.println(file.getParent());
        // 返回此抽象路径名的父,或抽象路径名 null如果此路径名没有指定父目录
        File parentFile = file.getParentFile();
//        System.out.println(file.getName());
//        System.out.println(file1.getPath());
//        System.out.println(new Date(file.lastModified()));
//        System.out.println(file.length());
        // 剪切粘贴 移动
        file.renameTo(new File("d:\\aaa.txt"));
    }

    @Test
    public void test05() throws IOException {
        /*
            绝对路径和相对路径
            绝对路径是有盘名的
            相对路径是没有盘名的
         */
        // 绝对路径
        File file = new File("e:/aaa.txt");
        // 相对路径  在file aaa.txt所在文件查找
        File file2 = new File("/idea");
        // file是否为隐藏文件
        System.out.println(file.isHidden());
        // file是否为普通文件
        System.out.println(file.isFile());
        // file是否为目录
//        System.out.println(file2.isDirectory());
        // 测试这个抽象路径名是否是绝对的
//        System.out.println(file.isAbsolute());
        // 判断文件是否存在
//        System.out.println(file.exists());
        // 新建文件是不会覆盖已有的文件
//        System.out.println(file.createNewFile());
//        System.out.println(file2.mkdirs());
    }



    @Test
    public void test03(){
        File file = new File("e:/hello");
        // 创建一个文件夹,若路径中有多个文件夹不存在,会返回false
        boolean mkdir = file.mkdir();
        // 删除文件夹
        file.delete();
        // 创建一堆文件夹,若路径中有多个文件夹不存在,可以全部创建
        // 若文件夹已存在,会返回false
    }

    @Test
    public void test02(){
        File file = new File("e:\\aaa.txt");
        // 如果存在就删除,如果不存在就拉倒
        // 若删除的为文件夹,这个文件夹必须是空的才能删除掉
        // file类的删除是不走回收站
        boolean delete = file.delete();
        System.out.println(delete);
    }

    @Test
    public void test01() {
        // 创建一个文件
        File file1 = new File("e:\\aaa.txt");
        try {
            file1.createNewFile();
            System.out.println("文件创建成功...");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

posted @   怂还嘴硬  阅读(37)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· DeepSeek在M芯片Mac上本地化部署
点击右上角即可分享
微信分享提示