public String friendlyText(List data)
{
    if(CollectionUtils.isEmpty(data)) {
        return "[]";
    }

    StringJoiner joiner = new StringJoiner(",", "[", "]");
    String body = (String)data.stream().map(entry -> toJsonString(entry)).collect(Collectors.joining(","));
    return joiner.add(body).toString();
}

public String toJsonString(Object target)
{
    return JSON.toJSONStringWithDateFormat(target,"yyyy-MM-dd HH:mm:ss");
}


List data = new ArrayList();

for (int i = 0; i <10 ; i++) {
        SnmpMemory memory = new SnmpMemory
                .Builder()
                .withId(Long.valueOf(i))
                .withCreateTime(LocalDateTime.now())
                .withMemTotalReal(1024L)
                .withMemTotalSwap(512L)
                .withNodeId(Long.valueOf(i^2))
                .build();
        data.add(memory);
    }
    
    
    
String friendlyText = (String)data.stream().reduce(new StringBuilder(), (buffer, item) -> buffer.append(toJsonString(item)).append(','), StringBuffer::append).toString();

String body = (String)data.stream().map(entry -> toJsonString(entry)).collect(Collectors.joining(", "));
String friendlyText =  "[".concat(body).concat("]");
System.out.println(friendlyText);


StringJoiner joiner = new StringJoiner(",", "[", "]");
String body = (String)data.stream().map(entry -> toJsonString(entry)).collect(Collectors.joining(","));
String friendlyText =  joiner.add(body).toString();
System.out.println(friendlyText);


输出结果:
[{"createTime":"2019-08-09 11:20:06","id":0,"memTotalReal":1024,"memTotalSwap":512,"nodeId":2},{"createTime":"2019-08-09 11:20:06","id":1,"memTotalReal":1024,"memTotalSwap":512,"nodeId":3},{"createTime":"2019-08-09 11:20:06","id":2,"memTotalReal":1024,"memTotalSwap":512,"nodeId":0},{"createTime":"2019-08-09 11:20:06","id":3,"memTotalReal":1024,"memTotalSwap":512,"nodeId":1},{"createTime":"2019-08-09 11:20:06","id":4,"memTotalReal":1024,"memTotalSwap":512,"nodeId":6},{"createTime":"2019-08-09 11:20:06","id":5,"memTotalReal":1024,"memTotalSwap":512,"nodeId":7},{"createTime":"2019-08-09 11:20:06","id":6,"memTotalReal":1024,"memTotalSwap":512,"nodeId":4},{"createTime":"2019-08-09 11:20:06","id":7,"memTotalReal":1024,"memTotalSwap":512,"nodeId":5},{"createTime":"2019-08-09 11:20:06","id":8,"memTotalReal":1024,"memTotalSwap":512,"nodeId":10},{"createTime":"2019-08-09 11:20:06","id":9,"memTotalReal":1024,"memTotalSwap":512,"nodeId":11}]


String friendlyText = (String)data.stream().reduce(new StringBuilder(), (buffer, item) -> buffer.append(toJsonString(item)).append(','), StringBuffer::append).toString();

输出结果:
{"createTime":"2019-08-09 11:20:06","id":0,"memTotalReal":1024,"memTotalSwap":512,"nodeId":2},{"createTime":"2019-08-09 11:20:06","id":1,"memTotalReal":1024,"memTotalSwap":512,"nodeId":3},{"createTime":"2019-08-09 11:20:06","id":2,"memTotalReal":1024,"memTotalSwap":512,"nodeId":0},{"createTime":"2019-08-09 11:20:06","id":3,"memTotalReal":1024,"memTotalSwap":512,"nodeId":1},{"createTime":"2019-08-09 11:20:06","id":4,"memTotalReal":1024,"memTotalSwap":512,"nodeId":6},{"createTime":"2019-08-09 11:20:06","id":5,"memTotalReal":1024,"memTotalSwap":512,"nodeId":7},{"createTime":"2019-08-09 11:20:06","id":6,"memTotalReal":1024,"memTotalSwap":512,"nodeId":4},{"createTime":"2019-08-09 11:20:06","id":7,"memTotalReal":1024,"memTotalSwap":512,"nodeId":5},{"createTime":"2019-08-09 11:20:06","id":8,"memTotalReal":1024,"memTotalSwap":512,"nodeId":10},{"createTime":"2019-08-09 11:20:06","id":9,"memTotalReal":1024,"memTotalSwap":512,"nodeId":11}

 

posted on 2019-08-09 13:11  你不知道的浪漫  阅读(534)  评论(0编辑  收藏  举报