Create a HashMap to support duplicate keys in Java

Description

The following code shows how to create a HashMap to support duplicate keys.

Example


//w w  w.j av  a 2  s. co  m
/*
 * 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;
  }
}




















Home »
  Java Tutorial »
    Java Collection »




Java ArrayList
Java Collection
Java Comparable
Java Comparator
Java HashMap
Java HashSet
Java Iterator
Java LinkedHashMap
Java LinkedHashSet
Java LinkedList
Java List
Java ListIterator
Java Map
Queue
Java Set
Stack
Java TreeMap
TreeSet