Java tutorial
/* * Copyright 2007-2012 Jiemamy Project and the Others. * Created on Apr 8, 2009 * * This file is part of Jiemamy. * * Licensed 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. */ package org.jiemamy.utils.collection; import java.util.Collection; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.ListIterator; import java.util.Map; import com.google.common.collect.Lists; /** * ????? * * <p>? {@link Map} ????? {@link Map#values()} ?? * {@link List} ???</p> * * @param <K> ? * @param <V> ? * @version $Id$ * @author j5ik2o */ @SuppressWarnings("serial") public class ArrayMap<K, V> extends HashMap<K, V> { private List<V> valueList = Lists.newArrayList(); /** * ?? */ public ArrayMap() { } /** * ?? * * @param capacity */ public ArrayMap(int capacity) { super(capacity); valueList = Lists.newArrayListWithCapacity(capacity); } /** * @see List#contains(Object) * * @param o List#contains(Object)? * @return List#contains(Object)? */ public boolean contains(Object o) { return valueList.contains(o); } /** * @see List#containsAll(Collection) * * @param c List#containsAll(Collection)? * @return List#containsAll(Collection)? */ public boolean containsAll(Collection<?> c) { return valueList.containsAll(c); } /** * @see List#get(int) * * @param index List#get(int)? * @return List#get(int)? */ public V get(int index) { return valueList.get(index); } /** * @see List#indexOf(Object) * * @param o {@link List#indexOf(Object)}? * @return{@link List#indexOf(Object)}? */ public int indexOf(Object o) { return valueList.indexOf(o); } /** * @see List#iterator() * * @return {@link List#iterator()}? */ public Iterator<V> iterator() { return valueList.iterator(); } /** * @see List#lastIndexOf(Object) * * @param o {@link List#lastIndexOf(Object)}? * @return {@link List#lastIndexOf(Object)}? */ public int lastIndexOf(Object o) { return valueList.lastIndexOf(o); } /** * @see List#listIterator() * * @return {@link List#listIterator()}? */ public ListIterator<V> listIterator() { return valueList.listIterator(); } /** * @see List#listIterator(int) * * @param index {@link List#listIterator(int)}? * @return {@link List#listIterator(int)}? */ public ListIterator<V> listIterator(int index) { return valueList.listIterator(index); } @Override public V put(K key, V value) { V result = super.put(key, value); valueList.add(value); return result; } @Override public V remove(Object key) { V result = super.remove(key); if (result != null) { valueList.remove(result); } return result; } /** * @see List#subList(int, int) * * @param fromIndex {@link List#subList(int, int)}? * @param toIndex {@link List#subList(int, int)}? * @return {@link List#subList(int, int)}? */ public List<V> subList(int fromIndex, int toIndex) { return valueList.subList(fromIndex, toIndex); } /** * @see List#toArray() * * @param <T> {@link List#toArray()}?? * @return {@link List#toArray()}? */ @SuppressWarnings("unchecked") public <T> T[] toArray() { return (T[]) valueList.toArray(); } /** * @see List#toArray(Object[]) * * @param <T> {@link List#toArray(Object[])}?? * @param values {@link List#toArray(Object[])}? * @return {@link List#toArray(Object[])}? */ public <T> T[] toArray(T[] values) { return valueList.toArray(values); } }