1、java对象复制:BeanUtils.copyProperties(temp, reimbursement);
2、流操作
List对象转map:Map<String, Fields> fieldsMap = fields.stream().collect(Collectors.toMap(Fields::getFieldName, Fields->Fields));
List对象转map分组:Map<String, List<Fields>> listMap = fields.stream().collect(Collectors.groupingBy(Fields::getOssId));
List对象转map,对应list顺序与list一致:<LinkedHashMap> abcM = fields.stream().collect(Collectors.groupingBy(t->t.getSupplierId(), LinkedHashMap::new, Collectors.toList()));
List对象转map,key为多个字段:Map<String, Fields> systemFieldsMap = fields.stream().collect(Collectors.toMap(x->x.getSupplierId() + "-" + x.getContractId(), Fields->Fields));
List对象转map多字段分组:Map<String, List<Fields>> listMap = fields.stream().collect(Collectors.groupingBy(s->s.getName()+'-'+s.getValue()+'-'+s.getKey()));
List对象转map(如果key重复,选择第一个值:(v1, v2)->v1):Map<String, String> idMap = fields.stream().collect(Collectors.toMap(Fields::getResId, Fields::getResBaseId, (v1, v2)->v1));
求和:Map<String, BigDecimal> allSumIn = fields.stream().collect(Collectors.groupingBy(Fields::getBaseId, CollectorsUtil.summingBigDecimal(Fields::getAmount)));
求最大值:Map<String, BigDecimal> allMaxIn = fields.stream().collect(Collectors.groupingBy(Fields::getBaseId, CollectorsUtil.maxBy(Fields::getAmount)));
对象转List<String> : List ids = fields.stream().map(Fields::getId).collect(Collectors.toList());
对象转Set<String> : List ids = fields.stream().map(Fields::getId).collect(Collectors.toSet());
求和:BigDecimal sumTotal = fields.stream().collect(CollectorsUtil.summingBigDecimal(Fields::getAmount));
3、List操作
去重:HashSet
Set转List:
Set<String> collect;
List<String> wordList =new ArrayList<>(collect)
String[]转List:String[] words = {"ace", "boom", "crew", "dog", "eon"}; List<String> wordList = Arrays.asList(words);
去掉list中null:List olds.removeAll(Collections.singleton(null));
map的值转换为list集合: List<String> kyList = map.keySet().stream().collect(Collectors.toList());
字符串转List<String> : List<String> list = Arrays.asList(str.split(","));
List转字符串:String str = StringUtils.join(list, ",");
List转数组:String[] arr = list.toArray(new String[list.size()]);
数组转List:List<String> list = Arrays.asList(arr);
json转对象:JSONObject.parseObject(jsonlist, List.class);
对象转json:JSONObject obj = (JSONObject) JSONObject.toJSON(t);
List求并集:retainAll
去掉字符串中尾号为数字:String name; name().replaceAll("\\d+", "");
移除:inres.removeIf(key->key.getAmount().compareTo(new BigDecimal("0.0")) <= 0);
判断元素是否存在:hashSet 而不是 list,list 判断一个元素是否存在的代码
批量修改List<String>,在值后面追加id: for (int i = 0; i < destName.size(); i++){destName.set(i, destName.get(i) + id);}
对象转Map:
Map<String, Object> map = null;
String jsonString = JSONUtil.toJsonStr(t);
Gson gson = new Gson();
map = new HashMap<>();
map = gson.fromJson(jsonString, map.getClass());