List转为String
List<String> lst=new ArrayList<>();
lst.add("1");
lst.add("2");
lst.add("3");
lst.add("3");
System.out.println(StringUtils.join(lst,","));
String collect = Stream.of("1", "2").collect(Collectors.joining(","));
System.out.println(collect);
List转Set
Set<String> set = Sets.newHashSet(lst);
System.out.println(set);
创建单个List
System.out.println(Collections.singletonList("s"));
List<Integer> collect = Stream.of(1).collect(Collectors.toList());
System.out.println(Arrays.asList("1","2"));
创建单个Map
System.out.println(Collections.singletonMap("1","1"));
Collections.sort方法,排序
List<Integer> lst=new ArrayList<>();
lst.add(4);
lst.add(1);
lst.add(2);
lst.add(3);
Collections.sort(lst, new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
//降序
return o2.compareTo(o1);
}
});
输出
[4, 3, 2, 1]
移除
List<Integer> collect = Stream.of(1, 2).collect(Collectors.toList());
collect.removeIf(s -> s.equals(1));
System.out.println(collect);
CollectionUtils.filter过滤
List<Integer> lst=new ArrayList<>();
lst.add(1);
lst.add(2);
lst.add(3);
CollectionUtils.filter(lst, new Predicate() {
@Override
public boolean evaluate(Object object) {
Integer obj=(Integer)object;
return obj==1;
}
});