属性集: Properties、函数式编程、Stream流

一。属性集: 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 class DemoProperties {
    @Test
    public void test01() throws IOException {
        // 1.创建属性集对象
        Properties pro = new Properties();
        // "root" "root"
        // 2.通过流加载属性集
        /*String path = DemoProperties.class.getClassLoader().getResource("config.properties").getPath();
        pro.load(new FileInputStream(path));*/
        InputStream is = DemoProperties.class.getClassLoader().getResourceAsStream("config.properties");
        //加载属性集:
		pro.load(is);

        // 3.获得属性集中的value值
        String name = pro.getProperty("name");
        System.out.println(name);

        // 4.直接给属性集设置属性
        pro.setProperty("url", "http://www.baidu.com");

        String url = pro.getProperty("url");
        System.out.println(url);

        String username = pro.getProperty("username");
        System.out.println(username);
        /*username = new String(username.getBytes("ISO8859-1"), "UTF-8");
        System.out.println(username);*/
    }
}
//创建配置文件config.properties
\\key = value
name = mysql
password = 12345
username = \u5F20\u4E09

二。函数式接口:接口中只有一个抽象方法
函数式编程: Lambda表达式(函数式接口作为方法的参数)
常用函数式接口: Supplier Consumer Predicate Function

  1. Supplier: 生产者 - T get();
public class Demo01 {
    /*
    为了使用Lambda表达式, 所以将Supplier作为方法参数使用
    方法的目的: 为了返回一个字符串对象
     */
    public static String getInstance(Supplier<String> sup) {
        // 调用Supplier 的get方法
        return sup.get();//get()方法没有参数
    }
    public static void main(String[] args) {
        // 调用getInstance方法, 需要传递一个Supplier接口实现类
        // 又因为Supplier接口是函数式接口, 所以可以使用Lambda表达式
        String str = getInstance(() -> {
            // 这是生产字符串的过程
            //生产过程,就是生产出一个返回的东西
            return "hello";});
        System.out.println(str);
    }
}
-----------------------------------------
public class Demo02Test {
    /**
     * 传入一个数组, 获得数组的最大值并返回
     * 还要使用Supplier这个接口
     */
    public static int getMax(Supplier<Integer> sup) {
        return sup.get();
    }

    public static void main(String[] args) {
        int[] arr = {98, 23, 16, 34, 72};
        int m = getMax(() -> {
            int max = 0;
            for(int i : arr) {
                if (max < i) {
                    max = i;
                }
            }
            return max;
        });
        System.out.println(m);
    }
}

2.Consumer: 消费者 - void accept(T t); 使用这个对象
默认方法 - andThen(Consumer)
将两个消费方式组合在一块

public class Consumer1 {
    public static void main(String[] args) {
        String xx = "Hello";
        consumeString(xx,//消费过程就是对传入的xx进行操作
                s -> System.out.println(s)
        );

        String mm = "www.9090";
        andThenConsume(mm,
                s -> System.out.println(s.split("\\.")[0]),//消费过程
                s -> System.out.println(s.split("\\.")[1])
        );
    }
    //accept()
    public static void consumeString(String xx, Consumer<String> function) {//泛型类型是消费对象的类型
        //调用接口函数方法
        function.accept(xx);
    }
    //andThen()
    public static void andThenConsume(String mm, Consumer<String> s1, Consumer<String> s2) {//andThen构造方法
        s1.andThen(s2).accept(mm);
    }
}

---------------------------------------------------------
public class Counsum2 {
 /**
     * 请按照格式“ 姓名:XX。性别:XX。 ”的格式将信息打印出 来
     * str: "迪丽热巴,女"
     * 第一次消费: 姓名:迪丽热巴.
     * 第二次消费: 性别:女.
     */
    public static void main(String[] args) {
        String[] arry={"迪丽热巴,女","古力娜扎,女","马儿扎哈,男"};
        printInfor(arry,
                s-> {System.out.println("姓名:"+s.split(",")[0]+"."); },
                s-> {System.out.println("性别:"+s.split(",")[1]+".");}
                );
    }
    public static void printInfor(String[] arry, Consumer<String>s1,Consumer<String>s2){
        for(String str:arry){
            s1.andThen(s2).accept(str);
            /*相当于
            s1.accept(str);
            s2.accept(str);*/
        }
    }
}

