Added methods isEmpty(), putAll(), toArray() to interfaces List and Map

This commit is contained in:
Eric Scharff
2007-09-26 08:57:34 -06:00
parent 4ae4221701
commit b02b98609e
10 changed files with 115 additions and 1 deletions

View File

@ -14,6 +14,13 @@ public class ArrayList<T> implements List<T> {
this(0); this(0);
} }
public ArrayList(Collection<T> source) {
this(source.size());
for (T o : source) {
add(o);
}
}
private void grow() { private void grow() {
if (array == null || size >= array.length) { if (array == null || size >= array.length) {
resize(array == null ? MinimumCapacity : array.length * 2); resize(array == null ? MinimumCapacity : array.length * 2);
@ -118,6 +125,27 @@ public class ArrayList<T> implements List<T> {
return false; return false;
} }
public boolean isEmpty() {
return size() == 0;
}
public <S> S[] toArray(S[] a) {
Object[] retVal = null;
if (a.length >= size) {
retVal = a;
} else {
retVal = new Object[size];
}
for (int i = 0; i < size; ++i) {
retVal[i] = array[i];
}
if (a.length > size) {
a[size] = null;
}
return (S[])retVal;
}
public void clear() { public void clear() {
array = null; array = null;
size = 0; size = 0;

View File

@ -38,6 +38,14 @@ public class Arrays {
return array[index]; return array[index];
} }
public <S> S[] toArray(S[] a) {
return (S[])array;
}
public boolean isEmpty() {
return size() == 0;
}
public T remove(int index) { public T remove(int index) {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }

View File

@ -44,6 +44,10 @@ public class HashMap<K, V> implements Map<K, V> {
return r; return r;
} }
public boolean isEmpty() {
return size() == 0;
}
public int size() { public int size() {
return size; return size;
} }
@ -179,6 +183,12 @@ public class HashMap<K, V> implements Map<K, V> {
return (c == null ? null : c.getValue()); return (c == null ? null : c.getValue());
} }
public void putAll(Map<? extends K,? extends V> elts) {
for (Map.Entry<? extends K, ? extends V> entry : elts.entrySet()) {
put(entry.getKey(), entry.getValue());
}
}
public V remove(K key) { public V remove(K key) {
Cell<K, V> c = removeCell(key); Cell<K, V> c = removeCell(key);
return (c == null ? null : c.getValue()); return (c == null ? null : c.getValue());

View File

@ -15,6 +15,10 @@ public class Hashtable<K, V> implements Map<K, V> {
return map.toString(); return map.toString();
} }
public synchronized boolean isEmpty() {
return map.isEmpty();
}
public synchronized int size() { public synchronized int size() {
return map.size(); return map.size();
} }
@ -35,6 +39,10 @@ public class Hashtable<K, V> implements Map<K, V> {
return map.put(key, value); return map.put(key, value);
} }
public synchronized void putAll(Map<? extends K,? extends V> elts) {
map.putAll(elts);
}
public synchronized V remove(K key) { public synchronized V remove(K key) {
return map.remove(key); return map.remove(key);
} }

View File

@ -11,6 +11,10 @@ public class IdentityHashMap<K, V> implements Map<K, V> {
this(0); this(0);
} }
public boolean isEmpty() {
return map.isEmpty();
}
public int size() { public int size() {
return map.size(); return map.size();
} }
@ -31,6 +35,10 @@ public class IdentityHashMap<K, V> implements Map<K, V> {
return map.put(key, value); return map.put(key, value);
} }
public void putAll(Map<? extends K,? extends V> elts) {
map.putAll(elts);
}
public V remove(K key) { public V remove(K key) {
return map.remove(key); return map.remove(key);
} }

View File

@ -73,6 +73,23 @@ public class LinkedList<T> implements List<T> {
} }
} }
public <S> S[] toArray(S[] a) {
Object[] retVal = null;
if (a.length >= size) {
retVal = a;
} else {
retVal = new Object[size];
}
int i=0;
for (Object o : this) {
retVal[i++] = o;
}
if (a.length > size) {
a[size] = null;
}
return (S[])retVal;
}
public int size() { public int size() {
return size; return size;
} }
@ -124,6 +141,10 @@ public class LinkedList<T> implements List<T> {
return c.value; return c.value;
} }
public boolean isEmpty() {
return size() == 0;
}
public T removeFirst() { public T removeFirst() {
if (front != null) { if (front != null) {
T v = front.value; T v = front.value;

View File

@ -4,4 +4,8 @@ public interface List<T> extends Collection<T> {
public T get(int index); public T get(int index);
public T remove(int index); public T remove(int index);
public boolean isEmpty();
public <S> S[] toArray(S[] a);
} }

View File

@ -1,6 +1,8 @@
package java.util; package java.util;
public interface Map<K, V> { public interface Map<K, V> {
public boolean isEmpty();
public int size(); public int size();
public boolean containsKey(K key); public boolean containsKey(K key);
@ -11,6 +13,8 @@ public interface Map<K, V> {
public V put(K key, V value); public V put(K key, V value);
public void putAll(Map<? extends K,? extends V> elts);
public V remove(K key); public V remove(K key);
public void clear(); public void clear();

View File

@ -11,6 +11,13 @@ public class Vector<T> implements List<T> {
this(0); this(0);
} }
public Vector(List<T> list) {
this(list.size());
for (T o : list) {
add(o);
}
}
public synchronized int size() { public synchronized int size() {
return list.size(); return list.size();
} }
@ -39,6 +46,14 @@ public class Vector<T> implements List<T> {
return list.remove(index); return list.remove(index);
} }
public synchronized boolean isEmpty() {
return list.isEmpty();
}
public synchronized <S> S[] toArray(S[] a) {
return list.toArray(a);
}
public T removeElementAt(int index) { public T removeElementAt(int index) {
return remove(index); return remove(index);
} }

View File

@ -26,6 +26,10 @@ public class WeakHashMap<K, V> implements Map<K, V> {
} }
} }
public boolean isEmpty() {
return map.isEmpty();
}
public int size() { public int size() {
return map.size(); return map.size();
} }
@ -50,6 +54,10 @@ public class WeakHashMap<K, V> implements Map<K, V> {
return map.put(key, value); return map.put(key, value);
} }
public void putAll(Map<? extends K,? extends V> elts) {
map.putAll(elts);
}
public V remove(K key) { public V remove(K key) {
poll(); poll();
return map.remove(key); return map.remove(key);