Extended Version of java.util.HashMap that provides an extended get method accpeting a default value. : HashMap « Collections Data Structure « Java






Extended Version of java.util.HashMap that provides an extended get method accpeting a default value.

     
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * 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 java.util.Map;

/**
 * Extended Version of {@link java.util.HashMap} that provides an extended
 * get method accpeting a default value. The default value is returned if
 * the map does not contain a value for the provided key.
 *
 * @version $Id: HashMap.java 587751 2007-10-24 02:41:36Z vgritsenko $
 */
public class HashMap extends java.util.HashMap {

    public HashMap () {
        super();
    }

    public HashMap ( int initialCapacity ) {
        super(initialCapacity);
    }

    public HashMap ( int initialCapacity, float loadFactor ) {
        super(initialCapacity, loadFactor);
    }

    public HashMap ( Map t) {
        super(t);
    }

    /**
     * Get method extended by default object to be returned when key
     * is not found.
     *
     * @param key key to look up
     * @param _default default value to return if key is not found
     * @return value that is associated with key
     */
    public Object get( Object key, Object _default ) {
        if (this.containsKey(key)) {
            return this.get(key);
        }
        return _default;
    }

}

   
    
    
    
    
  








Related examples in the same category

1.Iterate through the values of Java HashMap example
2.Get Synchronized Map from Java HashMap example
3.Check if a particular key exists in Java HashMap example
4.Check if a particular value exists in Java HashMap example
5.Get Set view of Keys from Java HashMap example
6.Get Size of Java HashMap
7.Remove all values from Java HashMap example
8.Remove value from Java HashMap
9.Create Java Hashtable from HashMap
10.Sort an HashMap based on the keys
11.For keys of a map
12.For values of a map
13.For both the keys and values of a map
14.Storing Primitive Types in a Collection
15.Creating a Copy of a Collection: the objects are not cloned.
16.Use Iterator to loop through the map key set
17.Generic hashmap with String as key and Integer as valueGeneric hashmap with String as key and Integer as value
18.Get key set and value set from map and use Iterator to loop through them
19.Clones a map
20.Hash map for counting references to Object keys.
21.Compact HashMap
22.This class wraps a HashMap and provides methods by which key objects can be associated with "counting" values.
23.This extension of HashMap support duplicate keys
24.Concurrent Hopscotch HashMap