xerga 2 жил өмнө
parent
commit
3e7775369c

+ 21 - 0
JavaSE/day24/day24.iml

@@ -0,0 +1,21 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<module type="JAVA_MODULE" version="4">
+  <component name="NewModuleRootManager" inherit-compiler-output="true">
+    <exclude-output />
+    <content url="file://$MODULE_DIR$">
+      <sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
+    </content>
+    <orderEntry type="inheritedJdk" />
+    <orderEntry type="sourceFolder" forTests="false" />
+    <orderEntry type="module-library">
+      <library name="JUnit4">
+        <CLASSES>
+          <root url="jar://$MAVEN_REPOSITORY$/junit/junit/4.13.1/junit-4.13.1.jar!/" />
+          <root url="jar://$MAVEN_REPOSITORY$/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar!/" />
+        </CLASSES>
+        <JAVADOC />
+        <SOURCES />
+      </library>
+    </orderEntry>
+  </component>
+</module>

+ 21 - 0
JavaSE/day24/src/com/lovecoding/day24/anno01/Column.java

@@ -0,0 +1,21 @@
+package com.lovecoding.day24.anno01;
+
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@Retention(RetentionPolicy.RUNTIME)
+@Target({ElementType.FIELD})
+public @interface Column {
+
+    //java
+    String property();
+
+    //mysql
+    String filed();
+
+
+
+}

+ 27 - 0
JavaSE/day24/src/com/lovecoding/day24/anno01/Person.java

@@ -0,0 +1,27 @@
+package com.lovecoding.day24.anno01;
+
+@Table(value = "tbl_person")
+public class Person {
+
+    @Column(property = "name", filed = "username")
+    private String name;
+
+    @Column(property = "age", filed = "age")
+    private Integer age;
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    public Integer getAge() {
+        return age;
+    }
+
+    public void setAge(Integer age) {
+        this.age = age;
+    }
+}

+ 15 - 0
JavaSE/day24/src/com/lovecoding/day24/anno01/Table.java

@@ -0,0 +1,15 @@
+package com.lovecoding.day24.anno01;
+
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@Retention(RetentionPolicy.RUNTIME)
+@Target({ElementType.TYPE})
+public @interface Table {
+
+    //方法
+    String value();
+}

+ 48 - 0
JavaSE/day24/src/com/lovecoding/day24/anno01/Test01Annocation.java

@@ -0,0 +1,48 @@
+package com.lovecoding.day24.anno01;
+
+import java.lang.reflect.Field;
+import java.util.Arrays;
+import java.util.stream.Stream;
+
+public class Test01Annocation {
+    public static void main(String[] args) {
+
+        //Person.class
+        Class<Person> personClass = Person.class;
+
+        //获取注解
+        Table annotation = personClass.getAnnotation(Table.class);
+
+        String tableName = annotation.value();
+
+        Field[] declaredFields = personClass.getDeclaredFields();
+        //字段
+        String[] cloumn = new String[declaredFields.length];
+
+        //遍历
+        Stream<Field> stream = Arrays.stream(declaredFields);
+
+        for (int i = 0; i < declaredFields.length; i++) {
+            declaredFields[i].setAccessible(true);
+            Column eAnnotation = declaredFields[i].getAnnotation(Column.class);
+            //获取字段
+            cloumn[i] = eAnnotation.filed();
+        }
+
+        StringBuffer sb = new StringBuffer();
+
+        sb.append( " select ");
+
+        for (String s : cloumn) {
+            sb.append(s).append(", ");
+        }
+
+        sb.append(" from ");
+        sb.append(tableName);
+
+        System.out.println(sb.toString());
+
+        //select username age from person
+
+    }
+}

+ 46 - 0
JavaSE/day24/src/com/lovecoding/day24/ex01/Employee.java

