Java类集

来源:互联网 发布:vivo手机4g网络不稳定 编辑:程序博客网 时间:2024/04/29 03:09




Java类集


类集框架主要接口:Collection;List;Set;Map;Iterator;ListIterator;Enumeration;SortedSet;SortedMap;Queue;Map.entry


Collection接口的定义:
public interface Collection<E> extends Iterable<E>
Collection接口的方法:
public boolean add(E o)//向集合中插入对象


public boolean addAll(Collection<? extends E> c)//将一个集合的内容插入进来


public void clear() //清楚此集合的所有元素


public boolean contains(Object o)//判断某一对象是不是在集合中


public boolean containsAll(Collection<?> c)//判断一组对象是不是在集合中存在


public boolean equals(Object o)//对象比较


public int hashCode() //哈希码


public boolean isEmpty()//是否为空


public Iterator<E> iterator()//为Iterator接口实例化


public boolean remove(Object o)//删除指定对象


public boolean removeAll(Collection<?> c)//删除一组对象


public boolean retainAll(Collection<?> c)//保留指定内容


public int size() //求出集合大小


public Object[] toArray()//将一个集合变成数组对象


public <T> T[] toArray()//指定好返回的对象数组的类型






Collection接口的子接口:
List:
List接口的定义
public interface List<E> extends Collection<E>
List接口扩展的方法:
public void add(int index,E element)//在指定位置增加元素


public boolean addAll(int index,Collection<? extends E> c)//在指定位置增加一组元素


E get(int index) //返回指定位置的元素


public int indexOf(Object o)//返回指定元素的位置


public int lastIndexOf(Object o)//从后往前查找指定元素的位置


public ListIterator<E> listIterator()//为ListIterator接口实例化


public E remove(int index)//按照指定的位置查找元素


public List<E> subList<int fromIndex,int toIndex)//取出集合中的子集合


public E set(int index,E element)//替换指定位置的元素






List接口的实现类ArrayList:


ArrayList是List的实现类。ArrayList类的定义:
public class ArrayList<E> extends AbstractList<E> implements List<E>,RandomAccess,Cloneable,Serializable
ArrayList类的父类AbstractList 类的定义:
public class AbstractList<E> extends AbstractCollection<E> implements List<E>






List接口的实现类Vector:
Vector类的定义:
public class Vector<E> extends AbstractList<E> implements List<E>,RandomAccess,Cloneable,Serializable
ArrayList类和Vector;类都是继承自AbstractList类






List接口的实现类LinkedList:
LinkedList类的定义:
public class LinkedList<E> extends AbstractSequentialList<E> implements List<E>,Queue<E>,Cloneable,Serializable


LinkedList中定义的方法:
public void addFirst(E o)//在链表开头增加元素


public void addLast(E o)//在链表结尾增加元素


public boolean offer(E o)//将指定元素增加到链表结尾


public E removeFirst()//删除链表的第一个元素


public E removeLast() //删除链表的最后一个元素




Queue接口定义的方法:
public E element() //找到链表的头


public boolean offer(E o)//将指定元素增加到链表的结尾


public E peek() //找到但是不删除链表的头


public E poll() //找到并且删除链表的头


public E remove() //检索并移除表头






Set接口:
Set接口定义:
public interface Set<E> extends Collection<E>
Set中不能有重复元素,不能双向输出。
HashSet是Set接口的一个实现类,其特点是里面不能存放重复元素,并且采用散列的存储方式,没有顺序
TreeSet:
TreeSet类可以对输入的数据进行排序,TreeSet类的定义:
public class TreeSet<E> extends AbstractSet<E> implements SortedSet<E>,Cloneable,Serializable
TreeSet中的每个对象所在的类都必须实现Comparable接口。








SortedSet接口:
SortedSet接口的定义:
public interface SortedSet<E> extends Set<E>
SortedSet接口中定义的方法:
public Comparator<? super E> comparator()//返回与排序有关的比较器


public E first() //返回集合中的第一个元素


public SortedSet<E> headSet(E toElement)//返回从开始到指定元素的集合


public E last() //返回最后一个元素


public SortedSet<E> subSet(E fromElement,E toElement)//返回指定对象间的元素


public SortedSet<E> tailSet(E fromElement)//从指定元素到最后








Iterator接口:
Iterator接口的定义:
public interface Iterator<E>
Iterator接口中常用的方法:
public boolean hasNext()//判断是否有下一个值


public E next() //读取当前元素


public void remove() //移除当前元素






Iterator接口的功能主要是从前向后单向输出,此时如果想实现双向输出,则可以使用ListIterator
ListIterator接口的定义:
public interface ListIterator<E> extends Iterator<E>





0 0
原创粉丝点击