List转为Map
1、业务需求,需要将List<SysSetting>转为Map
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;
}
2、我们需要用到一个CollectionUtils,具体操作如下:
List<SysSetting> sysSettings = iSysSettingService.listSettingByCondition(SysSettingConstants.TYPE_SIGN);
Map<String, SysSetting> settingMap = CollectionUtils.convertMap(sysSettings, SysSetting::getKey);
convertMap方法如下。传参时param1:List<Entity>对象,param2:Entity的属性
/**
* 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<Entity::属性,Entity>
3、这样就完成了转换,下面我们看一下具体的效果。
转换之前List<SysSetting>
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<Entity::属性,Entity>
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=签到周期)}
4、CollectionUtils扩展
/**
* 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);
}