If you are using Java 8, you can do the following:

Set<String> attributes = Sets.newHashSet("aaa", "bbb", "ccc", "ddd");
Set<String> activeAttributes = Sets.newHashSet("eee", "lll", "ccc", "mmm");
Map<String, Boolean> map = attributes.stream().collect(Collectors.toMap(s -> s, activeAttributes::contains));
System.out.println(map);

For an earlier version of Java and with Guava, you can use Maps.asMap since Guava 14.0:

Map<String, Boolean> map = Maps.asMap(
    attributes, Functions.forPredicate(Predicates.in(activeAttributes)));

Note that this returns a live copy (any changes to the set will be reflected on the map). If you want an immutable Map, use Maps.toMap.

Answer from Tunaki on Stack Overflow
๐ŸŒ
DZone
dzone.com โ€บ coding โ€บ java โ€บ converting collections to maps with jdk 8
Converting Collections to Maps With JDK 8
January 15, 2018 - 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.
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ java collections โ€บ java map โ€บ java collectors tomap
Java Collectors toMap | Baeldung
February 26, 2026 - Collector<T, ?, M> toMap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper, BinaryOperator<U> mergeFunction, Supplier<M> mapSupplier) where the mapSupplier is a function that returns a new, empty Map with the results. Letโ€™s take the same example and add a mapSupplier function to return a ConcurrentHashMap:
๐ŸŒ
Blogger
marxsoftware.blogspot.com โ€บ 2018 โ€บ 01 โ€บ jdk8-collections-maps.html
Inspired by Actual Events: Converting Collections to Maps with JDK 8
January 11, 2018 - 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.
๐ŸŒ
Blogger
javarevisited.blogspot.com โ€บ 2016 โ€บ 04 โ€บ 10-examples-of-converting-list-to-map.html
10 Examples of Converting a List to Map in Java 8
In the past, we have seen how to use the Collectors.groupingBy() method to group elements in Set and In this article, we will use Collectors.toMap() method to convert a List of an object into a Map in Java.
๐ŸŒ
javathinking
javathinking.com โ€บ blog โ€บ convert-collection-to-map-java
Convert Collection to Map in Java โ€” javathinking.com
Null Keys or Values: Maps in Java do not allow null keys (except for HashMap which allows one null key). If your collection contains elements that would result in null keys or values, you need to handle them appropriately. Use Stream API for Conciseness: When working with Java 8 or later, the Stream API provides a more concise and functional way to convert a collection to a map.
Find elsewhere
๐ŸŒ
Medium
medium.com โ€บ @alxkm โ€บ converting-a-list-to-a-map-in-java-multiple-approaches-1cf111e828cc
Java Interview: Converting a List to a Map in Java: Multiple Approaches | by Alex Klimenko | Medium
July 11, 2025 - Map<Integer, String> userMap = users.stream() .collect(Collectors.toMap( User::getId, User::getName, (v1, v2) -> v1, LinkedHashMap::new )); The Stream API is ideal for converting a List to a Map in a concise and flexible way, especially for ...
๐ŸŒ
Stack Abuse
stackabuse.com โ€บ guide-to-java-8-collectors-tomap
Guide to Java 8 Collectors: toMap()
March 30, 2023 - public static <T,K,U,M extends Map<K,U>> Collector<T,?,M> toMap(Function<? super T,? extends K> keyMapper, Function<? super T,? extends U> valueMapper, BinaryOperator<U> mergeFunction, Supplier<M> mapSupplier) The mapSupplier function specifies the particular implementation of Map we want to use as our final Map. When we use Map to declare our maps, Java defaults to using a HashMap as the implementation to store them.
Top answer
1 of 6
6

It you don't need a parallel stream you can use the length of the map as an index counter:

collection.stream().forEach(i -> map.put(i, map.size() + 1));
2 of 6
6

Here's an approach:

List<String> list = Arrays.asList("a", "b", "c");

Map<String, Integer> map =
    IntStream.range(0, list.size())
        .boxed()
        .collect(toMap(i -> list.get(i), i -> i));

Not necessarily a one-liner or shorter than the straightforward loop, but it does work using a parallel stream if you change toMap to toConcurrentMap.

Also note, this assumes that you have a random-access list, not a general Collection. If you have a Collection that you otherwise can make no assumptions about, there's not much you can do other than to iterate over it sequentially and increment a counter.

UPDATE

