Java常用api
1、stream
1、map筛选数据
map.entrySet().stream().filter(entry -> ObjectUtils.isNotEmpty(entry.getValue()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue))
2、list筛选数据
List<InfoVO> childList = list.stream().filter(child -> Objects.equals(id, child.getId()))
.collect(Collectors.toList());
3、list 转 map
list <对象> 转 Map <String, String>
Map<String, String> map = list.stream().collect(
Collectors.toMap(InfoVO::getId, InfoVO::getName, (k1, k2) -> k1));
list <对象> 转 Map <String, 对象>
Map<String, InfoVO> map = list.stream()
.collect(Collectors.toMap(InfoVO::getName, Function.identity(), (a, b) -> a));
list 分组转 map
Map<String, List<T>> map = list.stream().collect(
Collectors.groupingBy(x -> AttributeUtil.getValue(Constant.ID, x)));
list <对象> 转 LinkedHashMap
Map<String, List<T>> map= list.stream().collect(
Collectors.groupingBy(x -> x.getCode(), LinkedHashMap::new, Collectors.toList()));
4、list 转 list
list各个对象中的list转到同一个list
List<String> allChildList = list.stream().map(x -> ObjectUtils.isEmpty(x.getChildren()) ?
List.of(x) : x.getChildren())
.flatMap(Collection::stream)
.map(InfoVO::getTitle).collect(Collectors.toList());
5、排序
正序
list.stream().sorted()
list.sorted(Comparator.comparing(InfoVO::getName))
倒序
list.stream().sorted(Comparator.reverseOrder())
6、match
未匹配到,返回true
list.stream().noneMatch(child -> Objects.equals(id, child.getId()))
未匹配到,返回false
list.stream().anyMatch(child -> Objects.equals(id, child.getId()))
全部匹配到 或 流为空,返回true
list.stream().anyMatch(child -> Objects.equals(id, child.getId()))
7、截取
截取list前10个元素
list.stream().limit(10).collect(Collectors.toList());
去除list前10个元素
list.stream().skip(10);
8、计算
List< String >最大值、最小值
list.stream().max(String::compareTo).orElse("0")
list.stream().min(String::compareTo).orElse("0")
List< String > 转 int 最大值、最小值、均值
list.stream().mapToInt(x->Integer.parseInt(x)).max().orElse(0)
list.stream().mapToInt(x->Integer.parseInt(x)).min().orElse(0)
list.stream().mapToInt(x->Integer.parseInt(x)).average().orElse(0)
List< BigDecimal > 最大值、最小值、均值
list.stream().max(BigDecimal::compareTo).orElse(BigDecimal.ZERO)
list.stream().min(BigDecimal::compareTo).orElse(BigDecimal.ZERO)
list.stream().reduce(BigDecimal.ZERO, BigDecimal::add)
.divide(BigDecimal.valueOf(list.size()), 2, RoundingMode.HALF_UP)
List<对象> 最大值、最小值、均值
list.stream().map(ValueVO::getValue).max(BigDecimal::compareTo).orElse(BigDecimal.ZERO)
list.stream().map(ValueVO::getValue).min(BigDecimal::compareTo).orElse(BigDecimal.ZERO)
list.stream().map(ValueVO::getValue).reduce(BigDecimal.ZERO, BigDecimal::add)
.divide(BigDecimal.valueOf(list.size()), 2, RoundingMode.HALF_UP)
List< BigDecimal > 求和
list.stream().reduce(BigDecimal.ZERO, (a, b) -> a.add(b))
list.stream().reduce(BigDecimal.ZERO, BigDecimal::add)
list<对象> 求和
ValueVO sumVO = list.stream().reduce(
(a , b) -> {
ValueVO vo = new ValueVO();
columnList.forEach(column -> {
BigDecimal sum = BigDecimalUtil.sum(
getFieldValue(column.getValue(), a, Constant.ZERO),
getFieldValue(column.getValue(), b, Constant.ZERO));
PatternUtil.setFieldValue(column.getValue(), vo, sum.toString());
});
return vo;
})
.orElse( new ValueVO() );
Java常用方法
1、反射
1、获取对象的属性字段
public static <T> List<Field> getFieldList(Class<T> beanClass, List<String> list){
List<Field> fieldList = Lists.newArrayList();
while (ObjectUtils.isNotEmpty(beanClass)) {
fieldList.addAll(List.of(beanClass.getDeclaredFields()));
beanClass = (Class<T>) beanClass.getSuperclass();
}
if(ObjectUtils.isNotEmpty(fieldList) && ObjectUtils.isNotEmpty(list)){
fieldList = fieldList.stream().filter(f -> list.contains(f.getName())).collect(Collectors.toList());
}
return fieldList;
}
2、获取对象的属性值
public static <T> String getFieldValue(Object columnId, T data, String defaultValue) {
List<Field> fieldList = getFieldList(data.getClass(), List.of());
Field field = fieldList.stream().filter(f -> Objects.equals(f.getName(), columnId)).findFirst().get();
ReflectionUtils.makeAccessible(field);
Object value = ReflectionUtils.getField(field, data);
return ObjectUtils.isEmpty(value) || Objects.equals(Constant.NON, value)? defaultValue : String.valueOf(value);
}
3、设置对象的属性值
public static <T> void setFieldValue(String columnId, T data, String value) {
List<Field> fieldList = getFieldList(data.getClass(), List.of());
Field field = fieldList.stream().filter(f -> Objects.equals(f.getName(), columnId)).findFirst().get();
ReflectionUtils.makeAccessible(field);
ReflectionUtils.setField(field, data, value);
}
4、设置默认值
public static <T> void setDefault(T data, String defaultStr){
try {
List<Field> fieldList = getFieldList(data.getClass(), List.of());
for (Field f : fieldList) {
ReflectionUtils.makeAccessible(f);
if (ObjectUtils.isEmpty(f.get(data)) && String.class == f.getType()) {
ReflectionUtils.setField(f, data, defaultStr);
}
}
}catch (IllegalAccessException e) {
log.error(e.getMessage());
}
}