Group List with java 8

1

The input data would be a list of Products as below

[ {
"id": "123",
"ean": "7898100848355",
"title": "Cruzador espacial Nikana - 3000m - sem garantia",
"brand": "nikana",
"price": 820900.90,
"stock": 1
},
{
"id": "u7042",
"ean": "7898054800492",
"title": "Espada de fótons Nikana Azul",
"brand": "nikana",
"price": 2199.90,
"stock": 82
},
{
"id": "bb2r3s0",
"ean": "2059251400402",
"title": "Corredor POD 3000hp Nikana",
"brand": "nikana",
"price": 17832.90,
"stock": 8
},
{
"id": "321",
"ean": "7898100848355",
"title": "Cruzador espacial Nikana - 3000m - sem garantia",
"brand": "trek",
"price": 790300.90,
"stock": 0
},
{
"id": "80092",
"ean": "",
"title": "Espada de Fótons REDAV Azul",
"brand": "redav",
"price": 1799.90,
"stock": 0
},
{
"id": "7728uu",
"ean": "7898100848355",
"title": "Cruzador espacial Ekul - 3000m - sem garantia",
"brand": "ekul",
"price": 1300000.00,
"stock": 1
}
]

My question is how to group the list so that it returns the output like this:

{
"data": [
{
"description": "Espada de Fótons REDAV Azul",
"items": [
{
"id": "u7042",
"ean": "7898054800492",
"title": "Espada de fótons Nikana Azul",
"brand": "nikana",
"price": 2199.90,
"stock": 82
},
{
"id": "80092",
"ean": "",
"title": "Espada de Fótons REDAV Azul",
"brand": "redav",
"price": 1799.90,
"stock": 0
}
]
},
{
"description": "nikana",
"items": [
{
"id": "u7042",
"ean": "7898054800492",
"title": "Espada de fótons Nikana Azul",
"brand": "nikana",
"price": 2199.90,
"stock": 82
},
{
"id": "bb2r3s0",
"ean": "2059251400402",
"title": "Corredor POD 3000hp Nikana",
"brand": "nikana",
"price": 17832.90,
"stock": 8
},
{
"id": "123",
"ean": "7898100848355",
"title": "Cruzador espacial Nikana - 3000m - sem
garantia",
"brand": "nikana",
"price": 820900.90,
"stock": 1
}
]
}
]
}
    
asked by anonymous 01.10.2017 / 21:23

1 answer

0

Basically you will have to read the input json, scroll through it and create a new object from it.

For this operation I advise you to use POJOS with the GSON library.

Creating the pojo

public class ProdutoPojo implements Serializable {

    private static final long serialVersionUID = 1L;

    private String id;
    private String ean;
    private String title;
    private String brand;
    private String price;
    private String stock;
    //Gets e Sets ocultados

Main

public class Main {

    public static void main(String[] args) {
        StringBuilder json = new StringBuilder();
        json.append("[ { ");
        json.append("\"id\": \"123\", ");
        json.append("\"ean\": \"7898100848355\", ");
        json.append("\"title\": \"Cruzador espacial Nikana - 3000m - sem garantia\", ");
        json.append("\"brand\": \"nikana\", ");
        json.append("\"price\": 820900.90, ");
        json.append("\"stock\": 1 ");
        json.append("}, ");
        json.append("{ ");
        json.append("\"id\": \"u7042\", ");
        json.append("\"ean\": \"7898054800492\", ");
        json.append("\"title\": \"Espada de fótons Nikana Azul\", ");
        json.append("\"brand\": \"nikana\", ");
        json.append("\"price\": 2199.90, ");
        json.append("\"stock\": 82 ");
        json.append("}, ");
        json.append("{ ");
        json.append("\"id\": \"bb2r3s0\", ");
        json.append("\"ean\": \"2059251400402\", ");
        json.append("\"title\": \"Corredor POD 3000hp Nikana\", ");
        json.append("\"brand\": \"nikana\", ");
        json.append("\"price\": 17832.90, ");
        json.append("\"stock\": 8 ");
        json.append("}, ");
        json.append("{ ");
        json.append("\"id\": \"321\", ");
        json.append("\"ean\": \"7898100848355\", ");
        json.append("\"title\": \"Cruzador espacial Nikana - 3000m - sem garantia\", ");
        json.append("\"brand\": \"trek\", ");
        json.append("\"price\": 790300.90, ");
        json.append("\"stock\": 0 ");
        json.append("}, ");
        json.append("{ ");
        json.append("\"id\": \"80092\", ");
        json.append("\"ean\": \"\", ");
        json.append("\"title\": \"Espada de Fótons REDAV Azul\", ");
        json.append("\"brand\": \"redav\", ");
        json.append("\"price\": 1799.90, ");
        json.append("\"stock\": 0 ");
        json.append("}, ");
        json.append("{ ");
        json.append("\"id\": \"7728uu\", ");
        json.append("\"title\": \"Cruzador espacial Ekul - 3000m - sem garantia\", ");
        json.append("\"brand\": \"ekul\", ");
        json.append("\"price\": 1300000.00, ");
        json.append("\"stock\": 1 ");
        json.append("} ");
        json.append("] ");

        List<ProdutoPojo> produtos = Arrays.asList(new Gson().fromJson(json.toString(), ProdutoPojo[].class));
        for (ProdutoPojo produto : produtos) {
            //Nesse ponto você vai ter que criar um novo objeto e preenchê lo
            System.out.println(produto.getBrand());
            System.out.println(produto.getEan());
            System.out.println(produto.getId());
        }

    }

}
    
02.10.2017 / 17:00