[英]java8 convert string array to map(odd index is key, even index is value)
Now I have a String array, 现在我有一个String数组,
String[] a= {"from","a@a.com","to","b@b.com","subject","hello b"};
from command line arguments. 从命令行参数。
I want to convert it to Map, 我想将它转换为Map,
{"from":"a@a.com","to":"b@b.com","subject":"hello b"}
Does exist convenient manner in java8 to achieve this? 在java8中是否存在方便的方式来实现这一点? Now my way is
现在我的方式是
Map<String,String> map = new HashMap<>();
for (int i = 0; i < args.length; i+=2) {
String key = args[i].replaceFirst("-+", ""); //-from --> from
map.put(key, args[i+1]);
}
You can use an IntStream
to iterate on the indices of the array (which is required in order to process two elements of the array each time) and use the Collectors.toMap
collector. 您可以使用
IntStream
迭代数组的索引(这是每次处理数组的两个元素所必需的)并使用Collectors.toMap
收集器。
The IntStream
will contain a corresponding index for each pair of elements of the input array. IntStream
将包含输入数组的每对元素的相应索引。 If the length of the array is odd, the last element will be ignored. 如果数组的长度为奇数,则将忽略最后一个元素。
Map<String,String> map =
IntStream.range(0,a.length/2)
.boxed()
.collect(Collectors.toMap(i->a[2*i].replaceFirst("-+", ""),
i->a[2*i+1]));
In Java 8 you can use Stream.iterate
to divide list to sublists of 2 elements 在Java 8中,您可以使用
Stream.iterate
将列表划分为2个元素的子列表
String[] a= {"from","a@a.com","to","b@b.com","subject","hello b"};
Map<String, String> map = Stream.iterate(
Arrays.asList(a), l -> l.subList(2, l.size()))
.limit(a.length / 2)
.collect(Collectors.toMap(
l -> l.get(0).replaceFirst("-+", ""),
l -> l.get(1))
);
Another recursive solution using simple Iterator
另一种使用简单
Iterator
递归解决方案
Map<String, String> map = buildMap(new HashMap<>(), Arrays.asList(a).iterator());
private static Map<String, String> buildMap(
Map<String, String> map, Iterator<String> iterator) {
if (iterator.hasNext()) {
map.put(iterator.next().replaceFirst("-+", ""), iterator.next());
createMap(map, iterator);
}
return map;
}
You can do this quite elegant with Javaslang : 你可以用Javaslang做到这一点非常优雅:
String[] a= {"from","a@a.com","to","b@b.com","subject","hello b"};
Map<String, String> map = Stream.of(a).grouped(2) // use javaslang.collection.Stream here
.map(group -> group.toJavaArray(String.class))
.toJavaStream() // this is the plain old java.util.Stream
.collect(toMap(tuple -> tuple[0], tuple -> tuple[1]));
The grouped
function groups your stream in groups of 2 elements. grouped
函数将您的流分组为2个元素组。 These can be transformed to string arrays and those can be the base of a Map
. 这些可以转换为字符串数组,这些可以作为
Map
的基础。 Probably Javaslang allows you to do even more elegant. 可能Javaslang可以让你做得更优雅。
The trick is to first join the string, then split it up into key/value pairs , then the task is simple: 诀窍是首先加入字符串,然后将其拆分为键/值对 ,然后任务很简单:
Map<String,String> map = Arrays.stream(
String.join(",", a)
.split(",(?!(([^,]*,){2})*[^,]*$)"))
.map(s -> s.split(","))
.collect(Collectors.toMap(s -> s[0], s -> s[1]))
;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.