This extension of HashMap support duplicate keys : HashMap « Collections Data Structure « Java






This extension of HashMap support duplicate keys

    
/*
 * Copyright (c) 2006, Chuck Mortimore - xmldap.org
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the names xmldap, xmldap.org, xmldap.com nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */


//package org.xmldap.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * This extension of HashMap support duplicate keys
 */
public class Bag extends LinkedHashMap {
  public List getValues(Object key) {
    if (super.containsKey(key)) {
      return (List) super.get(key);
    } else {
      return new ArrayList();
    }
  }

  public Object get(Object key) {
    ArrayList values = (ArrayList) super.get(key);
    if (values != null && !values.isEmpty()) {
      return values.get(0);
    } else {
      return null;
    }
  }

  public boolean containsValue(Object value) {
    return values().contains(value);
  }

  public int size() {
    int size = 0;
    Iterator keyIterator = super.keySet().iterator();

    while (keyIterator.hasNext()) {
      ArrayList values = (ArrayList) super.get(keyIterator.next());
      size = size + values.size();
    }

    return size;
  }

  public Object put(Object key, Object value) {
    ArrayList values = new ArrayList();

    if (super.containsKey(key)) {
      values = (ArrayList) super.get(key);
      values.add(value);

    } else {
      values.add(value);
    }

    super.put(key, values);

    return null;
  }

  public void remove(Object key, Object value) {
    List values = getValues(key);
    if (values != null) {
      values.remove(value);
      if (values.isEmpty()) {
        remove(key);
      }
    }
  }

  public Collection values() {
    List values = new ArrayList();
    Iterator keyIterator = super.keySet().iterator();

    while (keyIterator.hasNext()) {
      List keyValues = (List) super.get(keyIterator.next());
      values.addAll(keyValues);
    }

    return values;
  }
}

   
    
    
    
  








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.Extended Version of java.util.HashMap that provides an extended get method accpeting a default value.
22.Compact HashMap
23.This class wraps a HashMap and provides methods by which key objects can be associated with "counting" values.
24.Concurrent Hopscotch HashMap