小编Jam*_*mie的帖子

如何使用Java流将两个数组合并到一个映射中?

让我们假设我们得到了以下两个数组

String[] keys   = new String[] {"a", "b", "c", "aa", "d", "b"}
int[]    values = new int[]    { 1 ,  2 ,  3 ,  4  ,  5 ,  6 }
Run Code Online (Sandbox Code Playgroud)

通过将这两个数组合并到HashTable中,我们得到以下结果

// pseudo-code
Map<String, Integer> dictionary = new HashTable<>(
  ("a"  => 1)
  ("b"  => 8) // because "b" appeared in index 1 and 5
  ("c"  => 3)
  ("aa" => 4)
  ("d"  => 5)
);
Run Code Online (Sandbox Code Playgroud)

我们怎么能用java Lambda样式做到这一点?

到目前为止,我有以下内容:

// this loops through the range given (used for index start and end) …
Run Code Online (Sandbox Code Playgroud)

java arrays lambda hashtable java-stream

6
推荐指数
1
解决办法
1177
查看次数

标签 统计

arrays ×1

hashtable ×1

java ×1

java-stream ×1

lambda ×1