高效 告别996,开启java高效编程之门 2-10实战:自定义函数式接口

1 有用部分

2    代码演练

 

 

1  有用部分

个人理解:

1.1  关于lambda的应用

通过 @FunctionalInterface注解 将其参数 fileContent  注入,调用的时候可以直接调取到。

服务层通过直接调用接口的方法,没有用实现类

 

1.2  关于文件流的读取操作

demo中设计更多的流的读取的操作

 

 

2  代码演练

需求:

将本地文件打印出来

 

接口:

 

package com.imooc.zhangxiaoxi.lambda.file;

/**
 *  接口中只有一个抽象方法,可以应用lambda表达式
 *
 *  加入注解
 *
 *  文件处理函数式接口
 */
@FunctionalInterface
public interface FileConsumer {

    /**
     * 函数式接口抽象方法
     * @param fileContent
     */
    void fileHandler(String fileContent);

}

 

 

 

文件服务类:

 

package com.imooc.zhangxiaoxi.lambda.file;

import java.io.*;

/**
 * 文件服务类
 */
public class FileService {

    /**
     * 根据url获取文件,打印文件内容
     */
    public void print(String url,FileConsumer fileConsumer) throws IOException {

        //流读取
        BufferedReader bufferedReader = new BufferedReader(
                new InputStreamReader(
                        new FileInputStream(url)));

        //定义行变量和内容sb
        String line =  "";
        StringBuilder stringBuilder = new StringBuilder();

        //循环读取文件内容
        while((line=bufferedReader.readLine())!=null){
            stringBuilder.append(line+"\n");
        }

        //调用函数式接口方法,将文件内容传递给lambda表达式
        fileConsumer.fileHandler(stringBuilder.toString());

    }
}

 

 

 

 

文件测试类:

 

package com.imooc.zhangxiaoxi.lambda.file;

import org.junit.Test;

import java.io.IOException;

public class FileServiceTest {


    @Test
    public void Test() throws IOException {

     //两种方式
   //1.2 FileService fileService = new FileService(); fileService.print("F:/xiangmu3/Xin/test996/src/test/java/com/imooc/zhangxiaoxi/" + "lambda/file/FileServiceTest.java", fileContent -> { System.out.println(fileContent); }); //1.1 /*FileService fileService = new FileService(); fileService.print("F:/xiangmu3/Xin/test996/src/test/java/com/imooc/zhangxiaoxi/" + "lambda/file/FileServiceTest.java", fileContent -> System.out.println(fileContent));*/ } }

 

 

 

打印日志

D:\java\jdk\jdk9\jdk-9+181_windows-x64_ri\java-se-9-ri\jdk-9\bin\java.exe -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:D:\java\devolopKit\idea\anZh\IntelliJ IDEA Community Edition 2018.1.4\lib\idea_rt.jar=30991:D:\java\devolopKit\idea\anZh\IntelliJ IDEA Community Edition 2018.1.4\bin" -Dfile.encoding=UTF-8 -classpath "D:\java\devolopKit\idea\anZh\IntelliJ IDEA Community Edition 2018.1.4\lib\idea_rt.jar;D:\java\devolopKit\idea\anZh\IntelliJ IDEA Community Edition 2018.1.4\plugins\junit\lib\junit-rt.jar;D:\java\devolopKit\idea\anZh\IntelliJ IDEA Community Edition 2018.1.4\plugins\junit\lib\junit5-rt.jar;F:\xiangmu3\Xin\test996\target\test-classes;F:\xiangmu3\Xin\test996\target\classes;F:\xiangmu3\Xin\test996\lib\com\google\guava\guava\28.2-jre\guava-28.2-jre.jar;F:\xiangmu3\Xin\test996\lib\com\google\guava\failureaccess\1.0.1\failureaccess-1.0.1.jar;F:\xiangmu3\Xin\test996\lib\com\google\guava\listenablefuture\9999.0-empty-to-avoid-conflict-with-guava\listenablefuture-9999.0-empty-to-avoid-conflict-with-guava.jar;F:\xiangmu3\Xin\test996\lib\com\google\code\findbugs\jsr305\3.0.2\jsr305-3.0.2.jar;F:\xiangmu3\Xin\test996\lib\org\checkerframework\checker-qual\2.10.0\checker-qual-2.10.0.jar;F:\xiangmu3\Xin\test996\lib\com\google\errorprone\error_prone_annotations\2.3.4\error_prone_annotations-2.3.4.jar;F:\xiangmu3\Xin\test996\lib\com\google\j2objc\j2objc-annotations\1.3\j2objc-annotations-1.3.jar;F:\xiangmu3\Xin\test996\lib\junit\junit\4.12\junit-4.12.jar;F:\xiangmu3\Xin\test996\lib\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar;F:\xiangmu3\Xin\test996\lib\com\alibaba\fastjson\1.2.58\fastjson-1.2.58.jar" com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 -junit4 com.imooc.zhangxiaoxi.lambda.file.FileServiceTest,Test
package com.imooc.zhangxiaoxi.lambda.file;

import org.junit.Test;

import java.io.IOException;

public class FileServiceTest {


    @Test
    public void Test() throws IOException {
        //1.2
        FileService fileService = new FileService();
        fileService.print("F:/xiangmu3/Xin/test996/src/test/java/com/imooc/zhangxiaoxi/" +
                        "lambda/file/FileServiceTest.java",
                fileContent -> {
            System.out.println(fileContent);
        });

       //1.1
        /*FileService fileService = new FileService();
        fileService.print("F:/xiangmu3/Xin/test996/src/test/java/com/imooc/zhangxiaoxi/" +
                "lambda/file/FileServiceTest.java",
                fileContent -> System.out.println(fileContent));*/
    }

}


Process finished with exit code 0

 

上一篇:autofac基础知识


下一篇:[考试总结]noip模拟27