You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Eclipse Collections将MutableMap<String,Double>转为ObjectDoubleMap<String>

Convert MutableMap<String, Double> to ObjectDoubleMap with Eclipse Collections

Got it, let's break down how to solve this conversion problem using Eclipse Collections—this framework is built for optimizing primitive type handling, so we can avoid unnecessary autoboxing/unboxing overhead while making the conversion clean.

Option 1: Convert an existing MutableMap directly

If you already have your MutableMap<String, Double> instance ready, use Eclipse Collections' utility methods to convert it to an ObjectDoubleMap<String> efficiently.

Basic Conversion (no null values)

If you’re confident your map has no null Double entries, use Maps.mutable.toObjectDoubleMap with a simple unboxing function:

import org.eclipse.collections.api.map.primitive.ObjectDoubleMap;
import org.eclipse.collections.impl.factory.Maps;

// Your existing boxed map
MutableMap<String, Double> boxedMap = list.aggregateBy(func, factory, aggregator);

// Convert to primitive-optimized ObjectDoubleMap
ObjectDoubleMap<String> primitiveMap = Maps.mutable.toObjectDoubleMap(boxedMap, Double::doubleValue);

Handle Null Double Values

If your map might contain null Double values (since Double is nullable), you’ll need to handle these explicitly to avoid NullPointerException. For example, default null values to 0.0 (adjust the default to match your business logic):

ObjectDoubleMap<String> primitiveMap = Maps.mutable.toObjectDoubleMap(
    boxedMap,
    value -> value != null ? value.doubleValue() : 0.0
);

You can also iterate manually to populate an ObjectDoubleHashMap directly:

import org.eclipse.collections.impl.map.mutable.primitive.ObjectDoubleHashMap;

ObjectDoubleMap<String> primitiveMap = new ObjectDoubleHashMap<>();
boxedMap.forEach((key, value) -> {
    double primitiveValue = value != null ? value.doubleValue() : 0.0;
    primitiveMap.put(key, primitiveValue);
});

Option 2: Generate ObjectDoubleMap directly during aggregation (better performance)

Instead of converting after the fact, modify your aggregation code to produce an ObjectDoubleMap<String> directly. This skips the boxing/unboxing step entirely, which is way more efficient for large datasets.

Use ObjectDoubleHashMap::new as the factory, and write your aggregator to work with primitive double values:

import org.eclipse.collections.api.map.primitive.ObjectDoubleMap;
import org.eclipse.collections.impl.map.mutable.primitive.ObjectDoubleHashMap;

// Aggregate directly to primitive-optimized map
ObjectDoubleMap<String> primitiveMap = list.aggregateBy(
    item -> item.getGroupKey(), // Your grouping function (func)
    ObjectDoubleHashMap::new, // Factory for ObjectDoubleMap
    (resultMap, item) -> {
        // Aggregation logic using primitive double
        double currentValue = resultMap.get(item.getGroupKey());
        resultMap.put(item.getGroupKey(), currentValue + item.getNumericValue());
    }
);

This approach is the best choice when possible—it leverages Eclipse Collections' primitive-optimized collections from the start, eliminating unnecessary overhead.


内容的提问来源于stack exchange,提问作者fabfas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 07:34:30