Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/386.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java:从队列读取数据并转换为所需的输出_Java_List_Hashmap_Time Complexity_Space Complexity - Fatal编程技术网

Java:从队列读取数据并转换为所需的输出

Java:从队列读取数据并转换为所需的输出,java,list,hashmap,time-complexity,space-complexity,Java,List,Hashmap,Time Complexity,Space Complexity,我有一个固定大小的阻塞队列,其中包含Customer对象。队列大小为1000 public class Customer { private final String accountId; private Double value = null; private Customer(final String accountId, Double value) { this.accountId = accountId; this.value= value; }

我有一个固定大小的阻塞队列,其中包含Customer对象。队列大小为1000

public class Customer {
private final String accountId;
private Double value = null;

private Customer(final String accountId, Double value) {
        this.accountId = accountId;
        this.value= value;
    }
}
有两个线程正在运行。主线程将这个Customer对象放入队列A中,另一个线程正在从队列中轮询Customer对象

第二个线程需要在hashmap映射中处理这些customers对象,其中键是accountId,值是记录对象

public class Record {
   List<Double> values;
   List<Double> count;
}
预期产量为

{"111", Record {[8.0, 9.0, 10.0], [2.0, 3.0, 1.0]}}
{"222" Record {[2.0, 3.0], [1.0, 2.0]}

我实现它的方法是首先在列表中添加所有值,计算列表中所有元素的出现频率,并从列表中删除重复项。但这不是一个好的解决方案,因为它占用了大量内存。你能给我建议一个优化的方法吗。谢谢

在处理值时,首先更改逻辑以保留计数。其次,将values对象的类型更改为
,这样您就永远不会保留任何超出需要的值。
集合
将考虑唯一性

因此,请保留一个跟踪数据的对象:

public class TrackingData {
   String customerId
   Set<Double> values;
   int count;
}
您现在可能不需要同步逻辑,但如果您曾经执行此处理(通常从队列读取),您将需要它


如果必须在内存中保留所有唯一值,最终会遇到问题

我无法更改记录对象。hashmap是API的参数。我们希望轮询队列A中的客户对象并创建一个哈希映射@好的。保留与记录不同的对象。我更新了答案。
public class TrackingData {
   String customerId
   Set<Double> values;
   int count;
}
synchronized(customerId) {
   TrackingData customerRecord = trackingRecords.get(customerId);
   customerRecord.values.add(value);
   customerRecord.count++;
}