java-实现类中的字段按kay1=value1kay2=value2..字符串拼接并实现按字母顺序排序

 public static String splicingString(Object object) throws IllegalAccessException {
        List requestParameters = new ArrayList<>();
        Field[] fields = object.getClass().getDeclaredFields();
        for (int i = 0; i < fields.length; i++) {
         

            fields[i].setAccessible(true);
            String value = "";
            if (!StringUtils.isEmpty(fields[i].get(object))) {
                value = fields[i].get(object).toString();
            }

            RequestParameter parameter = new RequestParameter(fields[i].getName(), value);
            requestParameters.add(parameter);
        }
        requestParameters.sort(Comparator.comparing(RequestParameter::getName));
        StringBuilder parameterBuilder = new StringBuilder();
        for (RequestParameter requestParameter : requestParameters) {
            parameterBuilder.append(requestParameter.getName());
            parameterBuilder.append("=");
            parameterBuilder.append(requestParameter.getValue());
        }
        return parameterBuilder.toString();

    }

public class RequestParameter {

    private String name;

    private String value;
    //set get ...
}

 

你可能感兴趣的:(java常用类)