1. 逗号分隔的字符串转换为List
感谢大佬的分享字符串以逗号分隔的转换为List方法
- split + Arrays.asList
String str ="a,b,c";
List<String> split= Arrays.asList(str.split(","));
- Guava SplitterString
String str ="a, b, c";
List<String> splitToList= Splitter.on(",").trimResults().splitToList(str);
- Apache Commons的StringUtils
String str ="a,b,c";
List<String> asList= Arrays.asList(StringUtils.split(str,","));
- Spring Framework的StringUtils
String str ="a,b,c";
List<String> str =Arrays.asList(StringUtils.commaDelimitedListToStringArray(str));
- java8 stream
String str ="a,b,c";
List<String> str = Stream.of(str.split(",")).collect(toList());
2. List转换为逗号分隔的字符串
- Guava Joiner
List<String> list =new ArrayList();
list.add("a");
list.add("b");
list.add("c");
String str = Joiner.on(",").join(list);
- Apache Commons的StringUtils
List<String> list =new ArrayList();
list.add("a");
list.add("b");
list.add("c");
String str = StringUtils.join(list.toArray(),",");
- Spring Framework的StringUtils
List<String> list =new ArrayList();
list.add("a");
list.add("b");
list.add("c");
String str = StringUtils.collectionToDelimitedString(list,",");
3. List对象排序
- jdk1.8之前
Collections.sort(list,new Comparator<Object>(){
public int compare(Object o1, Object o2) {
return o1.属性 - o2.属性);
}
});
- jdk1.8
list.sort(Comparator.comparing(CountCardDetailVo::属性).reversed());
list.sort(Comparator.comparing(CountCardDetailVo::属性));
4. List对象去重
- 重写对象的equals和hashCode方法,使用distinct方法
- 使用TreeSet有序、无重复数据的特点
Set<对象> personSet = new TreeSet<>((o1, o2) -> o1.getId().compareTo(o2.getId()));
- 通用使用TreeSet有序、无重复数据的特点
List<对象> unique = list.stream().collect(collectingAndThen(toCollection(() -> new TreeSet<>(comparingLong(对象::getId))), ArrayList::new)
);
- 使用Map key不能重复的特点
public static <T> Predicate<T> distinctByKey(Function<? super T, Object> keyExtractor) {
Map<Object, Boolean> map = new ConcurrentHashMap<>();
return t -> map.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
}
list.stream().filter(distinctByKey(p -> p.getId())).collect(toList());
5. List对象分组,组内求和
- groupingBy
Map<String, List<PersonData>> collect = list.stream().collect(Collectors.groupingBy(PersonData::getType));
Map<String, Integer> collect2 = list.stream().collect(Collectors.groupingBy(PersonData::getType,Collectors.summingInt(PersonData::getAge)));
6. Lis转Map
- key唯一时转map
Map<Long, User> maps = userList.stream().collect(Collectors.toMap(User::getId,Function.identity()));
- key不唯一时转map,取其中的一个key
Map<Long, User> maps = userList.stream().collect(Collectors.toMap(User::getId, Function.identity(), (key1, key2) -> key2));
- 转为字段map
Map<Long, String> maps = userList.stream().collect(Collectors.toMap(User::getId, User::getAge, (key1, key2) -> key2));
- 转为字段map,但是字段是多个拼接
Map<String, User> maps = Maps.uniqueIndex(userList, new Function<User, String>() {
@Override
public Long apply(User user) {
return user.getId()+":"+user.getAge();
}
});
7. Lis删除
- 迭代器
Iterator<User> it=list.iterator();
while(it.hasNext()){
if("属性".equals(it.next()."属性")){
it.remove();
}
}
- jdk1.8 removeIf
list.removeIf(user -> user.属性.contains(""));