문제

Usually people say we should use EnumMap when the key is an enum. But what about this case I want to count the frequencies of each enum value, it seems like trove4j TObjectIntHashMap is better in code.

The following is the example with trove4j collection:

TObjectIntMap<ExtraOperator> extraOpr = new TObjectIntHashMap<ExtraOperator>();
extraOpr.adjustOrPutValue(ExtraOperator.ternary, 1, 1);

In case of EnumMap, the code looks like:

Map<ExtraOperator, Integer> extraOpr = Maps.newEnumMap(ExtraOperator.class);
if (extraOpr.containsKey(ExtraOperator.ternary)) {
    extraOpr.put(ExtraOperator.ternary, extraOpr.get(ExtraOperator.ternary) + 1);
} else {
    extraOpr.put(ExtraOperator.ternary, 1);
}

So, trove4j checks existence internally and can auto-increment the value, which makes the code more concise. EnumMap use enum as key has higher performance but the retrieval and increment of the Integer(boxing and unboxing) also cost time.

Which one would be better if we consider low memory cost and fast speed?

도움이 되었습니까?

해결책

If you want lower cost/faster you could use a plain array.

 static final ExtraOperator[] VALUES = ExtraOperator.values();

 private final int[] counts = new int[VALUES.length];

 // to count.
 counts[e.ordinal()]++;
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top