# Java 8 List<V> into Map<K, V>

To convert a `List<V>` into a `Map<K, V>` in Java 8, you can use the `toMap()` method of the `Collectors` class from the `java.util.stream` package.

The `toMap()` method takes a function that extracts the key from an element and a function that extracts the value, and returns a `Collector` that accumulates elements into a `Map`. You can then use this collector with the `collect()` method of the `Stream` interface to create the map.

Here is an example of how to convert a `List<V>` into a `Map<K, V>` in Java 8:

``````import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class Main {
public static void main(String[] args) {
List<V> list = ...;
Map<K, V> map = list.stream()
.collect(Collectors.toMap(
element -> element.getKey(),  // Function to extract the key from an element
element -> element.getValue()  // Function to extract the value from an element
));
}
}``````

This code creates a stream of the elements in the `list` using the `stream()` method and then collects them into a `Map` using the `toMap()` method and the `collect()`
{"ft_lang_label":"__label__en","ft_lang_prob":0.5941078,"math_prob":0.73806965,"size":1060,"snap":"2023-14-2023-23","text_gpt3_token_len":263,"char_repetition_ratio":0.14204545,"word_repetition_ratio":0.09195402,"special_character_ratio":0.25377357,"punctuation_ratio":0.14864865,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9574905,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-06-02T16:23:19Z\",\"WARC-Record-ID\":\"<urn:uuid:9ae7e124-efa1-4fc3-91fa-50ceb47a2da9>\",\"Content-Length\":\"37011\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:dc9cfa7a-a7ab-41f0-a6f0-d67ed07df6d2>\",\"WARC-Concurrent-To\":\"<urn:uuid:61dc5268-137f-4f24-a0b8-c77f88410cc9>\",\"WARC-IP-Address\":\"172.67.69.171\",\"WARC-Target-URI\":\"https://www.w3docs.com/snippets/java/java-8-list-v-into-map-k-v.html\",\"WARC-Payload-Digest\":\"sha1:2C66JTHM3ANTHKJKCWGHRCSSEMNSE4FF\",\"WARC-Block-Digest\":\"sha1:EVZBHJGCFDPIPLA3TIOSCK3ZHSDU5N5K\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-23/CC-MAIN-2023-23_segments_1685224648695.4_warc_CC-MAIN-20230602140602-20230602170602-00107.warc.gz\"}"}