我有一个问题,我需要聚合一些向量,以便找到一些统计数据。例如,我有双打的向量,我需要总结它们。我的矢量看起来像这样:
1,0,3,4,5
2,3,4,5,6
3,4,5,5,6
到目前为止我的键值对是(String,String)。但每次我需要添加这些向量时,我首先必须将它们转换为双数组,添加它们,最后将聚合向量转换为字符串。我认为如果我只能在表单中使用键值对(String,double array)会快得多。没有必要来回转换它们。我的问题是我找不到将双数组作为值的方法。有没有简单的方法而不是创建新的自定义类型?
答案 0 :(得分:3)
你的意思是这样的吗?
Map<String, List<Double>> arrays = new HashMap<String, List<Double>>();
double[] array;
arrays.put("ArrayKey", Arrays.asList(array));
然后你可以调用你的地图方法:
map(String key, String arrayKey) {
List<Double> value = arrays.get(arrayKey);
}
此外,您可以序列化双数组,然后将其反序列化:
package test;
import org.apache.commons.codec.binary.Base64InputStream;
import org.apache.commons.codec.binary.Base64OutputStream;
import java.io.*;
import java.util.Arrays;
public class Test {
public static void main(String[] args) throws IOException, ClassNotFoundException {
double[] array = {0.0, 1.1, 2.2, 3.3};
String stringValue = serialize(array);
map("Key", stringValue);
}
public static void map(String key, String value) throws ClassNotFoundException, IOException {
double[] array = deserialize(value);
System.out.println("Key=" + key + "; Value=" + Arrays.toString(array));
}
public static String serialize(double[] array) throws IOException {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
Base64OutputStream base64OutputStream = new Base64OutputStream(byteArrayOutputStream);
ObjectOutputStream oos = new ObjectOutputStream(base64OutputStream);
oos.writeObject(array);
oos.flush();
oos.close();
return byteArrayOutputStream.toString();
}
public static double[] deserialize(String stringArray) throws IOException, ClassNotFoundException {
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(stringArray.getBytes());
Base64InputStream base64InputStream = new Base64InputStream(byteArrayInputStream);
ObjectInputStream iis = new ObjectInputStream(base64InputStream);
return (double[]) iis.readObject();
}
}
输出:
Key=Key; Value=[0.0, 1.1, 2.2, 3.3]
映射速度更快,但如果您使用节点和集群(如果需要将数组传递到另一个JVM),序列化将更有用:
private static class SpeedTest {
private static final Map<String, List> arrays = new HashMap<String, List>();
public static void test(final double[] array) throws IOException, ClassNotFoundException {
final String str = serialize(array);
final int amount = 10 * 1000;
long timeStamp = System.currentTimeMillis();
for (int i = 0; i < amount; i++) {
serialize(array);
}
System.out.println("Serialize: " + (System.currentTimeMillis() - timeStamp) + " ms");
timeStamp = System.currentTimeMillis();
for (int i = 0; i < amount; i++) {
deserialize(str);
}
System.out.println("Deserialize: " + (System.currentTimeMillis() - timeStamp) + " ms");
arrays.clear();
timeStamp = System.currentTimeMillis();
// Prepaire map, that contains reference for all arrays.
for (int i = 0; i < amount; i++) {
arrays.put("key_" + i, Arrays.asList(array));
}
// Getting array by its key in map.
for (int i = 0; i < amount; i++) {
arrays.get("key_" + i).toArray();
}
System.out.println("Mapping: " + (System.currentTimeMillis() - timeStamp) + " ms");
}
}
<强>输出:强>
Serialize: 298 ms
Deserialize: 254 ms
Mapping: 27 ms