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
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
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

Modern Digital Website Security: Prepare to face any form of malicious web activity and enable your sites to optimally serve your customers.

Containers Trend Report: Explore the current state of containers, containerization strategies, and modernizing architecture.

Low-Code Development: Learn the concepts of low code, features + use cases for professional devs, and the low-code implementation process.

E-Commerce Development Essentials: Considering starting or working on an e-commerce business? Learn how to create a backend that scales.

Related

  • Quarkus 3: The Future of Java Microservices With Virtual Threads and Beyond
  • A Maven Story
  • Ways To Reduce JVM Docker Image Size
  • The Resurrection of Virtual Threads: Unraveling Their Journey From JDK 1.1 to JDK 21

Trending

  • What Is CI/CD? Beginner’s Guide To Continuous Integration and Deployments
  • Amazon EC2 Deep Dive: Optimizing Workloads With Hardware Insights
  • The Future of Java: Virtual Threads in JDK 21 and Their Impact
  • Agile Testing: Blending Shift-Left, Automation, and Collaborative Testing Strategies
  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.

Dustin Marx user avatar by
Dustin Marx
·
Updated Jan. 15, 18 · Tutorial
Like (8)
Save
Tweet
Share
30.4K 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

  • Quarkus 3: The Future of Java Microservices With Virtual Threads and Beyond
  • A Maven Story
  • Ways To Reduce JVM Docker Image Size
  • The Resurrection of Virtual Threads: Unraveling Their Journey From JDK 1.1 to JDK 21

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • 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: