SysSetting是一个对象
@Data
@TableName("t_sys_setting")
@ApiModel(value = "SysSetting对象", description = "系统设置表")
public class SysSetting implements Serializable {
private static final long serialVersionUID = 1L;
@TableId(value = "id", type = IdType.AUTO)
private Long id;
@ApiModelProperty("类型 10:交易设置 20:提现设置 30:签到设置")
private String type;
@ApiModelProperty(value = "键")
@TableField("`key`")
private String key;
@ApiModelProperty(value = "值")
private String value;
@ApiModelProperty(value = "描述")
private String description;
}
List<SysSetting> sysSettings = iSysSettingService.listSettingByCondition(SysSettingConstants.TYPE_SIGN);
Map<String, SysSetting> settingMap = CollectionUtils.convertMap(sysSettings, SysSetting::getKey);
convertMap方法如下。传参时param1:List
/**
* List转Map(Map的值为List)
*
* @param from List对象
* @param keyFunc Map的键
* @param <T> List对象类型
* @param <K> 键类型
* @return map
*/
public static <T, K> Map<K, T> convertMap(List<T> from, Function<T, K> keyFunc) {
return from.stream().collect(Collectors.toMap(keyFunc, Function.identity()));
}
得到的:Map
转换之前List
syssettings[
SysSetting(id=13, type=sign, key=sign_cycle, value=7, description=签到周期),
SysSetting(id=14, type=sign, key=sign_reward_points, value=10, description=每日签到奖励-积分),
SysSetting(id=15, type=sign, key=sign_reward_exp, value=10, description=每日签到奖励-成长值),
SysSetting(id=16, type=sign, key=sign_cont_json, value=[
{
"signCycle": 2,
"rewardPoints": 20,
"rewardExp": 20
},
{
"signCycle": 3,
"rewardPoints": 40,
"rewardExp": 40
},
{
"signCycle": 6,
"rewardPoints": 60,
"rewardExp": 60
}
], description=连续签到Json)]
转换之后Map
settingmap{
sign_reward_points = SysSetting(id=14, type=sign, key=sign_reward_points, value=10, description=每日签到奖励-积分),
sign_cont_json = SysSetting(id=16, type=sign, key=sign_cont_json, value=[
{
"signCycle": 2,
"rewardPoints": 20,
"rewardExp": 20
},
{
"signCycle": 3,
"rewardPoints": 40,
"rewardExp": 40
},
{
"signCycle": 6,
"rewardPoints": 60,
"rewardExp": 60
}
], description=连续签到Json),
sign_reward_exp = SysSetting(id=15, type=sign, key=sign_reward_exp, value=10, description=每日签到奖励-成长值),
sign_cycle = SysSetting(id=13, type=sign, key=sign_cycle, value=7, description=签到周期)}
/**
* List转Map(Map的值为List对象的某个属性)
*
* @param from List对象
* @param keyFunc Map的键
* @param valueFunc Map的值
* @param <T> List对象类型
* @param <K> 键类型
* @param <V> 值类型
* @return map
*/
public static <T, K, V> Map<K, V> convertMap(List<T> from, Function<T, K> keyFunc, Function<T, V> valueFunc) {
return from.stream().collect(Collectors.toMap(keyFunc, valueFunc));
}
public static <T, K, V> Map<K, List<V>> convertMultiMap(List<T> from, Function<T, K> keyFunc, Function<T, V> valueFunc) {
return from.stream().collect(Collectors.groupingBy(keyFunc,
Collectors.mapping(valueFunc, Collectors.toList())));
}
// 暂时没想好名字,先以 2 结尾噶
public static <T, K, V> Map<K, Set<V>> convertMultiMap2(List<T> from, Function<T, K> keyFunc, Function<T, V> valueFunc) {
return from.stream().collect(Collectors.groupingBy(keyFunc, Collectors.mapping(valueFunc, Collectors.toSet())));
}
public static boolean containsAny(Collection<?> source, Collection<?> candidates) {
return org.springframework.util.CollectionUtils.containsAny(source, candidates);
}
手机扫一扫
移动阅读更方便
你可能感兴趣的文章