List<SortDTO> list = ImmutableList.of( new SortDTO("300"), new SortDTO("50"), new SortDTO("200"), new SortDTO("220")); SortDTO[] array = new SortDTO[list.size()];list.toArray(array); log.info("搜索之前:{}", JSON.toJSONString(array)); Arrays.sort(array, Comparator.comparing(SortDTO::getSortTarget)); log.info("先排序,结果为:{}", JSON.toJSONString(array));int index = Arr ays.binarySearch(array, new SortDTO("200"), Comparator.comparing(SortDTO::getSortTarget));if(ind ex<0){ throw new RuntimeException("没有找到 200");} log.info("搜索结果:{}", JSON.toJSONString(array[index]));解释一下
时间: 2024-04-25 13:28:04 浏览: 63
这段代码主要是对一个 SortDTO 的数组进行排序和查找操作。首先,定义了一个包含四个 SortDTO 对象的不可变列表,并将其转换为数组。然后,使用 Arrays.sort() 方法对该数组进行排序,排序的依据是 SortDTO 对象中的 sortTarget 字段。接着,使用 Arrays.binarySearch() 方法在数组中查找 sortTarget 字段为 "200" 的 SortDTO 对象,如果找到则返回该对象在数组中的索引,否则抛出异常。最后,输出排序后的数组和查找结果。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044901.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![jar](https://img-home.csdnimg.cn/images/20210720083455.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![dll](https://img-home.csdnimg.cn/images/20250102104920.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)