Java 中数组 binarySearch 方法and拷贝对象工具类CopyUtils-可忽略覆盖Null值详解

Java中给数组提供了一个二分法查找数组元素的位置,这个方法从JDK1.6开始,很多人不理解,做了一个总结对比看即可。

binarySearch(Object[], Object key)
方法的object[]参数是要查找的数组,key参数为要查找的key值。

方法的返回值有几种:

1.找到的情况下:如果key在数组中,则返回搜索值的索引,从0开始。
2.找不到的情况下:
 [1] 搜索值不是数组元素,且在数组范围内,从1开始计数,得“ - 插入点索引值”;
 [2] 搜索值是数组元素,从0开始计数,得搜索值的索引值;
 [3] 搜索值不是数组元素,且大于数组内元素,索引值为 – (length + 1);
 [4] 搜索值不是数组元素,且小于数组内元素,索引值为 – 1。

举例:

        int a[] = new int[] { 1, 3, 4, 6, 8, 9 };
        int x1 = Arrays.binarySearch(a, 5);    
        int x2 = Arrays.binarySearch(a, 4);    
        int x3 = Arrays.binarySearch(a, 0);    
        int x4 = Arrays.binarySearch(a, 10);   
        System.out.println("x1:" + x1 + ", x2:" + x2);
        System.out.println("x3:" + x3 + ", x4:" + x4);

打印结果 :

x1:-4, x2:2
x3:-1, x4:-7

binarySearch(Object[], int fromIndex, int toIndex, Object key)
方法的object[]参数是要查找的数组,fromIndex参数是搜索的开始索引(包括),toIndex参数是搜索的结束索引(不包括), key参数为要查找的key值。

方法的返回值有几种:

1.找到的情况下:如果key在数组中,则返回搜索值的索引。
2.找不到的情况下:
 [1] 该搜索键在范围内,但不是数组元素,由1开始计数,得“ - 插入点索引值”;
 [2] 该搜索键在范围内,且是数组元素,由0开始计数,得搜索值的索引值;
 [3] 该搜索键不在范围内,且小于范围(数组)内元素,返回–(fromIndex + 1);
 [4] 该搜索键不在范围内,且大于范围(数组)内元素,返回 –(toIndex + 1)。

举例:

        int a[] = new int[] { 1, 3, 4, 6, 8, 9 };
        int x1 = Arrays.binarySearch(a, 1, 4, 5);  
        int x2 = Arrays.binarySearch(a, 1, 4, 4);  
        int x3 = Arrays.binarySearch(a, 1, 4, 2);  
        int x4 = Arrays.binarySearch(a, 1, 4, 10);  
        System.out.println("x1:" + x1 + ", x2:" + x2);
        System.out.println("x3:" + x3 + ", x4:" + x4);

打印结果:

x1:-4, x2:2
x3:-2, x4:-5

 

使用场景:针对两个对象相互拷贝,然后只替换不为Null的值,自带的BeanUtils无法实现,所以单独在网上找了一个然后进行使用,可忽略Null值的拷贝。

最近做一个实训项目,然后持久层使用的JPA,前端使用的Layui,更新的时候如果前端传入了部分字段,那么其他字段没有传入就不做更新,在JPA当中默认传入一个完整的对象,一般都是直接先查询然后再修改这样操作 ,但是前端目前只要求传入什么就修改什么,没有传入的默认不修改,意思就是只修改部分字段内容,所以需要我后端先根据ID查询信息然后再修改就要使用到克隆对象忽略Null值,目前这个工具类就可以实现。

CopyUtils工具类代码:

/**
 * CopyUtils
 *
 * @author lcry
 * @date 2019/09/19 17:31
 * 对象互相拷贝忽略Null值
 */
public class CopyUtils {
    public static String[] getNullPropertyNames(Object source) {
        final BeanWrapper src = new BeanWrapperImpl(source);
        java.beans.PropertyDescriptor[] pds = src.getPropertyDescriptors();

        Set<String> emptyNames = new HashSet<String>();
        for (java.beans.PropertyDescriptor pd : pds) {
            Object srcValue = src.getPropertyValue(pd.getName());
            if (srcValue == null) {
                emptyNames.add(pd.getName());
            }
        }
        String[] result = new String[emptyNames.size()];
        return emptyNames.toArray(result);
    }

    public static void copyProperties(Object src, Object target) {
        BeanUtils.copyProperties(src, target, getNullPropertyNames(src));
    }
}

使用方法非常简单:

    /**
     * 修改
     *
     * @param employee
     */
    public void update(Employee employee) {
//        只更新部分字段、2019年9月19日 17:24:11 - 若前端不传入就不更新
        Employee desinfo = employeeDao.findById(employee.getId()).get();
        if (Validator.isNotEmpty(desinfo)) {
            CopyUtils.copyProperties(desinfo, employee);
            employeeDao.save(employee);
        }
    }

可以自行做测试,比BeanUtils中拷贝对象更好使用~

    @Test
    public void testcopytest() {
//        初始化第一个对象只设置name
        Employee employee1 = new Employee();
        employee1.setName("Lcry");
//        toString查看对象信息
        System.out.println("初始化employee1->" + employee1);
//        初始化第二个对象,不设置name,设置其他值
        Employee employee2 = new Employee();
        employee2.setAddress("123");
        employee2.setDepid("1");
        employee2.setPassword("123456");
        employee2.setAge(22);
        employee2.setEmail("i@51it.wang");
        System.out.println("初始化employee2->" + employee2);
//        采用CopyUtils只拷贝不为空的属性,name属性为赋值为employee2
        CopyUtils.copyProperties(employee1, employee2);
        System.out.println("通过CopyUtils的employee2->" + employee2);
//        采用自带的BeanUtil只能全部复制、包括Null值
        BeanUtils.copyProperties(employee1,employee2);
        System.out.println("通过BeanUtil的employee2->" + employee2);
    }

打印日志:

初始化employee1->Employee(id=null, name=Lcry, status=null, sex=null, address=null, img=null, phone=null, email=null, password=null, entrytime=null, age=null, empnum=null, title=null, depid=null)
初始化employee2->Employee(id=null, name=null, status=null, sex=null, address=123, img=null, phone=null, email=i@51it.wang, password=123456, entrytime=null, age=22, empnum=null, title=null, depid=1)
通过CopyUtils的employee2->Employee(id=null, name=Lcry, status=null, sex=null, address=123, img=null, phone=null, email=i@51it.wang, password=123456, entrytime=null, age=22, empnum=null, title=null, depid=1)
通过BeanUtil的employee2->Employee(id=null, name=Lcry, status=null, sex=null, address=null, img=null, phone=null, email=null, password=null, entrytime=null, age=null, empnum=null, title=null, depid=null)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值