Convert List<String> to Map in Java using Stream API
To convert a list of strings to a map in Java, you can utilize the Stream API. Here's an example of how to achieve this:
List<String> data = Org.getResult().getData();
Map<String, Integer> map = data.stream()
.collect(Collectors.toMap(Function.identity(), String::length));
In this example, each string within the 'data' list is transformed into a key in the resulting 'map'. The corresponding value for each key is determined by the length of the string. You have the flexibility to modify the value mapping function (String::length) based on your specific requirements.
原文地址: https://www.cveoy.top/t/topic/o1UB 著作权归作者所有。请勿转载和采集!