List<LoanBack> backList = loanBackMapper.selectList(queryWrapper); if(!CollectionUtils.isEmpty(backList)){ for(LoanBack back : backList){ hasCharge = hasCharge.add(back.getBackMoney()); } }如何优化这段代码
时间: 2023-07-16 15:16:23 浏览: 54
这段代码可以优化为使用聚合函数 SUM() 来计算总和,避免使用循环,提高性能,示例如下:
```java
BigDecimal hasCharge = loanBackMapper.selectObjs(queryWrapper).stream()
.map(obj -> (BigDecimal) obj)
.reduce(BigDecimal.ZERO, BigDecimal::add);
```
这里使用了 `selectObjs()` 方法,它会返回一个 Object 类型的 List,其中每个元素对应查询结果集的一行数据,这里我们假设查询结果只有一列数据,即 `back_money` 列,因此可以将每个元素强制转换为 BigDecimal 类型,然后使用 reduce() 方法将所有 BigDecimal 对象相加得到最终结果。
相关问题
class MyFilter extends Filter { //我们在performFiltering(CharSequence charSequence)这个方法中定义过滤规则 @Override protected FilterResults performFiltering(CharSequence charSequence) { FilterResults result = new FilterResults(); List<Note> list; if (TextUtils.isEmpty(charSequence)) {//当过滤的关键字为空的时候,我们则显示所有的数据 list = backList; } else {//否则把符合条件的数据对象添加到集合中 list = new ArrayList<>(); for (Note note : backList) { if (note.getContent().contains(charSequence)) { list.add(note); } } } result.values = list; //将得到的集合保存到FilterResults的value变量中 result.count = list.size();//将集合的大小保存到FilterResults的count变量中 return result; } //在publishResults方法中告诉适配器更新界面 @Override protected void publishResults(CharSequence charSequence, FilterResults filterResults) { noteList = (List<Note>)filterResults.values; if (filterResults.count>0){ notifyDataSetChanged();//通知数据发生了改变 }else { notifyDataSetInvalidated();//通知数据失效 } } } }这里运用了哪些技术
这段代码主要运用了Android中的Filter类,用于过滤搜索结果。在这个类中,我们定义了两个方法:
1. performFiltering(CharSequence charSequence):这个方法定义了过滤规则,它接受一个CharSequence参数,它代表用户输入的过滤条件。在这个方法中,我们根据用户输入的过滤条件,过滤出符合条件的Note对象,并把它们保存到一个List集合中。最后,我们把这个集合保存到FilterResults对象中。
2. publishResults(CharSequence charSequence, FilterResults filterResults):这个方法在过滤完成后被调用。在这个方法中,我们把过滤后得到的Note对象集合赋值给noteList变量,并通知数据发生了改变。如果过滤结果为空,我们则通知数据失效。
html历史记录代码,js 实现浏览历史记录示例
下面是一个简单的示例,展示如何使用 HTML 和 JavaScript 实现浏览历史记录功能。
HTML 代码:
```html
<!DOCTYPE html>
<html>
<head>
<title>浏览历史记录示例</title>
</head>
<body>
<h1>浏览历史记录示例</h1>
<p>点击下面的链接来添加浏览记录:</p>
<ul id="history-list">
<li><a href="#" onclick="addHistory('http://www.example.com/page1', '页面 1')">页面 1</a></li>
<li><a href="#" onclick="addHistory('http://www.example.com/page2', '页面 2')">页面 2</a></li>
<li><a href="#" onclick="addHistory('http://www.example.com/page3', '页面 3')">页面 3</a></li>
</ul>
<p>浏览历史记录:</p>
<ul id="back-list"></ul>
</body>
</html>
```
JavaScript 代码:
```javascript
// 定义一个数组,用于存储浏览历史记录
var historyList = [];
// 定义一个函数,用于添加浏览历史记录
function addHistory(url, title) {
// 将新的浏览历史记录添加到数组中
historyList.push({url: url, title: title});
// 刷新浏览历史记录列表
refreshHistory();
}
// 定义一个函数,用于刷新浏览历史记录列表
function refreshHistory() {
// 获取用于显示浏览历史记录的列表元素
var backList = document.getElementById("back-list");
// 清空列表元素
backList.innerHTML = "";
// 遍历浏览历史记录数组,将每个历史记录添加到列表中
for (var i = 0; i < historyList.length; i++) {
var history = historyList[i];
var li = document.createElement("li");
var a = document.createElement("a");
a.href = history.url;
a.textContent = history.title;
li.appendChild(a);
backList.appendChild(li);
}
}
```
在这个示例中,我们定义了一个用于存储浏览历史记录的数组 `historyList`,并且定义了两个函数:`addHistory` 和 `refreshHistory`。`addHistory` 函数用于添加新的浏览历史记录,并将其添加到数组中;`refreshHistory` 函数用于刷新浏览历史记录列表,遍历数组中的所有历史记录,并将它们添加到 HTML 页面中。
在 HTML 页面中,我们定义了一个用于显示浏览历史记录的列表元素 `back-list`,并在页面加载时调用 `refreshHistory` 函数,以便初始化浏览历史记录列表。
注意:这个示例只是一个简单的演示,实际的浏览历史记录功能可能需要更复杂的实现,比如记录用户的浏览时间,实现前进和后退功能等等。