神刀安全网

Transforming Collections — Java 8 Collectors API

Java 8 offers a new Stream   API that makes handling collections easier and less verbose and error-prone. Stream   API offers a set of methods for filtering and transforming underlying collections, but our interest is to cover the  collect(Collector collector) method. This method offers very interesting functionality, such as transforming underlying collection to another collection type, grouping elements of collection, joining elements, and partitioning elements.

Let’s get started with an example, and start explaining based on it. I suggest you open new tab with the source code so that you can review the code samples while following the explanation here.

In main method we first initalize a  List of simple Person objects that we’ll use to demonstrate Collectors API . After initializing this list of people, we’ll demonstrate by transforming collections to another  Collection type, implemented in a  toCollections(people) call. Collectors API has a pre-built set of Collector implementations to use. In our example, we’re calling:

  • Collector::toList
  • Collector::toSet
  • Collector::toCollection(Supplier<C> collectionFactory)

toList() transforms the underlying Collection to List collection type. toSet() transforms, as expected, to the  Set collection type. toCollection API is just a bit more sophisticated, since it will transform the current collection type to the one we supply by passing Supplier . In our case, we’re passing HashSet<Person>::new . That means we want the current Stream’s underlying collection to be transformed to a  HashSet instance. Similarly, we’ll call the same toCollection passing it ArrayList<Person>::new as an argument.

Another example of collectors is Collectors.joining , demonstrated in our joining(persons) method. Collectors.joining method is overloaded. The first no argument version just joins underlying collection elements to one String . Another version of this method is the one with one String argument, which is a delimiter to be used when joining. The last one is similiar to this one with delimiter argument, but accepts two additional String arguments: the first one is prefix , the second one is suffix . Basically it will join all elements of a stream, and the resulting String will be prepended with prefix , and appended suffix we passed.

In our summing(people) method, we demonstrate the ability to calculate the sum of elements in an underlying collection. For this case, we’ll call the  Collectors#summingInt API, which accepts IntFunction argument, which is basically a function that transforms each stream element to an int value. In our case we just pass Person::getAge method reference, which returns int value of a person’s age. There are also two very similar methods to this one in Collectors API: summintDouble and summingLong . When we pass these types of collectors to the Stream::collect method, it returns a sum of elements as either Integer, Double or Long . Another very interesting type of collector is Collectors#summarizingInt . In our case, we’re calling it via summarizingInt(Person::getAge) . What it returns is an instance of IntSummaryStatistics . It offers methods such as getMax(), getAverage(), getMin(), getSum(), which are the statistics we get based on underlying stream elements.

In our partitioningBy(people) , we demonstrate usage of Collectors#partitioningBy(Predicate<? super T>) method. If we pass that Collector to the  Stream::collect method, it will return Map<Boolean, List<Person>> as a result. Basically, we just need to provide Predicate which decides which partition the current element of Stream will end up in. At the end, our resulting  Map will contain two keys: TRUE one, which will contain list of our stream elements for which Predicate returned TRUE, and FALSE one — opposite. In our case, our predicate will put element in TRUE partition if person’s age is greater or equal to 18, and FALSE otherwise.

In our groupingBy(people) method, we demonstrate an additional grouping mechanism by calling the  Collectors#groupingBy(Function<? super T,? extends K>) method. When we call Stream#collect with this kind of collector, the returned value is Map which key is of the resulting type of our Function which we supply to the  groupingBy method. In our case, we passed Function as person -> person.getName().length() , thus our key will be of the  Integer type. Basically, what happens here is that for each element, the function we supply will be applied, and the resulting value will be key in the resulting Map , and the current element of the stream will be in the List value of that Map . In our case, all stream elements that have the same length of name attribute will be in the List that belongs to the key with that length as a value. So, if we have 3 people named Joe, Jack, and John, our resulting map will contain two keys: 3 and 4. Key 3 will have a list of persons with only Joe inside, whereas key 4 will point to the list containing Jack and John, since they have name 4 characters long.

In a maxBy(people) example, we demonstrate how find the max element of a stream by calling Collectors#maxBy , based on a custom Comparator we provided. In our case, we provided Comparator as a lambda expression:

(p1, p2) -> {     final int p1NameLength = p1.getName().length();     final int p2NameLength = p2.getName().length();     return p1NameLength - p2NameLength; }

In our case, we’re comparing name lengths, but logic can be any logic returning an integer value, as per the documented requirements of the Comparator interface. In our case, the max element will be the first element of collection that has longest name.

Last, but not the least, in our collectingAndThen(people) method we demonstrate usage of Collectors#collectingAndThen method. It receives two arguments: the first one is Collector itself, and the other one is Function . Our example is:

try {     final List<Person> unmodifiableList = people.stream()             .collect(Collectors.collectingAndThen(toList(), Collections::unmodifiableList));     unmodifiableList.add(new Person(2, "name")); } catch (UnsupportedOperationException ex) {     ex.printStackTrace(); }

Basically, we’re passing Collectors.toList() collector as a first argument, followed by Collections::unmodifiableList method reference. This example just converts underlying collection to List and afterwards converts that list ot immutable list, which is quite handy trick to ensure good practice of data immutability.

Source code

Stay tuned and please – don’t forget to subscribe in case you’re eager to find out what’s coming next in upcoming posts.

转载本站任何文章请注明:转载至神刀安全网,谢谢神刀安全网 » Transforming Collections — Java 8 Collectors API

分享到:更多 ()

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
分享按钮