我目前有一组数据,我从我的Java应用程序中的API中撤回。返回的数据如下:
$(document).ready(function() {
$('form').submit(function(e) {
var isValid = true;
var allow = false;
$('input[type="text"]').each(function() {
if ($.trim($(this).val()) == '') {
isValid = false;
$(this).css({
"border": "1px solid red",
"background": "#FFCECE"
});
}
else {
$(this).css({
"border": "",
"background": ""
});
}
});
if (isValid == false){
if(!confirm('Few Fields are empty! Are you sure want to continue?')){
e.preventDefault();
} else {
alert('Thank you for submitting');
}
}
else
alert('Thank you for submitting');
});
});
正如您所看到的,{
"id": 1,
"receiptId": "123456",
"selections": [
{
"name": "Apple",
"price": "£1"
}
]
},
{
"id": 2,
"receiptId": "678910",
"selections": [
{
"name": "Pear",
"price": "£0.5"
}
]
},
{
"id": 3,
"receiptId": "123456",
"selections": [
{
"name": "Banana",
"price:": "£2.00"
}
]
}
中的两个是相同的,我想将任何重复的receiptId
数据合并为一个块。即:
receiptId's
目前,我只是通过执行以下操作将数据流式传输到地图中:
{
"id": 1,
"receiptId": "123456",
"selections": [
{
"name": "Apple",
"price": "£1"
},
{
"name": "Banana",
"price": "£2.00"
}
]
},
{
"id": 2,
"receiptId": "678910",
"selections": [
{
"name": "Pear",
"price": "£0.5"
}
]
},
List<String> data = data.getData()
.stream()
.map(this::dataToReadable)
.collect(Collectors.toList());
执行以下操作:
dataToReadable
private String dataToReadable(List data) {
return new DataBuilder().fromData(data)
.buildData();
}
执行以下操作:
fromData
答案 0 :(得分:2)
看看这是否有效。
data.getData()
.stream()
.collect(Collectors.groupingBy(Data::getRecieptId))
.entrySet()
.stream()
.map(item -> dataToReadable(item.getValue()))
.collect(Collectors.toList());