简体   繁体   English

使用Java-8 Streams API将字符串列表转换为Map

[英]Convert List of Strings into Map using Java-8 Streams API

I have List 我有名单

List<String> cars = Arrays.asList("Ford", "Focus", "Toyota", "Yaris","Nissan", "Micra", "Honda", "Civic");

Now, can I convert this List into Map where I get ford = focus, Toyota = yaris, Nisan = Micra, Honda = Civic using Java 8 Streams API? 现在,我可以将此List转换为Map,其中我获得ford = focus,Toyota = yaris,Nisan = Micra,Honda = Civic使用Java 8 Streams API?

Here is an example on how you could do it : 这是一个如何做到这一点的例子:

 Map<String, String> carsMap =
            IntStream.iterate(0, i -> i + 2).limit(cars.size() / 2)
                    .boxed()
                    .collect(Collectors.toMap(i -> cars.get(i), i -> cars.get(i + 1)));

Basically, just iterates over every 2 elements and maps it with the next one. 基本上,只需迭代每2个元素并将其映射到下一个元素。
Note that if the number of elements is not even, it won't take into consideration the last element. 请注意,如果元素数不均匀,则不会考虑最后一个元素。

转换列表<object>至 Map <integer, list<object> &gt; 使用 Java 8 流 API<div id="text_translate"><p> 我有List&lt;Object&gt; ,我想将其转换为Map&lt;Integer, List&lt;Object&gt;&gt; ,其中 key 是列表的大小,value 是使用 java 8 个流 api 的列表本身。 我可以使用 Java 7 作为</p><pre>Map&lt;Integer, List&lt;Object&gt;&gt; map= new HashMap&lt;&gt;(); map.put(list.size(), list);</pre><p> 但是如何使用 Java 8 来做到这一点。</p></div></integer,></object> - Convert List<Object> to Map<Integer, List<Object>> using Java 8 Streams API

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java-8流:转换地图 <String, List<List<DataType> &gt;&gt;到地图 <String, List<DataType> &gt; - Java-8 Streams: Convert Map<String, List<List<DataType>>> to Map<String, List<DataType>> Java-8 流:转换 List&lt;{String,List<string> }&gt; 到 Map <string, list<string> &gt;</string,></string> - Java-8 Streams: Convert List<{String,List<String>}> to Map<String, List<String>> 如何使用 Java-8 Streams 将列表转换为 Map,其中 Key 是 ListValue,Value 是该特定列表的值的数量 - How does one Convert a List to Map with Java-8 Streams where Key is the ListValue and the Value is the amount of values of that specific list 转换列表<object>至 Map <integer, list<object> &gt; 使用 Java 8 流 API<div id="text_translate"><p> 我有List&lt;Object&gt; ,我想将其转换为Map&lt;Integer, List&lt;Object&gt;&gt; ,其中 key 是列表的大小,value 是使用 java 8 个流 api 的列表本身。 我可以使用 Java 7 作为</p><pre>Map&lt;Integer, List&lt;Object&gt;&gt; map= new HashMap&lt;&gt;(); map.put(list.size(), list);</pre><p> 但是如何使用 Java 8 来做到这一点。</p></div></integer,></object> - Convert List<Object> to Map<Integer, List<Object>> using Java 8 Streams API 使用Java流将字符串列表转换为地图列表 - Convert a list of strings to a list of maps using Java streams 如何使用Java-8 Streams将n维数组映射到另一种类型? - How to map n-Dimensional Array to another type using Java-8 Streams? 使用 Java Streams API 将列表动态转换为组 map 与枚举键 - Using Java Streams API to dynamically convert a list into a group map with enum key 转换清单 <E> 到地图 <String, List<String> &gt;使用Java 8流 - convert List<E> to Map<String, List<String>> using java 8 streams 如何将字符串列表转换为带有流的地图 - How to convert a List of strings into a Map with Streams Java-8:Stream如何在地图上进行转换 <K, List<D> &gt;到地图 <D, List<K> &gt; - Java-8: Stream How to convert on Map<K, List<D>> to Map<D, List<K>>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM