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

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

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

Related

  • Jakarta WebSocket Essentials: A Guide to Full-Duplex Communication in Java
  • Java Is Greener on Arm
  • Mastering Date/Time APIs: Challenges With Java's Calendar and JDK Date/Time APIs
  • Java 23: What Developers Need to Know

Trending

  • How to Ensure Cross-Time Zone Data Integrity and Consistency in Global Data Pipelines
  • Understanding IEEE 802.11(Wi-Fi) Encryption and Authentication: Write Your Own Custom Packet Sniffer
  • Unit Testing Large Codebases: Principles, Practices, and C++ Examples
  • How Kubernetes Cluster Sizing Affects Performance and Cost Efficiency in Cloud Deployments
  1. DZone
  2. Coding
  3. Java
  4. Converting Collections to Maps With JDK 8

Converting Collections to Maps With JDK 8

Java 8 makes it easy to convert Lists, Sets, and arrays to Maps. Here are a couple of potential use cases and a step-by-step guide.

By 
Dustin Marx user avatar
Dustin Marx
·
Updated Jan. 15, 18 · Tutorial
Likes (8)
Comment
Save
Tweet
Share
31.5K Views

Join the DZone community and get the full member experience.

Join For Free

I have run into situations several times where it is desirable to store multiple objects in a Map instead of a Set or List because there are some advantages from using a Map of unique identifying information to the objects. Java 8 has made this translation easier than ever with streams and the Collectors.toMap(...) methods.

One situation in which it has been useful to use a Map instead of a Set is when working with objects that lack or have sketchy equals(Object) or hashCode() implementations, but do have a field that uniquely identifies the objects. In those cases, if I cannot add or fix the objects' underlying implementations, I can gain better uniqueness guarantees by using a Map of the uniquely identifying field of the class (key) to the class's instantiated object (value). Perhaps a more frequent scenario when I prefer Map to List or Set is when I need to lookup items in the collection by a specific uniquely identifying field. A map lookup on a uniquely identifying key is speedy and often much faster than depending on iteration and comparing each object with an invocation to the equals(Object) method.

With JDK 8, it's easier than ever to construct a Map from an existing List or Set. To help demonstrate this, a simple Book class will be used. This Book class does not override equals(Object) or hashCode() from the Object class and so is not an appropriate class to use in a Set or as a Map key. However, its getIsbn() method returns an International Standard Book Number that is assumed unique for purposes of this demonstration.

Book.java:

package dustin.examples.jdk8;

/**
 * Represents a book, but does not override {@code equals(Object)}
 * or {@code hashCode()}.
 */
public class Book {
    /** International Standard Book Number (ISBN-13). */
    final String isbn;

    /** Title of book. */
    final String title;

    /** Edition of book. */
    final int edition;

    /**
     * Constructor.
     *
     * @param newIsbn International Standard Book Number (-13).
     * @param newTitle Title.
     * @param newEdition Edition.
     */
    public Book(final String newIsbn, final String newTitle, final int newEdition) {
        isbn = newIsbn;
        title = newTitle;
        edition = newEdition;
    }

    /**
     * Provide ISBN-13 identifier associated with this book.
     *
     * @return ISBN-13 identifier.
     */
    public String getIsbn() {
        return isbn;
    }

    /**
     * Provide title of this book.
     *
     * @return Book's title.
     */
    public String getTitle() {
        return title;
    }

    /**
     * Provide edition of this book.
     *
     * @return Book's edition.
     */
    public int getEdition() {
        return edition;
    }

    @Override
    public String toString() {
        return title + " (Edition " + edition + ") - ISBN-13: " + isbn;
    }
}


With this class in place, the demonstration class CollectionToMapDemo shows how easy it is with JDK 8 to convert various Java collection types (Set, List, and even arrays) to a Map.

CollectionToMapDemo.java:

package dustin.examples.jdk8;

import static java.lang.System.out;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * Demonstrates conversion of Java collections to Java Maps.
 */
public class CollectionToMapDemo {
    /**
     * Multiple instances of Book, a class that lacks a proper
     * equals(Object) method, but for which its getIsbn() method
     * is assumed to return a unique identifier for each instance.
     */
    private static final Book[] books;

    static {
        books = new Book[] {
            new Book("978-0-201-31005-4", "Effective Java", 1),
                new Book("978-0-321-35668-0", "Effective Java", 2),
                new Book("978-0-13-468599-1", "Effective Java", 3)
        };
    }

    /**
     * Convert provided array of Book instances to Map of each Book's ISBN to
     * that instance of the Book.
     * 
     * @param booksArray Array of Book instances.
     * @return Map of each book's ISBN (key) to the book's full instance (value).
     */
    private static Map < String, Book > convertArrayToMap(final Book[] booksArray) {
        return Arrays.stream(booksArray).collect(
            Collectors.toMap(Book::getIsbn, book - > book));
    }

    /**
     * Convert provided List of Book instances to Map of each Book's ISBN to
     * that instance of the Book.
     *
     * @param booksList List of Book instances.
     * @return Map of each book's ISBN (key) to the book's full instance (value).
     */
    private static Map < String, Book > convertListToMap(final List < Book > booksList) {
        return booksList.stream().collect(
            Collectors.toMap(Book::getIsbn, book - > book));
    }

    /**
     * Convert provided Set of Book instances to Map of each Book's ISBN to
     * that instance of the Book.
     *
     * @param booksSet Set of Book instances.
     * @return Map of each book's ISBN (key) to the book's full instance (value).
     */
    private static Map < String, Book > convertSetToMap(final Set < Book > booksSet) {
        return booksSet.stream().collect(
            Collectors.toMap(Book::getIsbn, book - > book));
    }

    public static void main(final String[] arguments) {
        out.println("ARRAY->MAP:\n" + convertArrayToMap(books));

        final List < Book > booksList = Arrays.asList(books);
        out.println("LIST->MAP:\n" + convertListToMap(booksList));

        final Set < Book > booksSet = new HashSet < > (Arrays.stream(books).collect(Collectors.toSet()));
        out.println("SET->MAP:\n" + convertSetToMap(booksSet));
    }
}


The most important methods in the class listing just shown are convertArrayToMap(Book[]), convertListToMap(List<Book>), and convertSetToMap(Set<Book>). All three implementations are the same once a stream based on the underlying Set, List, or array has been accessed. In all three cases, it's merely a matter of using one of the stream's collect() method (a reduction operator that is typically preferable over sequential iteration) and passing it an implementation of the Collector interface that is provided via a predefined toMap() Collector from the Collectors class.

The output from running this demonstration class against the instances of Book is shown next:

ARRAY->MAP:
{978-0-201-31005-4=Effective Java (Edition 1) - ISBN-13: 978-0-201-31005-4, 978-0-321-35668-0=Effective Java (Edition 2) - ISBN-13: 978-0-321-35668-0, 978-0-13-468599-1=Effective Java (Edition 3) - ISBN-13: 978-0-13-468599-1}
LIST->MAP:
{978-0-201-31005-4=Effective Java (Edition 1) - ISBN-13: 978-0-201-31005-4, 978-0-321-35668-0=Effective Java (Edition 2) - ISBN-13: 978-0-321-35668-0, 978-0-13-468599-1=Effective Java (Edition 3) - ISBN-13: 978-0-13-468599-1}
SET->MAP:
{978-0-201-31005-4=Effective Java (Edition 1) - ISBN-13: 978-0-201-31005-4, 978-0-321-35668-0=Effective Java (Edition 2) - ISBN-13: 978-0-321-35668-0, 978-0-13-468599-1=Effective Java (Edition 3) - ISBN-13: 978-0-13-468599-1}


I have run into several situations in which it has been advantageous to have multiple objects in a Map of unique identifier to full instance of those objects, but have been given the objects in a Set, List, or array. Although it's never been particularly difficult to convert these Sets, Lists, and arrays to Maps in Java, it's easier than ever in Java 8 to make this conversion.

Java (programming language) Java Development Kit

Published at DZone with permission of Dustin Marx, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Jakarta WebSocket Essentials: A Guide to Full-Duplex Communication in Java
  • Java Is Greener on Arm
  • Mastering Date/Time APIs: Challenges With Java's Calendar and JDK Date/Time APIs
  • Java 23: What Developers Need to Know

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!