Skip to content

java--lambda表达式

stream

  • boolean anyMatch
  • boolean allMatch
  • boolean noneMatch
  • Optional<T> findAny()

从 C#的角度看代码

FirstOrDefault

java
friends.stream()
    .filter(f -> f.getFriendId().equals(user.getUserId()))
    .findFirst()
    .orElse(null);

workIdMaps.stream()
    .filter(pair -> pair.getLeft().equals(part))
    .map(Pair::getRight)
    .findFirst()
    .orElse(null);

??

java
user.setAtId(friend == null ? "" : friend.getFriendId());

Select+ToList()

java
List<String> roomIds = roomList.stream()
    .map(WecomGroup::getRoomId) // 只提取单个字段
    .toList();
java
List<String> roomIds = roomList.stream()
    .map(room -> room.getRoomId().replaceFirst("^R:", "")) //对单个字段做处理
    .toList();

集合



data.getWorkIds().stream()
                .filter(workId -> workId != null && !workId.isEmpty())
                .collect(Collectors.joining("|"))

long.ToString

java
String.valueOf(groupChatCrackId)

switch String 转 Enum

java
switch (type) {
    case "text":
        return "a";
    case "img":
        return "b";
    case "link":
    case "infoGrpCreate":
    case "infoGrpJoin":
        return "c";
    default:
        throw new IllegalArgumentException("Unsupported type: " + type);
}
java
//第一版
List<String> urls = configs.stream()
    .filter(config -> config.getConditions().stream()
            .anyMatch(condition -> "*".equals(condition.getValue()) ||
                    condition.getValue().equals(siyuChatMessage.getChatId())))
    .map(MessageForwardConfig::getTargetUrl)
    .toList();

//第二版
List<String> urls = configs.stream()
    .filter(config -> config.getConditions() == null || config.getConditions().isEmpty() ||
            config.getConditions().stream().anyMatch(condition -> {
                String field = condition.getField();
                String value = condition.getValue();

                if ("msgType".equals(field)) {
                    return msg.getMsgType().equals(value);
                } else if ("crackId".equals(field)) {
                    return msg.getChatId().equals(value);
                }
                return false;
            }))
    .map(MessageForwardConfig::getTargetUrl)
    .toList();

//第三版
List<String> urls = configs.stream()
    .filter(config -> {
        if (config.getConditions() == null || config.getConditions().isEmpty()) {
            return true; // 没有条件则直接匹配
        }
        return config.getConditions().stream().allMatch(condition -> {
            String field = condition.getField();
            String value = condition.getValue();
            switch (field) {
                case "msgType":
                    return msg.getMsgType().equals(value);
                case "crackId":
                    return msg.getChatId().equals(value);
                case "chatName":
                    return msg.getChatName() != null && msg.getChatName().matches(value);
                default:
                    SkynetUtils.printWarn("未知的条件字段: " + field, MODULE, CATEGORY, "", "", "", null);
                    return false;
            }
        });
    })
    .map(MessageForwardConfig::getTargetUrl)
    .toList();

wrRbE_DgAABZb_fBtzUKBHE5s1Cqoblg