2

I have an object, "Item" with fields: int: id String: prices

The string prices contains one or more price values separated by comma.

Problem: Using stream operations I want a map of type Map<Integer,Set<Integer>> from List<Item> items

where key of Map is id and Set has value of prices as Integer extracted from the String prices.

There will be repeated ids in the List and can have different price strings.

I have come up with an approach which produces the following result:

Map<Integer, Set<List<Integer>>> itemStoresMapIntermediate = items.stream()
            .collect(Collectors.groupingBy(Item::getItemId,Collectors.mapping(Item::getStoresAsIntList, Collectors.toSet())));

getStoresAsIntList() returns List of price values from String prices in the object.

The above is certainly not what I want.

5
  • Will one stream contain multiple items of the same ID? Commented Mar 7, 2017 at 11:34
  • What does getStoresAsIntList do? Commented Mar 7, 2017 at 11:37
  • Yes, there will be multiple items of same id. Commented Mar 7, 2017 at 11:37
  • getStoresAsIntList returns List<Integer> of price values in String prices Commented Mar 7, 2017 at 11:38
  • If the IDs are unique, you can use Collectors.toMap(Item::getItemId, i->new HashSet<>( i.getStoresAsIntList())). Otherwise, you need to do either, flatMap to some kind of key-price pair before groupingBy or use a flatMapping collector like at the end of this answer. Commented Mar 7, 2017 at 11:48

1 Answer 1

5

If I understood correctly...

 Item item1 = new Item(1, "22,23,24");
 Item item2 = new Item(2, "33,34,35");
 Item item3 = new Item(1, "22,57,58");

 Map<Integer, Set<Integer>> map = Stream.of(item1, item2, item3)
            .collect(Collectors.toMap(
                    Item::getId,
                    i -> new HashSet<>(i.getStoresAsIntList()),
                    (left, right) -> {
                        left.addAll(right);
                        return left;
                    }, HashMap::new));

 System.out.println(map); // {1=[22, 23, 24, 57, 58], 2=[33, 34, 35]}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.