java不常见方法的学习

拷贝数组

1.for循环方法:
2.System.arraycopy()方法:
3.Arrays.copyOf()方法:
4.Object.clone()方法:

// for循环
public static void copyArr1ToArr2_01(){
}

// System.arraycopy,深克隆
// System.arraycopy(arr1,0,arr2,0,arr1.length);
// 从arr1的0号位置复制arr1.length放到arr2的0号位置
public static void copyArr1ToArr2_02(){
    int[] arr1 = {1,2,3,4,5,6,7};
    int[] arr2 = new int[arr1.length];
    System.arraycopy(arr1,0,arr2,0,arr1.length);
    arr1 = null;
    System.out.println(Arrays.toString(arr1));
    System.out.println(Arrays.toString(arr2));
}
// Arrays.copyof,深克隆
// 第一个参数是原来的数组名,第二个参数是数组的长度
public static void copyArr1ToArr2_03(){
    int[] arr1 = {1,2,3,4,5,6,7};
    int[] arr2 = new int[arr1.length];
    arr2 = Arrays.copyOf(arr1,arr1.length);
    arr1 = null;
    System.out.println(Arrays.toString(arr1));
    System.out.println(Arrays.toString(arr2));
}

// object的clone方法,深克隆
public static void copyArr1ToArr2_04(){
    int[] arr1 = {1,2,3,4,5,6,7};
    int[] arr2 = new int[arr1.length];
    arr2 = arr1.clone();
    arr1 = null;
    System.out.println(Arrays.toString(arr1));
    System.out.println(Arrays.toString(arr2));
}

jdk1.8新特性

Lambda表达式

lambda表达式本质上是一段匿名内部类,也可以是一段可以传递的代码
lambda最直观的优点:简洁代码

  //匿名内部类
  Comparator<Integer> cpt = new Comparator<Integer>() {
      @Override
      public int compare(Integer o1, Integer o2) {
          return Integer.compare(o1,o2);
      }
  };

  TreeSet<Integer> set = new TreeSet<>(cpt);

  System.out.println("=========================");

  //使用lambda表达式
  Comparator<Integer> cpt2 = (x,y) -> Integer.compare(x,y);
  TreeSet<Integer> set2 = new TreeSet<>(cpt2);

只需要一行代码,极大减少代码量!!

这样一个场景,在商城浏览商品信息时,经常会有条件的进行筛选浏览,例如要选颜色为红色的、价格小于8000千的….

// 筛选颜色为红色
public  List<Product> filterProductByColor(List<Product> list){
    List<Product> prods = new ArrayList<>();
    for (Product product : list){
        if ("红色".equals(product.getColor())){
            prods.add(product);
        }
    }
    return prods;
 }

// 筛选价格小于8千的
public  List<Product> filterProductByPrice(List<Product> list){
    List<Product> prods = new ArrayList<>();
    for (Product product : list){
        if (product.getPrice() < 8000){
            prods.add(product);
        }
    }
    return prods;
 }

我们发现实际上这些过滤方法的核心就只有if语句中的条件判断,其他均为模版代码,每次变更一下需求,都需要新增一个方法,然后复制黏贴,假设这个过滤方法有几百行,那么这样的做法难免笨拙了一点。如何进行优化呢?

优化一:使用设计模式

定义一个MyPredicate接口

public interface MyPredicate <T> {
    boolean test(T t);
}

如果想要筛选颜色为红色的商品,定义一个颜色过滤类

public class ColorPredicate implements MyPredicate <Product> {

     private static final String RED = "红色";

     @Override
     public boolean test(Product product) {
         return RED.equals(product.getColor());
     }

基础语法:https://blog.csdn.net/zyc88888/article/details/82622137
详细讲解:https://blog.csdn.net/qq_29411737/article/details/80835658

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值