DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • The Complete Guide to Stream API and Collectors in Java 8
  • Java Stream API: 3 Things Every Developer Should Know About
  • Optimizing Java Applications: Parallel Processing and Result Aggregation Techniques
  • Functional Approach To String Manipulation in Java

Trending

  • *You* Can Shape Trend Reports: Join DZone's Software Supply Chain Security Research
  • Build Your First AI Model in Python: A Beginner's Guide (1 of 3)
  • How to Build Local LLM RAG Apps With Ollama, DeepSeek-R1, and SingleStore
  • Scalable, Resilient Data Orchestration: The Power of Intelligent Systems
  1. DZone
  2. Data Engineering
  3. Databases
  4. Transforming Collections — Java 8 Collectors API

Transforming Collections — Java 8 Collectors API

A tutorial on how to use collector methods and APIs in Java 8.

By 
Dusan Odalovic user avatar
Dusan Odalovic
·
Apr. 03, 16 · Tutorial
Likes (5)
Comment
Save
Tweet
Share
12.7K Views

Join the DZone community and get the full member experience.

Join For Free

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 underlyingCollection toList 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.

API Element Stream (computing) Java (programming language)

Published at DZone with permission of Dusan Odalovic. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • The Complete Guide to Stream API and Collectors in Java 8
  • Java Stream API: 3 Things Every Developer Should Know About
  • Optimizing Java Applications: Parallel Processing and Result Aggregation Techniques
  • Functional Approach To String Manipulation in Java

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!