Collection Map List SortedMai p Hash Set HashMapl Sortedset i Array list RemaP linkeDlist TreeSet TreeMap一使用平衡二叉树算法实现Map接口
Default Implementation • 集合框架中提供了一些常用的集合接口的实 现类: HashSet — 使用散列表算法实现Set接口 TreeSet — 使用平衡二叉树算法实现SortedSet接口 ArrayList — 使用数组存放对象来实现List接口 LinkedList — 使用双向链表来实现List接口 HashMap — 使用散列表算法实现Map接口 TreeMap — 使用平衡二叉树算法实现Map接口 Collection Set List Map SortedSet SortedMap ArrayList LinkedList TreeSet HashSet TreeMap HashMap
java util, Collection Interface 除映射类型的集合外,接口java.uti. Collection是 所有集合类型的超类型,声明了与集合操作相关 的通用方法 public int sized public boolean isEmptyo public boolean contains(object elem) public Objectl toArrayo public Objectl toArray(objectl dest)
java.util.Collection Interface • 除映射类型的集合外,接口java.util.Collection是 所有集合类型的超类型,声明了与集合操作相关 的通用方法 public int size() public boolean isEmpty() public boolean contains(Object elem) public Object[] toArray() public Object[] toArray(Object[] dest)
java util, Collection Interface public add(object elem) public remove(object elem) public boolean containsAll(Collection coll public boolean addAll( Collection coll) public boolean removeAll(Collection coll) public boolean retainAll(Collection col) public void clear public Iterator iterator
java.util.Collection Interface public add(Object elem) public remove(Object elem) public boolean containsAll(Collection coll) public boolean addAll(Collection coll) public boolean removeAll(Collection coll) public boolean retainAll(Collection coll) public void clear() public Iterator iterator()
public void removeLongStrings( Collection coll, int maxLen)( Iterator it=coll. iterator while(it. hasNextoi String str=(String)it. nexto; if(strength(>maxLen) coll.remove(str;错误的删除操作! public void remove LongStrings( Collection coll, int maxLen) Iterator it=coll. iterator 0: while(it. hasNextO) String str=(String)it. nexto if(strlength(>maxLen) it remove;正确的删除操作!
java.util.Iterator Interface • 该接口声明了用于遍历集合的方法: public boolean hasNext() public Object next() public void remove() • 通过调用Collection接口及其子接口的实现类的对 象的iterator()方法可以返回一个该接口的实现类的 对象,使用该对象来遍历访问集合中每个对象 • 在使用Iterator对象遍历集合时,如果要删除集合 中的某个对象,必须使用该接口中remove()方法! public void removeLongStrings(Collection coll, int maxLen){ Iterator it=coll.iterator(); while(it.hasNext()){ String str=(String)it.next(); if(str.length()>maxLen) coll.remove(str); } } 错误的删除操作! public void removeLongStrings(Collection coll, int maxLen){ Iterator it=coll.iterator(); while(it.hasNext()){ String str=(String)it.next(); if(str.length()>maxLen) it.remove(); } } 正确的删除操作!