ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

2022-08-08 第二小组 张鑫 学习笔记

2022-08-13 17:04:46  阅读:127  来源: 互联网

标签:stream 张鑫 08 System collect 2022 println public out


实训三十天 单元测试

1.学习重点

1.JUnit单元测试
2.Stream类方法
3.IO流前述
4.File类

2.学习内容

JUnit单元测试

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

JUnit单元测试的好处

1.可以书写一些列的测试方法,对项目的所有的接口或者方法进行单元测试。
2.启动后,自动化的测试。
3.只需要查看最后的结果。
4.每个单元测试的用例相对独立,由JUnit启动。
5.添加,删除,屏蔽测试方法

测试方法
1.不能有返回值
2.不能有参数列表
3.必须有Test注解

 @Test
    public void test01(){
        System.out.println("hello junit");
    }

    @Test
    public void test02() {
        System.out.println("hello junit02");
    }
JUnit断言

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

1.assertEquals:检查两个变量或等式是否平衡
2.assertTrue:检查条件是否为真
3.assertFalse:检查条件是否为假
4.assertNotNull:检查对象是否不为空
5.assertNull:检查对象是否为空

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

JUnit注解
1.Test
2.Before:在测试方法执行之前执行的方法
3.After
命名规范

单元测试类的命名:被测试类的类名 + Test
测试方法的命名:test + 被测试方法的方法名

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

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

    @After
    public void testAfter() {
        System.out.println("after方法执行...");
    }
使用一个流时的三个步骤
1.获取一个数据源
2.执行操作获取想要的结果
3.每次操作,原有的流对象不改变,返回一个新的Stream对象
Stream有几个特性
1.Stream不存储数据,一般会输出结果
2.Stream不会改变数据源,通常情况下会生成一个新的集合
3.Stream具有延迟执行的特性,只有调用终端操作时,中间操作才会执行
综合案例
@Test
public void test01() {
    List<String> list = Arrays.asList("a","b","c");
    // 创建一个顺序流
    Stream<String> stream = list.stream();
    // 创建一个并行流
    Stream<String> parallelStream = list.parallelStream();
    
    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);


    }

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

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

工具类-File操作文件的类
文件的路径
正斜杠:左斜杠,撇,/
反斜杠:右斜杠,捺,\
在Unix/Linux,路径的分隔采用正斜杠/,
在windows中,路径分隔采用反斜杠\。
在java中,\代表转义
在File类中,定义了路径分隔符的常量,自动识别操作系统。
File类的构造器
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);
    }
    public static boolean createDirAndFile(String filepath) throws IOException {
        File file = null;
        if(filepath.contains("/")){
            if(filepath.contains("\\")){
                throw new RuntimeException("路径不合法");
            }else {
                // e:\\a\\b\\c\\a.txt
                int index = filepath.lastIndexOf("/");
                String filename = filepath.substring(index,filepath.length());
                filepath = filepath.substring(0, index+ 1);
                file = new File(filepath);
                file.mkdirs();
                // 创建文件
                File newFile = new File(filepath,filename);
                newFile.createNewFile();
                return true;
            }
        }
        if(filepath.contains("\\")){
            if(filepath.contains("/")){
                throw new RuntimeException("路径不合法");
            }else {
                // e:\\a\\b\\c\\a.txt
                int index = filepath.lastIndexOf("\\");
                String filename = filepath.substring(index,filepath.length());
                filepath = filepath.substring(0, index+ 1);
                file = new File(filepath);
                file.mkdirs();
                // 创建文件
                File newFile = new File(filepath,filename);
                newFile.createNewFile();
                return true;
            }
        }

        throw new RuntimeException("路径不合法");
    }

标签:stream,张鑫,08,System,collect,2022,println,public,out
来源: https://www.cnblogs.com/zxscj/p/16583442.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有