在开发过程中一些常用的list的操作,自己总结一下
1、将map转换成List
public static <T> List<T> mapToList(Map<String,T> map){
if(map == null){
return Collections.emptyList();
}
Collection<T> valueCollection = map.values();
List<T> valueList = new ArrayList<>(valueCollection);
return valueList;
}
2、String和List之间的转换
/**
* @Description:String字符串用逗号(,)分隔转换成Integer的List
* @Author: liujian
* @Version: 1.0
* @Create Date Time: 2022-03-02 17:26
* @Update Date Time:
* @see
*/
public List<Integer> getStrTransInt(String type) {
List<Integer> list = Lists.newArrayList();
if (Strings.isNullOrEmpty(type)) {
return list;
}
String[] split = type.split(",");
for (String splitTemp : split) {
//Integer.parseInt(splitTemp)将String转成Integer
list.add(Integer.parseInt(splitTemp));
}
return list;
}
/**
* @Description:List转String,将集合通过字符(;)拼接成String
* @Author: liujian
* @Version: 1.0
* @Create Date Time: 2022-03-02 17:26
* @Update Date Time:
* @see
*/
public List<Integer> getStrTransInt(String type) {
List list = Lists.ArrayList();
StringUtils.join(list,';');
}
3、list和set之间的转换
1) List转换为set
List<Long> ids=new ArrayList<>();
HashSet<Long> set=new HashSet<>(ids);
2) Set转换为List
HashSet<Long> set=new HashSet<>();
List<Long> ids=new ArrayList<>(set);
一些简单的总结,希望可以帮助到大家