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

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JDK 6
25.JNDI LDAP
26.JPA
27.JSP
28.JSTL
29.Language Basics
30.Network Protocol
31.PDF RTF
32.Reflection
33.Regular Expressions
34.Scripting
35.Security
36.Servlets
37.Spring
38.Swing Components
39.Swing JFC
40.SWT JFace Eclipse
41.Threads
42.Tiny Application
43.Velocity
44.Web Services SOA
45.XML
Java » Collections Data Structure » HashMapScreenshots 
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 (Listsuper.get(key);
    else {
      return new ArrayList();
    }
  }

  public Object get(Object key) {
    ArrayList values = (ArrayListsuper.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 = (ArrayListsuper.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 = (ArrayListsuper.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 = (Listsuper.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
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.