The OP has clarified that the input is a Collection and not a List so the above doesn't apply. It seems that we can assume very little about the input Collection. The OP has specified iteration order. With a sequential iterator, the elements will come out in some order although no guarantees can be made about it. It might change from run to run, or even from one iteration to the next (though this would be unusual in practice -- unless the underlying collection is modified).

If the exact iteration order needs to be preserved, I don't believe there's a way to preserve it into the result Map without iterating the input Collection sequentially.

If, however, the exact iteration order isn't important, and the requirement is that the output Map have unique values for each input element, then it would be possible to do something like this in parallel:

Collection<String> col = apiCall();
Iterator<String> iter = col.iterator();

Map<String, Integer> map =
    IntStream.range(0, col.size())
        .parallel()
        .boxed()
        .collect(toConcurrentMap(i -> { synchronized (iter) { return iter.next(); }},
                                 i -> i));

This is now far from a one-liner. It's also not clear to me how useful it is. :-) But it does demonstrate that it's possible to do something like this in parallel. Note that we've had to synchronize access to the input collection's iterator since it will be called from multiple threads. Also note that this is an unusual use of the iterator, since we never call hasNext and we assume that it is safe to call next exactly the number of times returned by the input collection's size().

๐ŸŒ
Mkyong
mkyong.com โ€บ home โ€บ java8 โ€บ java 8 โ€“ convert list to map
Java 8 โ€“ Convert List to Map | mkyong.com
May 26, 2017 - Hosting.java ยท package ... = websites; } //getters, setters and toString() } Create a list of the Hosting objects, and uses Collectors.toMap to convert it into a Map....
๐ŸŒ
Java Guides
javaguides.net โ€บ 2018 โ€บ 12 โ€บ java-8-convert-list-to-map-example.html
Java: Convert List to Map Example
May 30, 2024 - Converting a List to a Map is a common task in Java, and Java 8 provides several ways to accomplish this using the Stream API. This guide will cover different ways to convert a List to a Map, including using the Collectors.toMap method with various ...
๐ŸŒ
javathinking
javathinking.com โ€บ blog โ€บ convert-list-to-map-java-8
Convert List to Map in Java 8 โ€” javathinking.com
When converting a List to a Map, we can use the stream() method to convert the List into a stream and then perform operations on it. The Collectors.toMap() method is a collector that accumulates elements into a Map.
๐ŸŒ
Java Code Geeks
javacodegeeks.com โ€บ home โ€บ core java
Converting Collections to Maps with JDK 8 - Java Code Geeks
January 12, 2018 - I have run into the situation 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.
๐ŸŒ
ConcretePage
concretepage.com โ€บ java โ€บ jdk-8 โ€บ java-8-convert-list-to-map-using-collectors-tomap-example
Java Convert List to Map using Collectors.toMap()
package com.concretepage; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.stream.Collectors; public class ListToMapWithBinaryOperator { public static void main(String[] args) { List&ltPerson&gt list = new ArrayList&lt&gt(); list.add(new Person(100, "Mohan")); list.add(new Person(100, "Sohan")); list.add(new Person(300, "Mahesh")); Map&ltInteger, String&gt map = list.stream() .collect(Collectors.toMap(Person::getId, Person::getName, (x, y) -&gt x+", "+ y)); map.forEach((x, y) -&gt System.out.println("Key: " + x +", value: "+ y)); } } Output
๐ŸŒ
DZone
dzone.com โ€บ coding โ€บ java โ€บ how to convert list to map in java
How to Convert List to Map in Java - DZone
July 23, 2018 - With Java 8, you can convert a List to Map in one line using the stream() and Collectors.toMap() utility methods.
๐ŸŒ
Medium
medium.com โ€บ @AlexanderObregon โ€บ javas-collectors-tomap-method-explained-f95999d4ebe6
Java's Collectors.toMap() Method Explained
October 20, 2024 - This can be particularly useful when you need to transform a collection into a key-value data structure, allowing for more efficient data retrieval based on keys. The most common form of toMap() takes two arguments: A key mapper function, which extracts the key from the stream elements.
๐ŸŒ
EDUCBA
educba.com โ€บ home โ€บ software development โ€บ software development tutorials โ€บ java 8 tutorial โ€บ java 8 list to map
Java 8 List to Map | Examples on How to Convert List to Map in Java 8
February 10, 2023 - The key value is duplicate, but the duplicate key causes a problem in Java 8. So, by handling the duplicate values, we can say that the list with duplicates is not directly converted into the map. The list is an ordered collection, but the map does not take that guarantee until we use a linked hash map, which keeps order insertion, or a tree map, which keeps mapping in sorted order.
Address: Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai