Source Code : Map from a given key to a list of values

Java Is Open Source Programming Language You Can Download From Java and Java Libraries From http://www.oracle.com. Click Here to download
We provide this code related to title for you to solve your developing problem easily. Libraries which is import in this program you can download from http://www.oracle.com. Click Here or search from google with Libraries Name you get jar file related it

Map from a given key to a list of values

      
//package org.streets.commons.collections;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Map from a given key to a list of values
 * 
 * @author dzb
 */
public class MapList<K, V> extends HashMap<K, List<V>> {

  private static final long serialVersionUID = 3462998132471897564L;

  public void add(K key, V value) {
    List<V> values = get(key);
    if (values == null) {
      values = new ArrayList<V>();
      put(key, values);
    }
    values.add(value);
  }

  public void remove(K key, V value) {
    List<V> values = get(key);
    if (values != null) {
      values.remove(value);
    }
  }
  
  public List<V> list(K key) {
    return super.get(key);
  }
  
  public V first(K key) {
    List<V> list = list(key);
    if (list != null && list.size() > 0) {
      return list.get(0);
    }
    return null;
  }
}

   
    
    
    
    
    
  

Thank with us