StringJoiner,StringBuffer的一些lamada写法

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}

 

转载于:https://www.cnblogs.com/passedbylove/p/11326587.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值