集合方法
每个元素只能出现一次
典型应用:客户统计、词汇量统计
void add(E e);
void remove(E e);
boolean contains(E e);
int getSize();
boolean isEmpty();
}
BST、LinkedList都属于动态结构
基于链表的集合实现
public class LinkedListSet<E> implement Set<E>{
private LinkedList<E> list;
public LinkedListSet() {
list=new LinkedList<>();
}
public int getSize() {
return list.getSize();
}
public boolean isEmpty() {
return list.isEmpty();
}
public boolean contains(E e) {
return list.contains(e);
}
public void add(E e) {
if(!list.contains(e))
list.addFirst(e);
}
public void remove(E e) {
list.removeElement(e);
}
基于BST的集合实现
public interface BSTSet<E extends Comparable<E>> implements Set<E> {
private BST<E> bst;
public BSTSet() {
bst=new BST<>();
}
public int getSize() {
return bst.size();
}
public boolean isEmpty() {
return bst.isEmpty();
}
public void add(E e) {
bst.add(e);
}
public boolean contains(E e) {
return bst.contains(e);
}