将字符串数组转换为Map

我有两个字符串数组键和值

String[] keys = {a,b,c,d}; String[] values = {1,2,3,4}; 

将它们转换为地图的最快方法是什么? 我知道我们可以遍历它们。 但是,有没有任何实用工具?

比这更快?

 Map map = new HashMap<>(); if(keys.length == values.length){ for(int index = 0; index < keys.length; index++){ map.put(keys[index], values[index]); } } 

恕我直言,你不太可能找到这样的实用工具。

但是,即使你发现一个机会真的很低,它也会提供任何性能提升。 因为,我认为如果不迭代两个数组中的所有元素,你将无法做到这一点。

我可以建议的一件事是(只有你的数组有大量的元素),你可以指定地图的容量,同时实例化它,以减少resize的开销。

 Map map = new HashMap(keys.length); //put keys and values into map ... 

从一开始就进行恒定时间查找

如果您正在寻找一个可以在恒定时间内检索与键相关联的值的Map(意味着无需查看大多数值),那么您就无法做得更快,因为需要处理数组。

但是,您可以使用已编写的实用程序: com.google.common.collect.Maps.uniqueIndex

瞬时转换,线性时间查找

如果您可以使用每次都在数组中搜索键的Map,那么您可以使用两个数组立即创建Map,方法是定义一个实现Map接口的新类:

 class TwoArrayMap implements Map { private final String[] keys; private final String[] values; // If you want to enable to add more key value pairs to your map, and // want to make the process faster, you could use ArrayLists instead of arrays public TwoArrayMap(String[] array1, String[] array2){ if(array1 == null || array2 == null || array2.length < array1.length) throw new IllegalArgumentException(); keys = array1; values = array2; // Alternatively, you could want to clone the arrays, to // make sure they are not modified, using array1.clone(), etc } public String get(String key){ for(int i=0; i myMap = new TwoArrayMap(keys, values); 

延迟转换,转换后的恒定时间查找

另一种方法是“懒惰地”执行它,这意味着修改上面的类,以便它在内部保持对HashMap的引用,并且只在查找元素时填充它:

 class TwoArrayMap implements Map { private final Map hashmap; private int maxIndexAlreadyTransferred = -1; private final String[] keys; private final String[] values; public TwoArrayMap(String[] array1, String[] array2){ if(array1 == null || array2 == null || array2.length < array1.length) throw new IllegalArgumentException(); hashmap = new HashMap<>(); keys = array1; values = array2; // Alternatively, you could want to clone the arrays, to // make sure they are not modified, using array1.clone(), etc } public String get(String key){ if(hashmap.containsKey(key)) return hashmap.get(key); String k, value; while( maxIndexAlreadyTransferred + 1 < keys.length ){ k = keys[ maxIndexAlreadyTransferred + 1 ]; value = values[ maxIndexAlreadyTransferred +1 ]; if(!hashmap.containsKey(k)) hashmap.put( k, value ); maxIndexAlreadyTransferred++; if(key == null && k == null || key != null && key.equals(k) ) return value; } return null; } public String put(String key, String Value) { hashmap.put(key, value); } } 

这个解决方案意味着:

  • 即时创建新对象
  • 线性时间查找第一次查询它,直到所有内容都被传输
  • 之后的常量时间查找,表现为哈希表

在Java中将两个String数组转换为Map

 import java.util.HashMap; public static void main(String[] args){ String[] keys= {"a", "b", "c"}; int[] vals= {1, 2, 3}; HashMap hash= new HashMap(); for(int i= 0; i < keys.length; i++){ hash.put(keys[i], vals[i]); } } 

查看此LINK以获取更多不同编程语言的解决方案

Note :键应该是唯一的..

我的目的是两个非常简单的实现。 一个是Java 8的流Api,一个没有。

Java <8 (没有流api)

 if(keys.length != values.length) { throw new IllegalArgumentException("Keys and Values need to have the same length."); } Map map = new HashMap<>(); for (int i = 0; i < keys.length; i++) { map.put(keys[i], values[i]); } 

Java> 8 (带流api)

 if(keys.length != values.length) { throw new IllegalArgumentException("Keys and Values need to have the same length."); } Map map = IntStream.range(0, keys.length).boxed() .collect(Collectors.toMap(i -> keys[i], i -> values[i]));