com.iohao.game.common.kit.CollKit Maven / Gradle / Ivy
/*
* ioGame
* Copyright (C) 2021 - present 渔民小镇 ([email protected]、[email protected]) . All Rights Reserved.
* # iohao.com . 渔民小镇
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see .
*/
package com.iohao.game.common.kit;
import lombok.experimental.UtilityClass;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
* 集合相关工具
*
* @author 渔民小镇
* @date 2022-01-14
*/
@UtilityClass
public class CollKit {
/**
* 分组统计
*
* key is 元素下标
* value is 元素下标对应的数量
*
*
*
* 示例
* handCards: [11, 11, 11, 21, 46, 33,33, 18, 18, 18, 18]
*
* 得到的 map {
* 11 : 3
* 18 : 4
* 21 : 1
* 33 : 2
* 46 : 1
* }
*
*
* @param list 元素列表
* @return map
*/
public Map groupCounting(List list) {
return list.stream().
collect(
Collectors.groupingBy(Function.identity(), Collectors.summingInt(e -> 1))
);
}
public boolean notEmpty(Collection> collection) {
return !isEmpty(collection);
}
public boolean isEmpty(Collection> collection) {
return collection == null || collection.isEmpty();
}
public Optional findAny(Set set) {
if (isEmpty(set)) {
return Optional.empty();
}
return set.stream().findAny();
}
}