简体   繁体   中英

How can I map the elements of the Lists created by the Collectors.groupingby collector to an other type using streams?

I have a stream of objects with multiple attributes and I want to create a Map of two of the Attributes of these objects.

For example:

class MyClass
{
   private int A;
   private int B;
   private String C;

   //constructor, getters, setters, etc...
}

using Collectors.groupingBy(), I can create a Map< String, List< MyClass>>:

Map<String, List<Myclass>> = inputStream.collect(Collectors.groupingby(MyClass::getC));

but I want to have a Map< String, List< int>> with Myclass.C as key and MyClass.B as list elements.
MyClass.A should not be contained in the result.
How can I do this?

Thanks in advance

inputStream.collect(
  Collectors.groupingBy(
    MyClass::getC,
    Collectors.mapping(
      MyClass::getB,
      Collectors.toList())));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM