java数组反射实现动态的判断一个对象是否是数组并且对数组进行拆包输出

时间:2022-09-16 05:19:25
public static Map<String, String> maptoMapString(Map<String, ?> map) { return map.entrySet().stream().collect(Collectors.toMap( (entry) -> { return entry.getKey(); }, (entry) -> { if(entry.getValue().getClass().isArray()) { StringBuilder sb = new StringBuilder(); for(int i=0; i<Array.getLength(entry.getValue()); ++i) { Object obj = Array.get(entry.getValue(), i); sb.append(obj.toString()).append(","); } if (sb.length() > 0) { sb.deleteCharAt(sb.length() - 1); } return sb.toString(); } else { return entry.getValue().toString(); } } )); }