Java基础学习笔记之七(1)--Collection接口&Collections集合工具类

时间:2023-02-24 23:11:38
***容器API
  Collection接口定义了存取单个对象的方法,其子接口
分别定义存储的方式和形式。
  1.List:存储的数据有顺序,可以重复。
  2.Set:存储的数据没有顺序,并且不可以重复  
  3.Map接口定义了用来存储"键值对" key-value的方法
  
***Collection
  A collection represents a group of objects, known as its elements. 
Some collections allow duplicate elements and others do not. 
Some are ordered and others unordered.
  容器类对象在调用remove,contains等方法时需要比较
对象是否相等,这会涉及到对象的equals方法和hashcode方法。
对于自己定义的类,需要重写equals和hashcode方法。
  备注:equals为true,hashCode的值也应该相等。
  通过hashCode可以找到数据在内存中存放的地址,hashCode
方法比equals方法块,hashCode适合做索引。

***Collections 集合工具类
常用方法
  1.void sort(List) 
  Sorts the specified list into ascending order, 
according to the natural ordering of its elements.
  对List内的元素排序(LinkedList排序比较快)
  
  2.void shuffle(List) 
  Randomly permutes the specified list using a default source of randomness
  对List容器内的对象进行随机排列
  
  3.void reverse(List)
  Reverses the order of the elements in the specified list
  翻转指定列表中元素的顺序
  
  4.void fill(List,Object)
  Replaces all of the elements of the specified list with the specified element.
  使用指定元素替换指定列表中的所有元素。
  
  5.void copy(List dest,List src)
  Copies all of the elements from one list into another. 
  将所有元素从一个列表复制到另一个列表。
将Src List容器内容拷贝到dest List 容器

  6.int binarySearch(List list,Object obj)
  Searches the specified list for the specified object using the binary search algorithm.
  使二分法来搜索指定列表,以获得指定对象。
  在指定的列表中查找指定的元素