3.Predicate: 对对象做判断 - boolean test(T t);
默认方法 - or(||) and(&&) negate(!)

public class demo1 {
    public static void main(String[] args) {
        //test
        testPredicate("看看看看",s->{
            return s.length()==4;//判断过程
                }
                );
        //and
        andPredicate("电话卡",
                s -> {return s.length()==3;
                },
                s -> {return s.contains("电");}
                );
        //or
        orPredicate("悬浮 悟空",
                s -> {return s.contains(" ");},
                s -> {return s.contains("d");}
                );
        //negate
       negatePredicate("悬浮 悟空",
                s -> {return s.contains(" ");}
        );
    }
    public static void testPredicate(String str, Predicate<String> pri){
        //test
        boolean b=pri.test(str);
        System.out.println(b);
    }
    public static void andPredicate(String str,Predicate<String> p1,Predicate<String> p2){
        //and
        boolean b=p1.and(p2).test(str);
        //相当于p1.test(str)&&p2.test(str);
        System.out.println(b);
    }
    public static void orPredicate(String str,Predicate<String> p1,Predicate<String> p2){
        //or
        boolean b=p1.or(p2).test(str);
        //相当于p1.test(str)||p2.test(str);
        System.out.println(b);
    }
    public static void negatePredicate(String str,Predicate<String> p1){
        //negate
        boolean b=p1.negate().test(str);
        System.out.println(b);
    }
}
------------------------------------------------------------
public class Demo2 {
/**
 * 数组当中有多条“姓名+性别”的信息如下,
 * { "迪丽热巴,女", "古力娜扎,女", "⻢尔扎哈,男", "赵丽颖,女" };
 * 请通过 Predicate 接口的拼装将符合要求的字符串筛选到集合 ArrayList 中,
 * 需要同时满足两个条件:
 * 1. 必须为女生;
 * 2. 姓名为4个字。
 */
    public static void main(String[] args) {
        String[] arr = {"迪丽热巴,女", "古力娜扎,女", "马儿扎哈,男", "赵丽颖,女"};
        List<String> list = new ArrayList<>();
        list = filter(arr,
                //判断过程
                s -> s.split(",")[1].equals("女"),

                s -> s.split(",")[0].length() == 4
        );
        System.out.println(list);
    }

    public static List<String> filter(String[] arr, Predicate<String> p1, Predicate<String> p2) {
        List<String> list = new ArrayList<>();

        for (String str : arr) {
            if (p1.and(p2).test(str)) {
                list.add(str);
            }
        }
        return list;
    }
}
  1. Function<T, R>: 类型转换 - R apply(T t);
    默认方法 - andThen(Function)
    连续做两种类型转换