@@ -0,0 +1,46 @@
+package com.lovecoding.day24.ex01;
+
+public class Employee  {
+    private String name;
+    private int age;
+    private double salary;
+
+    public Employee(String name, int age, double salary) {
+        this.name = name;
+        this.age = age;
+        this.salary = salary;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    public int getAge() {
+        return age;
+    }
+
+    public void setAge(int age) {
+        this.age = age;
+    }
+
+    public double getSalary() {
+        return salary;
+    }
+
+    public void setSalary(double salary) {
+        this.salary = salary;
+    }
+
+    @Override
+    public String toString() {
+        return "Employee{" +
+                "name='" + name + '\'' +
+                ", age=" + age +
+                ", salary=" + salary +
+                '}';
+    }
+}

+ 56 - 0
JavaSE/day24/src/com/lovecoding/day24/ex01/Test01.java

@@ -0,0 +1,56 @@
+package com.lovecoding.day24.ex01;
+
+import org.junit.Test;
+
+import java.util.HashMap;
+import java.util.function.BiFunction;
+
+public class Test01 {
+
+
+    /*
+     - 声明一个Employee员工类型,包含属性编号、姓名、薪资,属性私有化,提供有参构造,get/set,重写toString。
+
+    - 添加n个员工对象到一个HashMap<Integer,Employee>集合中,其中员工编号为key,员工对象为value。
+    - 调用Map的forEach遍历集合
+    - 调用Map的replaceAll方法,将其中薪资低于10000元的,薪资设置为10000。
+     */
+    @Test
+    public void test01(){
+        HashMap<Integer,Employee> map = new HashMap<>();
+
+        map.put(1001,new Employee("zs",18,900));
+        map.put(1002,new Employee("lis",19,1200));
+        map.put(1003,new Employee("ww",22,1300));
+        map.put(1004,new Employee("zl",34,800));
+
+        //lambda
+        map.forEach( ( integer, employee) -> {
+            System.out.println(integer+","+employee);
+        });
+
+        //Map的replaceAll方法,将其中薪资低于10000元的,薪资设置为10000。
+
+        map.replaceAll( ( i , e ) ->{
+            if (e.getSalary()< 1000){
+                e.setSalary(1000);
+            }
+            return e;
+        });
+
+        map.replaceAll(new BiFunction<Integer, Employee, Employee>() {
+            @Override
+            public Employee apply(Integer integer, Employee employee) {
+                return null;
+            }
+        });
+
+        System.out.println("----------");
+        map.forEach( ( integer, employee) -> {
+            System.out.println(integer+","+employee);
+        });
+
+
+
+    }
+}

+ 24 - 0
JavaSE/day24/src/com/lovecoding/day24/ex02/Person.java

@@ -0,0 +1,24 @@
+package com.lovecoding.day24.ex02;
+
+public class Person {
+    private String name;
+
+    public Person(String name) {
+        this.name = name;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    @Override
+    public String toString() {
+        return "Person{" +
+                "name='" + name + '\'' +
+                '}';
+    }
+}

+ 68 - 0
JavaSE/day24/src/com/lovecoding/day24/ex02/Test01.java

@@ -0,0 +1,68 @@
+package com.lovecoding.day24.ex02;
+
+
+import org.junit.Test;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+public class Test01 {
+
+    /*
+    - 第一个队伍只要名字为小于等于3个字的成员姓名;第一个队伍筛选之后只要前3个人;存储到一个新集合中。
+    - 第二个队伍只要姓张的成员姓名;第二个队伍筛选之后去掉前2个人;存储到一个新集合中。
+    - 将两个队伍合并为一个队伍;存储到一个新集合中,打印合并队伍后成员姓名。
+    - 将两个队伍合并为一个队伍,并根据姓名创建 Person 对象;存储到一个新集合中,打印整个队伍的Person对象信息。
+     */
+    @Test
+    public void test01(){
+        //第一支队伍
+        ArrayList<String> one = new ArrayList<>();
+        one.add("迪丽热巴");
+        one.add("宋远桥");
+        one.add("苏星河");
+        one.add("石破天");
+        one.add("石中玉");
+        one.add("老子");
+        one.add("庄子");
+        one.add("洪七公");
+
+        //第一个队伍只要名字为小于等于3个字的成员姓名;第一个队伍筛选之后只要前3个人;存储到一个新集合中。
+        List<String> collect1 = one.stream().filter(e -> e.length() == 3).limit(3).collect(Collectors.toList());
+
+        collect1.forEach(System.out::println);
+
+        //第二支队伍
+        ArrayList<String> two = new ArrayList<>();
+        two.add("古力娜扎");
+        two.add("张无忌");
+        two.add("赵丽颖");
+        two.add("张三丰");
+        two.add("尼古拉斯赵四");
+        two.add("张天爱");
+        two.add("张二狗");
+
+        //第二个队伍只要姓张的成员姓名;第二个队伍筛选之后去掉前2个人;存储到一个新集合中。
+
+        List<String> collect2 = two.stream().filter(e -> e.startsWith("张")).skip(2).collect(Collectors.toList());
+
+        collect2.forEach(System.out::println);
+
+        //将两个队伍合并为一个队伍;存储到一个新集合中,打印合并队伍后成员姓名。
+
+        System.out.println("-----------");
+        List<String> collect = Stream.concat(collect1.stream(), collect2.stream()).collect(Collectors.toList());
+
+        collect.stream().forEach(System.out::println);
+
+        //将两个队伍合并为一个队伍,并根据姓名创建 Person 对象;存储到一个新集合中,打印整个队伍的Person对象信息。
+
+        System.out.println("-----------");
+        List<Person> collect3 = collect.stream().map(e -> new Person(e)).collect(Collectors.toList());
+        List<Person> collect4 = collect.stream().map(Person::new).collect(Collectors.toList());
+
+        collect4.forEach(System.out::println);
+    }
+}

+ 10 - 0
JavaSE/day24/src/com/lovecoding/day24/function01/Convter.java

@@ -0,0 +1,10 @@
+package com.lovecoding.day24.function01;
+
+
+@FunctionalInterface
+public interface Convter<T,R> {
+
+    //方法 参数  有 返回值
+    public R change(T t);
+
+}

+ 117 - 0
JavaSE/day24/src/com/lovecoding/day24/function01/Test01.java

@@ -0,0 +1,117 @@
+package com.lovecoding.day24.function01;
+
+import org.junit.Test;
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.function.Consumer;
+import java.util.function.Function;
+import java.util.function.Predicate;
+import java.util.function.Supplier;
+
+public class Test01 {
+
+
+    //功能型
+    @Test
+    public void test06(){
+
+        Convter<String,Character> convter1 = ( String s ) -> { return s.charAt(0);};
+        //省略 ()类型
+        Convter<String,Character> convter2 = ( s ) -> { return s.charAt(0);};
+        //省略 ()类型  只有一个参数 小括号 也可以省略
+        Convter<String,Character> convter3 =  s  -> { return s.charAt(0);};
+        // {} 里面只有一行语句  {}省略 省略 return
+        Convter<String,Character> convter4 =  s  ->  s.charAt(0);
+    }
+
+    //功能型
+    @Test
+    public void test05(){
+        Convter<String,Character> convter = ( String s ) -> { return s.charAt(0);};
+
+        Character a = convter.change("a");
+        System.out.println(a);
+    }
+
+    @Test
+    public void test04(){
+
+        Function<Integer,String[]> function1 = (Integer e) -> {return new String[e];};
+
+        String[] apply = function1.apply(10);
+
+        System.out.println(apply);
+    }
+
+    @Test
+    public void test03(){
+        //判断
+        Predicate<String> predicate1 =  (String e ) -> { return e.startsWith("a"); };
+
+        boolean aaaa = predicate1.test("baaa");
+        System.out.println(aaaa);
+
+        Predicate<String> predicate = new Predicate<>() {
+            @Override
+            public boolean test(String s) {
+                return s.startsWith("a");
+            }
+        };
+    }
+
+    @Test
+    public void test02(){
+        //返回值
+        Supplier<Double> supplier = new Supplier<Double>() {
+            @Override
+            public Double get() {
+                return Math.random();
+            }
+        };
+
+        Supplier<Double> supplier1 = () -> { return Math.random(); };
+
+        Double aDouble = supplier1.get();
+        System.out.println(aDouble);
+    }
+
+    @Test
+    public void test01(){
+        //消费型
+        Consumer<String> consumer1 = new Consumer<String>() {
+            @Override
+            public void accept(String s) {
+                System.out.println(s);
+            }
+        };
+
+        //lambda
+        Consumer<String> consumer2 = new Consumer<String>() {
+            @Override
+            public void accept(String s) {
+                System.out.println(s);
+            }
+        };
+
+        //lambda
+        Consumer<String> consumer3 =  (String s)-> { System.out.println(s); };
+
+        //输出
+        consumer3.accept("hello");
+
+        String[] arr = {"1","2"};
+
+        List<String> strings = Arrays.asList(arr);
+        //lambda
+        strings.forEach( (e) -> System.out.println(e) );
+
+        //之前
+        strings.forEach(new Consumer<String>() {
+            @Override
+            public void accept(String s) {
+                System.out.println(s);
+            }
+        });
+    }
+}

+ 36 - 0
JavaSE/day24/src/com/lovecoding/day24/function02/Person.java

@@ -0,0 +1,36 @@
+package com.lovecoding.day24.function02;
+
+
+public class Person {
+
+    private String name;
+    private Integer age;
+
+    public Person(String name) {
+        this.name = name;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    public Integer getAge() {
+        return age;
+    }
+
+    public void setAge(Integer age) {
+        this.age = age;
+    }
+
+    @Override
+    public String toString() {
+        return "Person{" +
+                "name='" + name + '\'' +
+                ", age=" + age +
+                '}';
+    }
+}

+ 45 - 0
JavaSE/day24/src/com/lovecoding/day24/function02/Test01.java

@@ -0,0 +1,45 @@
+package com.lovecoding.day24.function02;
+
+import org.junit.Test;
+
+import java.util.function.Consumer;
+import java.util.function.Function;
+import java.util.function.Supplier;
+
+public class Test01 {
+
+    @Test
+    public void test03(){
+        Function<Integer,String[] > function1 =  String[]::new;
+        Function<Integer,String[] > function2 =  e -> new String[e];
+
+        String[] apply = function1.apply(10);
+        System.out.println(apply.length);
+    }
+
+    @Test
+    public void test02(){
+
+        Function<String, Person> function1 =  e -> new Person(e);
+        //构造器
+        Function<String, Person> function2 =  Person::new;
+
+    }
+
+    @Test
+    public void test01(){
+
+        //lambda
+        Consumer<String>  consumer1 = e -> System.out.println(e);
+        //对象引用
+        Consumer<String>  consumer2 = System.out::println;
+
+        // e -> System.out.println(e);
+        // System.out::println;
+
+        //类引用
+        Supplier<Double> supplier1 = Math::random;
+        Supplier<Double> supplier2 = () -> Math.random() ;
+
+    }
+}

+ 27 - 0
JavaSE/day24/src/com/lovecoding/day24/optional01/Test01.java

@@ -0,0 +1,27 @@
+package com.lovecoding.day24.optional01;
+
+import org.junit.Test;
+
+import java.util.Optional;
+
+public class Test01 {
+
+    @Test
+    public void test01(){
+
+        String str = "hello";
+        Optional<String> str1 = Optional.of(str);
+
+        String s = str1.get();
+        System.out.println(s);
+
+        String str2 = null;
+
+        Optional<String> str21 = Optional.ofNullable(str2);
+        System.out.println(str21.orElseGet( () -> "1" ));
+
+        String str3 = str21.orElse("java");
+        System.out.println(str3);
+
+    }
+}

+ 41 - 0
JavaSE/day24/src/com/lovecoding/day24/stream01/Test01.java

@@ -0,0 +1,41 @@
+package com.lovecoding.day24.stream01;
+
+import org.junit.Test;
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.stream.Stream;
+
+public class Test01 {
+
+    //创建无限流**
+    @Test
+    public void test02(){
+        //Stream.iterate(0,  integer -> {  return  integer+1; } ).forEach(System.out::println);
+
+        Stream.generate(Math::random).forEach(System.out::println);
+    }
+
+    @Test
+    public void test01(){
+        //list
+        List<Integer> integers = List.of(1, 2, 3, 4, 5, 6);
+        //获取流
+        Stream<Integer> stream = integers.stream();
+        System.out.println(stream);
+
+        //并行流
+        Stream<Integer> integerStream = integers.parallelStream();
+
+        //数组
+        Object[] objects = integers.toArray();
+        //Arrays 获取流
+
+        Stream<Object> stream1 = Arrays.stream(objects);
+
+        //Stream
+        Stream<String> stringStream = Stream.of("1", "2", "3");
+
+
+    }
+}

+ 52 - 0
JavaSE/day24/src/com/lovecoding/day24/stream01/Test02.java

@@ -0,0 +1,52 @@
+package com.lovecoding.day24.stream01;
+
+import com.lovecoding.day24.function02.Person;
+import org.junit.Test;
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.stream.Stream;
+
+public class Test02 {
+
+    @Test
+    public void test04(){
+        String[] arr = {"hello","java"};
+
+        Stream<String> stream = Arrays.stream(arr);
+
+        stream.flatMap(  e -> Stream.of(e.split("|")) ).forEach(System.out::println);
+
+    }
+
+    @Test
+    public void test03(){
+        List<Person> people = List.of(new Person("zs"), new Person("ls"), new Person("ww"));
+
+        people.stream().map( e -> e.getName() ).forEach(System.out::println);
+    }
+
+    @Test
+    public void test02(){
+        List<Person> people = List.of(new Person("zs"), new Person("ls"), new Person("ww"));
+        //自然排序
+        //people.stream().sorted().forEach(System.out::println);
+        //定制
+        people.stream().sorted( (e1 , e2 ) -> e1.getName().compareTo(e2.getName()) ).forEach(System.out::println);
+    }
+
+    @Test
+    public void test01(){
+        Stream<Integer> stringStream = Stream.of(1,2,3,4,5,6,7,8,1,2,3,4,11);
+
+        //filter
+        stringStream
+                .filter( e -> e % 2 != 0 )
+                .distinct()
+                .skip(1)
+                .limit(3)
+                //.peek(System.out::println) //输出
+                .forEach( e ->System.out.println());
+
+    }
+}

+ 30 - 0
JavaSE/day24/src/com/lovecoding/day24/stream01/Test03.java

@@ -0,0 +1,30 @@
+package com.lovecoding.day24.stream01;
+
+import com.lovecoding.day24.function02.Person;
+import org.junit.Test;
+
+import java.util.List;
+import java.util.stream.Collectors;
+
+public class Test03 {
+
+    @Test
+    public void test01(){
+        List<Person> people = List.of(new Person("zs"), new Person("zzz"), new Person("ww"));
+
+        List<Person> z = people.stream().filter(e -> e.getName().startsWith("z")).collect(Collectors.toList());
+
+        System.out.println(z);
+    }
+
+    @Test
+    public void test02(){
+        List<Person> people = List.of(new Person("zs"), new Person("zzz"), new Person("ww"));
+
+        long z = people.stream().filter(e -> e.getName().startsWith("z")).count();
+
+        System.out.println(z);
+    }
+
+
+}