第二十课:文件复制,函数式编程,Stream流

文件复制,函数式编程,Stream流

文件复制

文件复制:BufferedInputStream/BufferedOutputStream
文件内容读写:BufferedReader/PrintWriter
FileReader/FileWriter
对象读写:ObjectInputStream/ObjectOutputStream
属性集: Properties, 仅支持String类型的属性映射
extends Hashtable implements Map,key - value
方法:void setProperty(String key, String value)
String getProperty(String key)
加载属性集::void load(Reader)
void load(InputStream)
下面展示一些 属性集代码片

public  void test01() throws IOException {
    Properties p = new Properties();
    String path = test.class.getClassLoader().getResource("properties/a.txt").getPath();
    p.load(new FileInputStream(path));
    p.setProperty("url","http://www.baidu.com");
    String url = p.getProperty("url");
    System.out.println(url);
    p.setProperty("name","zangsan");
    String name = p.getProperty("name");
    System.out.println(name);
}

函数式编程

函数式编程:Lambda表达式(函数式接口作为方法的参数)
函数式接口:接口中只有一个抽象方法
常用函数式接口:Supplier Consumer Predicate Function
Supplier: 生产者 - T get();
下面展示一些 Supplier

public class SupplierTest {
    public static String getInstance(Supplier<String> sup){
        return sup.get();
    }

    public static void main(String[] args) {
        String str = getInstance(()->{
                return "hello";
        });
        System.out.println(str);

}
}

Consumer: 消费者 - void accept(T t);
默认方法 - andThen(Consumer)
将两个消费方式组合在一块
下面展示一些 Consumer代码片

public class ConsumerTest {
    public static void test(String str, Consumer<String> first,Consumer<String> second){
        first.andThen(second).accept(str);
    }

    public static void main(String[] args) {
        test("PythonGood",  s-> {
            System.out.println(s.toUpperCase());
        },  s-> {
            System.out.println(s.toLowerCase());
        });
    }
}

Predicate: 对对象做判断 - boolean test(T t);
默认方法 - or(||) and(&&) negate(!)
下面展示一些 Predicate

public class PredicateTest {
    public static void test(String str, Predicate<String> pre1, Predicate<String> pre2){
        boolean b = pre1.and(pre2).test(str);
        System.out.println(b);
    }

    public static void main(String[] args) {
        test("tongyao",  (s)-> {
                return s.length() > 1;
            }
        , (s)-> {
                return s.startsWith("tong");
            });
    }
}

Function<T, R>: 类型转换 - R apply(T t);
默认方法 - andThen(Function)
连续做两种类型转换

下面展示一些 Function代码片

public class PredicateTest {
    public static void test(String str, Predicate<String> pre1, Predicate<String> pre2){
        boolean b = pre1.and(pre2).test(str);
        System.out.println(b);
    }

    public static void main(String[] args) {
        test("tongyao",  (s)-> {
                return s.length() > 1;
            }
        , (s)-> {
                return s.startsWith("tong");
            });
    }
}

Stream流

获取流: 集合 Collection Map,数组
常用得API: void forEach(Consumer) - 终结方法
Stream filter(Predicate) - 延迟方法
Predicate中test返回true, 是保留在流中的
Stream map(Function<T, R>) - 延迟方法
将 流中的 T类型的数据, 转成 R类型数据, 并且存入新的流中
static Stream concat(Stream, Stream)
将两个流拼接成一个

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值