guyanqing 9 months ago
parent
commit
8147dcfc4b

+ 18 - 0
src/main/java/com/sf/javase/day21/Data.java

@@ -0,0 +1,18 @@
+package com.sf.javase.day21;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * 数据类
+ */
+public class Data {
+  static   List<Product> products = new ArrayList<>();
+    static {
+        products.add(new Product(1L, "苹果手机", 8888.88,"手机"));
+        products.add(new Product(2L, "华为手机", 6666.66,"手机"));
+        products.add(new Product(3L, "联想笔记本",7777.77,"电脑"));
+        products.add(new Product(4L, "机械键盘", 999.99,"键盘"));
+        products.add(new Product(5L, "雷蛇鼠标", 222.22,"鼠标"));
+    }
+}

+ 6 - 0
src/main/java/com/sf/javase/day21/IPrintable.java

@@ -0,0 +1,6 @@
+package com.sf.javase.day21;
+
+@FunctionalInterface
+public interface IPrintable {
+    void print(String str);
+}

+ 6 - 0
src/main/java/com/sf/javase/day21/IShowable.java

@@ -0,0 +1,6 @@
+package com.sf.javase.day21;
+@FunctionalInterface
+public interface IShowable {
+
+    void show();
+}

+ 9 - 0
src/main/java/com/sf/javase/day21/MyPredicate.java

@@ -0,0 +1,9 @@
+package com.sf.javase.day21;
+
+//   断言
+@FunctionalInterface
+public interface MyPredicate {
+
+    boolean test(Product product);
+//    boolean test1(Product product);
+}

+ 60 - 0
src/main/java/com/sf/javase/day21/Product.java

@@ -0,0 +1,60 @@
+package com.sf.javase.day21;
+
+public class Product {
+    private Long    id;         // 序号
+    private String  name;       // 商品名称
+    private Double 	price;      // 价格
+    private String  type;       // 类型
+
+    public Product() {
+    }
+
+    public Product(Long id, String name, Double price, String type) {
+        this.id = id;
+        this.name = name;
+        this.price = price;
+        this.type = type;
+    }
+
+    public Long getId() {
+        return id;
+    }
+
+    public void setId(Long id) {
+        this.id = id;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    public Double getPrice() {
+        return price;
+    }
+
+    public void setPrice(Double price) {
+        this.price = price;
+    }
+
+    public String getType() {
+        return type;
+    }
+
+    public void setType(String type) {
+        this.type = type;
+    }
+
+    @Override
+    public String toString() {
+        return "Product{" +
+                "id=" + id +
+                ", name='" + name + '\'' +
+                ", price=" + price +
+                ", type='" + type + '\'' +
+                '}';
+    }
+}

+ 106 - 0
src/main/java/com/sf/javase/day21/T.java

@@ -0,0 +1,106 @@
+package com.sf.javase.day21;
+
+import org.junit.jupiter.api.Test;
+
+import java.util.*;
+
+public class T {
+    public void t1(){
+       new Thread(() -> System.out.println("开一个子线程处理一个耗时操作")).start();
+    }
+
+    /**
+     * 需求1: 筛选出所有名称包含手机的商品
+     *
+     * 需求2: 筛选出所有价格大于1000的商品
+     */
+
+    @Test
+    public void t2(){
+       List<Product> list1 = findProductsByName(Data.products);
+        System.out.println(list1);
+        List<Product> list2 = findProductsByPrice(Data.products);
+        System.out.println(list2);
+    }
+
+    private List<Product> findProductsByPrice(List<Product> products) {
+        List<Product> list = new ArrayList<>();
+        for (Product product : products) {
+            if (product.getPrice() > 1000) {
+                list.add(product);
+            }
+        }
+        return list;
+    }
+
+    private List<Product> findProductsByName(List<Product> products) {
+        List<Product> list = new ArrayList<>();
+        for (Product product : products) {
+            if(product.getName().contains("手机")){
+                list.add(product);
+            }
+        }
+        return list;
+    }
+
+
+    @Test
+    public void t3(){
+      List<Product> list1=  findProductsByCondition(Data.products, (product -> product.getName().contains("手机")));
+        System.out.println(list1);
+
+        List<Product> list2 = findProductsByCondition(Data.products, product -> product.getPrice() > 1000);
+        System.out.println(list2);
+    }
+
+    private List<Product> findProductsByCondition(List<Product> products, MyPredicate myPredicate) {
+        List<Product> list = new ArrayList<>();
+        for (Product product : products) {
+            if(myPredicate.test(product)){
+                list.add(product);
+            }
+        }
+     return list;
+    }
+
+    @Test
+    public void t6(){
+        //**练习1:编写一个接口 IShowable,
+        // 接口中存在一个抽象方法show,无参数无返回值,传入对应的lambda表达式**
+        useShow(() -> {System.out.println("H");});
+        }
+
+     public void useShow(IShowable iShowable){
+        iShowable.show();
+     }
+
+    /**
+     * **练习2:编写一个接口 IPrintable,
+     * 接口中存在一个抽象方法print,有参数无返回值,传入对应的lambda表达式**
+     */
+    @Test
+     public void t7(){
+        useIPrintable((a) -> {
+            System.out.println(a);
+        });
+     }
+
+
+     public void useIPrintable(IPrintable iPrintable){
+        iPrintable.print("H");
+     }
+
+
+     @Test
+     public void t8(){
+         List<String> list = Arrays.asList("a","c","b");
+         Map<String,String> map = new HashMap<>();
+         map.put("手机","小米");
+         map.put("笔记本","苹果笔记本");
+         map.put("鼠标","罗技鼠标");
+
+         list.forEach((str) -> System.out.println(str));
+
+     }
+}
+

BIN
target/classes/com/sf/javase/day21/Data.class


BIN
target/classes/com/sf/javase/day21/MyPredicate.class


BIN
target/classes/com/sf/javase/day21/Product.class


BIN
target/classes/com/sf/javase/day21/T$1.class


BIN
target/classes/com/sf/javase/day21/T.class