1. 数组转集合
//数组转集合
String[] split = quickRechargeAmount.split(",");
List<String> stringList = Stream.of(split).collect(Collectors.toList());
2. 对List集合数据内容进行分组
//对List集合数据内容进行分组
Map<String, List<LiveAppGiftResponse>> collectList = giftResponseList.stream().collect(Collectors.groupingBy(LiveAppGiftResponse::getGiftTypeName));
3. 匹配两个集合内相等的元素,生成新的集合
//匹配两个集合内相等的元素,生成新的集合
private List<AppLiveUserRankResponse> getUserRankInfo(List<LiveUserRankCache> dayRankCacheList, Integer anchorId) {
List<AppLiveUserInfoResponse> userRewardList = appLiveRewardMapper.getUserRewardList(CommonContext.get().getSiteId(), anchorId);
if (CollectionTool.isNotEmpty(dayRankCacheList) && CollectionTool.isNotEmpty(userRewardList)) {
//组装用户与排行榜信息
List<AppLiveUserRankResponse> userRankList = dayRankCacheList.stream().map(dayRankCache -> userRewardList.stream()
.filter(userReward -> Objects.equals(userReward.getUserId(), dayRankCache.getUserId()))
.findFirst().map(m -> {
AppLiveUserRankResponse appLiveUserRank = new AppLiveUserRankResponse();
appLiveUserRank.setRank(dayRankCache.getRank());
appLiveUserRank.setScore(dayRankCache.getScore());
appLiveUserRank.setAvatarUrl(m.getAvatarUrl());
appLiveUserRank.setNickName(m.getNickName());
return appLiveUserRank;
}).orElse(null)).filter(Objects::nonNull).collect(Collectors.toList());
return userRankList;
}
return null;
}
4. 根据A集合某元素去对比B集合某元素,生成过滤后的B集合
//根据A集合某元素去对比B集合某元素,生成过滤后的B集合
public List<User> selectUnassignedGameList(List<User> userBList) {
// 查询所有用户信息
List<User> userAList = this.list();
// 获取userBList的中所有用户信息并对比后排除userAList已存在用户信息
List<User> list = userBList.stream().filter(b -> !userAList.stream().map(a
-> a.getUId()).collect(Collectors.toList()).contains(b.getUId())).collect(Collectors.toList());
return list;
}
5. 集合内某一元素比对特定条件进行排序
//查询当前账户币种信息集合
List<LiveCustomerWallet> customerWalletList = customerWalletMapper.search(Criteria.add(LiveCustomerWallet.PROP_CUSTOMER_ID, Operator.EQ, model.getUserId()), Order.asc(LiveCustomerWallet.PROP_CURRENCY));
if (CollectionTool.isNotEmpty(customerWalletList)){
//先按指定币种排序,然后再主货币排序,再按币种排序
String mainCurrency = BaseCacheTool.getSysSite(model.getSiteId()).getMainCurrency();
if (!mainCurrency.equals(model.getCurrency())){
customerWalletList.sort((o1, o2) -> {
int i = o1.getCurrency().equals(mainCurrency) ? 0 : 1;
int i2 = o2.getCurrency().equals(mainCurrency) ? 0 : 1;
return i - i2;
});
}
customerWalletList.sort((o1, o2) -> {
int i = o1.getCurrency().equals(model.getCurrency()) ? 0 : 1;
int i2 = o2.getCurrency().equals(model.getCurrency()) ? 0 : 1;
return i - i2;
});
}
6. 集合去重
//使用java steam进行去重
List<Integer> list = numbersList.stream().distinct().collect(Collectors.toList());
//将重复的列表添加到 LinkedHashSet,然后将内容返回到列表中
LinkedHashSet<Integer> hashSet = new LinkedHashSet<>(numbersList);
7. 对集合进行分页并模糊查询
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
public class ListTest {
public static void main(String[] args) {
List<User> list = new ArrayList<>();
list.add(new User("zhangsan", 18));
list.add(new User("lisi", 19));
list.add(new User("wangwu", 20));
list.add(new User("zhaoliu", 21));
String searchName = "h";
// 是否全部匹配,还是 局部包含匹配
Boolean globalMatch = Boolean.FALSE;
//获取当前页
int pageNumber = 1;
//一页返回多少条
int pageSize = 2;
List<User> stringList = list.stream().filter(l -> globalMatch?l.getName().equals(searchName):
l.getName().contains(searchName))
//根据年龄升序排序
.sorted(Comparator.comparing(User::getAge))
.collect(Collectors.toList());
//一页 返回二条
List<User> pageList = stringList.stream().skip((pageNumber-1)*pageSize).limit(pageSize).collect(Collectors.toList());
System.out.println("返回总条数:"+stringList.size()+"当前页为:"+pageNumber+"每页显示条数:"+pageSize);
System.out.println("本次返回的数据为:");
pageList.forEach(System.out::println);
}
}
class User{
private String name;
private Integer age;
public User(String name, Integer age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}