enum-map

Might EnumMap be considered a reasonable alternative to Java beans?

懵懂的女人 提交于 2020-01-04 06:00:37
问题 Curious if anybody has considered using EnumMap in place of Java beans, particularly "value objects" (with no behavior)? To me it seems that one advantage would be that the name of a "property" would be directly accessible from the backing Enum, with no need for reflection, and therefore I'd assume it would be faster. 回答1: It may be a little faster then using reflection (I didn't measure it, didn't find any metrics in Google either); however there are big disadvantages to this approach: You

How to build a Map that replicates a Function in Java's Lambda API

梦想与她 提交于 2020-01-02 07:01:12
问题 From a java.util.function.BiFunction that maps a pair of Enum s into a value, I want to build a EnumMap that reflects that mapping. For instance, let E1 and E2 be enum types and T any given type: BiFunction<E1,E2, T> theBiFunction = //...anything EnumMap<E1,EnumMap<E2,T>> theMap = buildTheMap( // <-- this is where the magic happens E1.values(), E2.values(), theBiFunction); Given any pair of values of type E1 and E2 E1 e1 = //any valid value... E2 e2 = //any valid value.... both values below

Why use EnumMap instead of HashMap

杀马特。学长 韩版系。学妹 提交于 2019-12-30 18:28:35
问题 As we already have HashMap , why would we use EnumMap ? 回答1: The Javadoc makes a pretty good argument: Enum maps are represented internally as arrays. This representation is extremely compact and efficient. Implementation note: All basic operations execute in constant time. They are likely (though not guaranteed) to be faster than their HashMap counterparts. 回答2: The main reason for EnumMap is that it is specifically optimised for enums. Further benefits are mentioned below. Taken help from

How to build a Map that replicates a Function in Java's Lambda API

我只是一个虾纸丫 提交于 2019-12-05 21:23:56
From a java.util.function.BiFunction that maps a pair of Enum s into a value, I want to build a EnumMap that reflects that mapping. For instance, let E1 and E2 be enum types and T any given type: BiFunction<E1,E2, T> theBiFunction = //...anything EnumMap<E1,EnumMap<E2,T>> theMap = buildTheMap( // <-- this is where the magic happens E1.values(), E2.values(), theBiFunction); Given any pair of values of type E1 and E2 E1 e1 = //any valid value... E2 e2 = //any valid value.... both values below should be equal: T valueFromTheMaps = theMap.get(e1).get(e2); T valueFromTheFunction = theBiFunction

EnumMap or HashMap if lookup key is a String

你离开我真会死。 提交于 2019-12-03 08:17:09
问题 I'm trying to weigh the pros and cons of using an EnumMap over a HashMap . Since, I will always be looking up using a String , it seems that a HashMap with a String key would be the right choice. However, an EnumMap seems like better design because it conveys my intent to restrict keys to a specific enumeration. Thoughts? Here is a make-believe example, showing how I will be using the Map : enum AnimalType { CAT, DOG } interface Animal {} class Cat implements Animal {} class Dog implements

Why an EnumSet or an EnumMap is likely to be more performant than their hashed counterparts?

浪尽此生 提交于 2019-12-01 03:41:57
The following is from the Implementation Note section of Java doc of EnumMap : Implementation note: All basic operations execute in constant time. They are likely (though not guaranteed) to be faster than their HashMap counterparts. I have seen a similar line in the java doc for EnumSet also . I want to know why is it more likely that EnumSets and EnumMaps will be more faster than their hashed counterparts ? EnumSet is backed by a bit array. Since the number of different items you can put in EnumSet is known in advance, we can simply reserve one bit for each enum value. You can imagine similar

Why an EnumSet or an EnumMap is likely to be more performant than their hashed counterparts?

左心房为你撑大大i 提交于 2019-11-30 23:22:40
问题 The following is from the Implementation Note section of Java doc of EnumMap : Implementation note: All basic operations execute in constant time. They are likely (though not guaranteed) to be faster than their HashMap counterparts. I have seen a similar line in the java doc for EnumSet also . I want to know why is it more likely that EnumSets and EnumMaps will be more faster than their hashed counterparts ? 回答1: EnumSet is backed by a bit array. Since the number of different items you can

Is there a corresponding immutable enumMap in guava?

天涯浪子 提交于 2019-11-30 06:02:19
I recently learnt about the benefits of EnumMap in Java and would like to replace the existing ImmutableMap<OccupancyType, BigDecimal> to EnumMap. However, I'd also like the immutable property offered by ImmutableMap. Is there a variant, ImmutableEnumMap available in guava ? In terms of storage which one (EnumMap vs ImmutableMap) performs better ? I couldn't find a comparison of the two. I'd appreciate if someone can point me to a link or give some insights on the efficiency of the two data structures ? Louis Wasserman Guava contributor here. Guava doesn't currently have an ImmutableEnumMap

Is there a corresponding immutable enumMap in guava?

醉酒当歌 提交于 2019-11-29 05:19:11
问题 I recently learnt about the benefits of EnumMap in Java and would like to replace the existing ImmutableMap<OccupancyType, BigDecimal> to EnumMap. However, I'd also like the immutable property offered by ImmutableMap. Is there a variant, ImmutableEnumMap available in guava ? In terms of storage which one (EnumMap vs ImmutableMap) performs better ? I couldn't find a comparison of the two. I'd appreciate if someone can point me to a link or give some insights on the efficiency of the two data