public class Function1 {//类型转换
    public static void main(String[] args) {
        //apply
        String str="321";
        applyFunction(str,s ->
            Integer.parseInt(str)
                );
        //andthen
        String str2="5622";
        andThenFunction(str,
                b -> Integer.parseInt(str2),
                b -> String.valueOf(b)
        );
    }
    public static void applyFunction(String str,Function<String,Integer> fun){
        //apply
        int num=fun.apply("str");
        System.out.println(num);
    }
    public static void andThenFunction(String str,Function<String,Integer> b1,Function<Integer,String> b2){
        //andThen
        String numstr=b1.andThen(b2).apply(str);
        System.out.println(numstr);
    }
}
-------------------------------------------------
public class Function2 {
/**
 * String str = "赵丽颖, 20";
 * 1. 将字符串截取数字年龄部分,得到字符串;
 *      "赵丽颖, 20" -> "20"   Function<String, String>
 * 2. 将上一步的字符串转换成为int类型的数字;
 *      "20" -> 20     Function<String, Integer>
 * 3. 将上一步的int数字累加100,得到结果int数字。
 *      20 -> 120      Function<Integer, Integer>
 */
    public static void main(String[] args) {
       change("赵丽颖, 20",
               // "赵丽颖, 20" -> "20"
               s -> s.split("[, ]+")[1],
               // "20" -> 20
               s -> Integer.valueOf(s),
               // 20 -> 120
               i -> i + 100 );
    }
public static void change(String str, Function<String, String> fun1, Function<String, Integer> fun2, Function<Integer, Integer> fun3) {
        int i = fun1.andThen(fun2).andThen(fun3).apply(str);
        System.out.println(i);
    }
}

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

public class Demo02GetStream {
    public static void main(String[] args) {
        // 1.单列集合获得流对象 List Set
        // List -> Stream
        List<String> list = List.of("喜羊羊", "美羊羊","懒洋洋","暖洋洋");
        Stream<String> streamList = list.stream();
        // Set -> Stream
        Set<String> set = Set.of("张无忌", "张三丰", "张翠山", "张飞");
        Stream<String> streamSet = set.stream();

        // 2.多列集合 Map
        Map<Integer, String> map = Map.of(1, "张飞", 2, "关羽", 3, "刘备", 4, "周瑜");
        // 获得map中的key
        Set<Integer> keys = map.keySet();
        Stream<Integer> streamKey = keys.stream();
        // 获得map中 key-value对 entry
        Set<Map.Entry<Integer, String>> entries = map.entrySet();
        Stream<Map.Entry<Integer, String>> streamEntry = entries.stream();
        // 获得value部分
        Collection<String> values = map.values();
        Stream<String> streamValues = values.stream();

        // 3.数组获得Stream流的方式
        Integer[] arrInt = {1,2,3,4,5};
        Stream<Integer> streamInt = Stream.of(arrInt);

        // 4.传入数组的方式可以使用可变长参数替代
        Stream<Integer> streamInt2 = Stream.of(1, 2, 3, 4, 5);

    }
}
public class StreamText {
    public static void main(String[] args) {
        ArrayList<String> one = new ArrayList<>();
        one.add("迪丽热巴");
        one.add("宋远桥");
        one.add("苏星河");
        one.add("石破天");
        one.add("石中玉");
        one.add("老子");
        one.add("庄子");
        one.add("洪七公");
        ArrayList<String> two=new ArrayList<>();
        two.add("古力娜扎");
        two.add("张无忌");
        two.add("赵丽颖");
        two.add("张三丰");
        two.add("尼古拉斯赵四");
        two.add("张天爱");
        two.add("张二狗");
        //第一个队伍名字为三个字的成员姓名
        //第一队筛选后只要前三个
        Stream<String> streamOne=one.stream().filter(s->s.length()==3).limit(3);
        //第二个队伍只要姓张的
        //第二队筛选后前两个不要
        Stream<String> streamTwo=two.stream().filter(s->s.startsWith("张")).skip(2);
        //将两个队伍合并成一个
        Stream<String>newstream=Stream.concat(streamOne,streamTwo);
        //根据姓名创建Person对象
        Stream<Person> personStream=newstream.map(s -> new Person(s));
        //打印整个队伍的Person对象信息
        personStream.forEach(person -> System.out.println(person));// forEach 是一个终结方法

        /*合并队伍,创建对象,打印信息升级版
        Stream.concat(streamOne,streamTwo).map(Person::new).forEach(System.out
        ::println);
         */
    }
}
----------------------------------
public class Person {
    private String name;

    public Person() {
    }

    public Person(String name) {
        this.name = name;
    }
    ...
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值