mak_doni
mak_doni

Reputation: 581

Transform List of object to a Map in java

I have a list of item List<Item> resultBl like below :

id  = 18003  amount = 128 nameType = SUBMITTED
id  = 18189 amount = 95 nameType = SUBMITTED
id  = 18192 amount = 160 nameType = POSITIVE
id  = 18192 amount = 30 nameType = DRAFT
id  = 18192 amount = 873 nameType = SUBMITTED
id  = 18237 amount = 390 nameType = POSITIVE
id  = 18237 amount = 60 nameType = DRAFT
id  = 18237 amount = 2731 nameType = SUBMITTED

I want to transform this list to a map with this form, the key is the id and the value is a list of objects :

Map<Integer,List<ItemDetails>> mapTest= new HashMap<Integer,List<ItemDetails>>();
[18237 , [amount = 390 ,nameType = POSITIVE],[amount = 60 nameType = DRAFT], [amount = 2731 nameType = SUBMITTED]], ...

I tried with different ways but always I had repeated elements:

List<Integer> ids2 = new ArrayList<Integer>();
List<Integer> ids = new ArrayList<Integer>();

for(Item item: resultBl) {
  ids.add(item.getId());
}

ids2 =ids.stream().distinct().collect(Collectors.toList());
Map<Integer,List<ItemDetails>>  mapTest= new HashMap<Integer,List<ItemDetails>>();
List<ItemDetails> itemDetailsList = new ArrayList<ItemDetails>();
for(Integer s:ids2) {
    for(Item i : resultBl) {
      if(s.equals(i.getId())) {
         ItemDetails it =new ItemDetails();
         it.setAmount(i.getAmount());
         it.setNameType(i.getNameType()) ;
         itemDetailsList .add(it);
      }
    }
    mapTest.put(s, itemDetailsList);
}

Upvotes: 1

Views: 1612

Answers (4)

Jo&#227;o Dias
Jo&#227;o Dias

Reputation: 17460

Your best option would be to add a constructor to ItemDetails accepting an Item object:

public class ItemDetails {

    // properties, getters and setters

    public ItemDetails(Item item) {
        this.amount = item.getAmount();
        this.nameType = item.getNameType();
    }

}

And then use the following Java 8 features:

Map<Integer, List<ItemDetails>> itemsPerId = 
  itemsList.stream().collect(Collectors.groupingBy(Item::getId, Collectors.mapping(ItemDetails::new, Collectors.toList())));

Upvotes: 0

Nolequen
Nolequen

Reputation: 4245

Collectors.groupingBy with Collectors.mapping for downstream should work:

Map<Integer, List<ItemDetails>> result = resultBl.stream().collect(Collectors.groupingBy(Item::getId, Collectors.mapping(ItemDetails::new, Collectors.toList())));

Upvotes: 2

Grzegorz Kossakowski
Grzegorz Kossakowski

Reputation: 24

Map<Integer,List<Item>>  map = resultBL.stream(Collectors.toMap(Item::getId, Function.identity()));

It is Lambda. Java version >= 8

Upvotes: -1

Mershel
Mershel

Reputation: 552

I would use streams and groupingBy to do this.

When you have a list of your items resultBl all you have to do is

Map<Integer, List<Item>> resultMap = resultBl.stream().collect(groupingBy(Item::getId, toList()));

imports:

import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.toList;

This will group your items by the id parameter.

Upvotes: 0

Related Questions