Java: 集合类

Set

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public interface Set<E> extends Collection<E>
{
//基本操作
int size();
boolean isEmpty();
boolean contains(Objects element);
boolean add(E element);
boolean remove(Objects element);
Iterator<E> iterator();
boolean containsAll(Collection <?> c);
boolean addAll(Collection<?extends E> c);//将集合c的元素添加到本集合
boolean removeAll(Collection<?> c);
boolean retainAll(Collection<?> c);//只保留属于c的元素
void clear();
Objects[] toArray();
<T>T[] toArray(T[] a);
}

JDK 提供 Set 接口 3 个实用类

  • HashSet,采用 Hash, 无固定顺序

  • TreeSet,实现 SortedSet 接口,有序

  • LinkedHashSet,有固定顺序


List

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public interface List<E> extends Collection<E>{
E get(int index);
E set(int index,E element);
boolean add(E element);
void add(int index,E element);
E remove(int index);
boolean addAll(int index,Collection<? extends E> c);
//查找
int indexOf(Object o);
int lastIndexOf(Object o);
ListIterator<E> listIterator();
ListIterator<E> listIterator(int index);
Lis<E> subList(int from,int to);
}

JDK 提供 Set 接口 3 个实用类

  • ArrayList, 效率最高

  • LinkedList, 还提供在结尾和开头 get,remove,insert

  • Vector


Queue

1
2
3
4
5
6
7
8
public interface Queue<E> extends Collection<E> {
E element();
    boolean add(E e);
boolean offer(E e);
E remove();
E poll();
E peek();
}

Map

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public interface Map<K, V> {
int size();
V put(K key,V value);
V get(Object key);
V remove(Object key);
boolean containsKey(Object key);
boolean containsValue(Object value);
int size();
boolean isEmpty();
void putAll(Map<?extends K,?extends V>m);
void clear();
public Set(K) keySet();
public Collection<V> values();
public Set<Map.Entry<k,v>> entrySet();
public interface Entry{
K getKey();
V getValue();
V setValue(V value);
}
}