Collection

List

import java.util.Collections;

// list to 串接 1,2,3
List<Long> ids = set.stream()
                .map(Long::valueOf)
                .collect(Collectors.toList());
String idStr = StrUtil.join(",", ids); // ex: 5,1

// String to List
String str = "aaa";
Collections.singletonList(str);

// 空集合
Collections.emptyList();

Stream

import java.util.stream.Collectors;

// 打印
longs.stream().forEach(System.out::println);

// Set to List
Set<String> intersect = new HashSet<>(Arrays.asList("1", "2", "2", "3"));
List<Long> ids = intersect.stream()
        .map(Long::valueOf) // id String to Long
        .collect(Collectors.toList());

// user to UserDTO
List<UserDTO> userDTOS = userService.query().list() 
        .stream()
        .map(user -> BeanUtil.copyProperties(user, UserDTO.class))
        .collect(Collectors.toList());

// distinct() 取得userId后去重复
List<Long> ids = list.stream()
        .map(Blog::getUserId)
        .distinct()
        .collect(Collectors.toList());
Map<Long, List<Blog>> blogIds = list.stream()
        .collect(Collectors.groupingBy(Blog::getUserId));

Map<Long, List<Long>> blogIds = list.stream()
        .collect(Collectors.groupingBy(Blog::getUserId,
                Collectors.mapping(Blog::getId, Collectors.toList())));

Map<Long, List<Long>> map = new HashMap<>();
bean.forEach(b -> {
    map.computeIfAbsent(b.getUserId(), k -> new ArrayList<>()).add(b.getId());
});

map.forEach((k, v) -> {});

从 set 中筛选出不在 list 中的元素

String key = "z1:" + id;
// ZADD z1 1 m1 2 m2 3 m3 4 m4
Set<String> set = stringRedisTemplate.opsForZSet().range(key, 0, -1);

// 创建一个List<Long>,包含元素 1, 2
List<Long> list = Arrays.asList(1L, 2L);

// 过滤掉在 list中的元素也就是1, 2,然后将剩下的 3, 4 从redis 移除 
set.stream()
    .filter(e -> !list.contains(Long.valueOf(e))) 
    .forEach(blogId -> {
    stringRedisTemplate.opsForZSet().remove(key, blogId.toString()); 
});