operations.opsForZSet().add(followeeKey, entityId, System.currentTimeMillis());
时间: 2024-05-21 15:18:05 浏览: 13
这行代码使用了 Redis 的操作,具体是将一个带有分数的元素(entityId)添加到一个有序集合(ZSet)中(followeeKey),分数是当前时间戳。这个操作可以用于实现关注列表的功能,可以将关注的用户ID作为元素,当前时间戳作为分数,添加到关注者的有序集合中。这样,可以方便地根据关注时间的先后顺序来获取关注列表。
相关问题
list<entity> stream流,合并相同entityid,value相加
假设你有一个 `List<Entity>` 的对象,其中 `Entity` 包含两个属性:`entityId` 和 `value`。你想要将具有相同 `entityId` 的对象的 `value` 相加并合并成一个新的 `List<Entity>` 对象。你可以按照以下步骤进行操作:
1. 创建一个 `Map<String, Double>` 的对象,其中 `String` 表示 `entityId`,`Double` 表示 `value`。这个 `Map` 将用于将相同的 `entityId` 的值相加。
2. 遍历 `List<Entity>`,将每个对象的 `entityId` 作为 `Map` 的键,将 `value` 添加到对应的值上。
3. 创建一个新的 `List<Entity>`,遍历 `Map` 中的键值对,将键作为 `entityId`,值作为 `value` 创建一个新的 `Entity` 对象,并添加到新的 `List<Entity>` 中。
4. 返回新的 `List<Entity>` 对象。
下面是一个示例代码:
```java
public List<Entity> mergeEntities(List<Entity> entities) {
Map<String, Double> map = new HashMap<>();
for (Entity entity : entities) {
String entityId = entity.getEntityId();
double value = entity.getValue();
map.put(entityId, map.getOrDefault(entityId, 0.0) + value);
}
List<Entity> mergedEntities = new ArrayList<>();
for (Map.Entry<String, Double> entry : map.entrySet()) {
String entityId = entry.getKey();
double value = entry.getValue();
Entity mergedEntity = new Entity(entityId, value);
mergedEntities.add(mergedEntity);
}
return mergedEntities;
}
```
注意,这里使用了 `getOrDefault` 方法来获取 `entityId` 对应的值,如果该值不存在则返回默认值 0.0。这是为了确保之前不存在的 `entityId` 对应的值也能被正确地添加到 `Map` 中并进行累加。
这段代码的泛型写法,写出代码 public Review addProjectSpecialty(Integer reviewid, List<Integer> projectspecialtyids) { ReviewDO reviewdo = basedorepo.findById(reviewid).get(); projectspecialtyids.forEach(item -> { ProjectSpecialtyDO projectspecialtydo = projspecdorepo.findById(item).get(); reviewdo.getProjectSpecialtyDOs().add(projectspecialtydo); }); return dataconverter.toTModel(basedorepo.save(reviewdo)); }
这段代码的泛型写法已经很好了,没有必要再进行泛型改写。如果你一定要进行泛型改写,可以尝试以下写法:
```
public <T extends BaseEntity, S extends BaseEntity> T addSpecialtyToEntity(Integer entityId, List<Integer> specialtyIds, JpaRepository<T, Integer> entityRepo, JpaRepository<S, Integer> specialtyRepo, Function<T, List<S>> getSpecialtiesFunc) {
T entity = entityRepo.findById(entityId).get();
specialtyIds.forEach(specialtyId -> {
S specialty = specialtyRepo.findById(specialtyId).get();
getSpecialtiesFunc.apply(entity).add(specialty);
});
return entityRepo.save(entity);
}
```
然后可以使用以下方式调用:
```
Review review = addSpecialtyToEntity(reviewId, projectSpecialtyIds, reviewRepo, projectSpecialtyRepo, Review::getProjectSpecialtyDOs);
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![java](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)