List接口、Set接口、Map接口的方法

一、Collection接口中的方法介绍

  • int size();返回此Collection中的元素数
  • boolean isEmpty(); 判断是否为空
  • boolean containsAll(Collection c);判断形参c所指向的集合中的所有元素是不是已经全部包含在了当前集合中,是true,不是flase
  • Iterator iterator();返回能遍历当前集合所有元素的迭代器
  • Object[] toArray();容器不是数组,不能通过下标的方式访问容器中的元素,返回一个包含此Collection中所有元素的数组。
  • bollean add(Object e);  把e添加到当前集合中
  • boolean remove(Object o);   boolean addAll(Collection c)
  • boolean removeAll(Collection c); 
  • void clear();
  • boolean equals(Object o);
  • int hashCode();

二、List接口ArrayList LinkedList

  • Object get(int index);
  • Object set(oint index, Object element);
  • void add(int index, Object element);
  • Object remove(int index);
  • int indexOf( Object o);
  • int lastIndexOf(Object o);

三、Set接口

  • 因为Set和List都是继承自Collection接口,所以Set和List中很多方法都是一样的。
  • List接口中有add,set,indexOf方法,但Set中只有add方法,没有set,indexOf方法,因为Set是无序不可重复的,不存在某元素具体位置这个概念

四、Map接口

  • Object put(Object key,Object value);
  • Object get(Object key);  
  • boolean isEmpty();
  • void clear();
  • int size();
  • boolean containsKey(Object key);
  • boolean containsValue(Object value);

注意一个Map的一个Map.Entry接口,其中含有getKey(),getValue方法

(1) Object getKey(): 返回条目的关键字

       (2) Object getValue(): 返回条目的值

       (3) Object setValue(Object value): 将相关映像中的值改为value,并且返回旧值

此处再说两种图的遍历方法: 

Map map = new HashMap();
           Irerator iterator = map.entrySet().iterator();
           while(iterator.hasNext()) {
                   Map.Entry entry = iterator.next();
                   Object key = entry.getKey();
} 
Map map = new HashMap(); 
           Set  keySet= map.keySet();
           Irerator iterator = keySet.iterator;
           while(iterator.hasNext()) {
                   Object key = iterator.next();
                   Object value = map.get(key);
}
 Map map = new HashMap();
      Collection c = map.values();
      Iterator iterator = c.iterator();
      while(iterator.hasNext()) {
             Object value = iterator.next(); 
     }

五、Collections类的方法

      Collection接口的实现类,比如ArrayList,LinkedList本身并没有提供排序、倒置和查找的方法,这些方法由Collections类来实现,该类有很多 static public 方法,可以直接对Collections接口的实现类进行操作。

  1. void sort(List); 对List容器内元素进行排序
  2. void shuffle(List);  对List容器内对象进行随机排序
  3. void reverse(List);  对List容器内对象进行逆序排序
  4. rotate(List list,int m)方法的使用(含义:集合中的元素向后移m个位置,在后面被遮盖的元素循环到前面来)
  5. void fill(List, Object);用一个特定的对象重写整个List容器
  6. nCopies(int n,Object o)方法的使用(含义:返回大小为n的List,List不可改变,其中的所有引用都指向o)
  7. enumeration(Collection)方法的使用(含义:为参数生成一个旧式的Enumeration)
  8. void copy(List dest, List src); 讲src List容器的内容拷贝到dest List容器中
  9. swap(List list,int i,int j)方法的使用(含义:交换集合中指定元素索引的位置)
  10. int binarySearch(List ,Object);  对顺序的List容器,采用折半查找的方法查找特定对象
  11. max(Collection),max(Collection,Comparator)方法的使用(前者采用Collection内含自然比较法,后者采用Comparator进行比较)
  12. min(Collection),min(Collection,Comparator)方法的使用(前者采用Collection内含自然比较法,后者采用Comparator进行比较)
  13.  indexOfSubList(List list,List subList)方法的使用(含义:查找subList在list中首次出现位置的索引)。
  14. lastIndexOfSubList(List source,List target)方法的使用与上例方法的使用相同,在此就不做介绍了
  15. replaceAll(List list,Object old,Object new)方法的使用(含义:替换批定元素为某元素,若要替换的值存在刚返回true,反之返回false)
  16. list(Enumeration e)方法的使用(含义:返回使用Enumeration生成的ArrayList,用来转换遗留的老代码)
原文地址:https://www.cnblogs.com/xiaoxiaohui2015/p/5836387.html