Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   * Copyright (C) 2007 The Guava Authors
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
 import static;
 import java.util.Map;
 import  javax.annotation.Nullable;

A BiMap backed by an EnumMap instance for keys-to-values, and a HashMap instance for values-to-keys. Null keys are not permitted, but null values are. An EnumHashBiMap and its inverse are both serializable.

See the Guava User Guide article on BiMap.

Mike Bostock
2.0 (imported from Google Collections Library)
 @GwtCompatible(emulated = true)
 public final class EnumHashBiMap<K extends Enum<K>, V>
     extends AbstractBiMap<K, V> {
   private transient Class<K> keyType;

Returns a new, empty EnumHashBiMap using the specified key type.

keyType the key type
   public static <K extends Enum<K>, V> EnumHashBiMap<K, V>
       create(Class<K> keyType) {
     return new EnumHashBiMap<K, V>(keyType);

Constructs a new bimap with the same mappings as the specified map. If the specified map is an EnumHashBiMap or an EnumBiMap, the new bimap has the same key type as the input bimap. Otherwise, the specified map must contain at least one mapping, in order to determine the key type.

map the map whose mappings are to be placed in this map
IllegalArgumentException if map is not an EnumBiMap or an EnumHashBiMap instance and contains no mappings
   public static <K extends Enum<K>, V> EnumHashBiMap<K, V>
       create(Map<K, ? extends V> map) {
     EnumHashBiMap<K, V> bimap = create(EnumBiMap.inferKeyType(map));
     return bimap;
   private EnumHashBiMap(Class<K> keyType) {
         new EnumMap<K, V>(keyType)),
         Maps.<V, K>newHashMapWithExpectedSize(
     this. = keyType;
   // Overriding these 3 methods to show that values may be null (but not keys)
   K checkKey(K key) {
     return checkNotNull(key);
   @Override public V put(K key, @Nullable V value) {
     return super.put(keyvalue);
   @Override public V forcePut(K key, @Nullable V value) {
     return super.forcePut(keyvalue);

Returns the associated key type.
  public Class<K> keyType() {
    return ;


the key class, number of entries, first key, first value, second key, second value, and so on.
  private void writeObject(ObjectOutputStream streamthrows IOException {
  @SuppressWarnings("unchecked"// reading field populated by writeObject
  private void readObject(ObjectInputStream stream)
      throws IOExceptionClassNotFoundException {
     = (Class<K>) stream.readObject();
    setDelegates(WellBehavedMap.wrap(new EnumMap<K, V>()),
        new HashMap<V, K>(.getEnumConstants().length * 3 / 2));
  @GwtIncompatible("only needed in emulated source.")
  private static final long serialVersionUID = 0;
New to GrepCode? Check out our FAQ X