collection集合学习笔记

collection  


list     linklist  arraylist




set     hashset  treeset




map   hashmap   propreties


      treemap   hashtable 




collection  是list 和set的父类 本省是个借口  


集合的元素必须是对象  不允许是基本数据类型




collection支持添加和删除的基本操作  如果元素存在 删除的是这个元素的实例




collection实现的基础是数组 有转换为object数组的方法




collection 有的操作集合的基本方法


containsAll 判断是否存在集合  取交际


contains 判断是否存在


removeall(collection)  删除子元素
retainAll(collection)  取交集






collection只是一个接口 


我们在实现他的时候 需要用到他得实现类










interator对collection的作用
应为collection 是list和set的父接口 所以collection
的参数是obj类型  里面可以是list 也可以是set   
在不确定类型的情况下interator 在实现上提供了便利
所以collection也没有get方法


interator本省就是对象


java 的interator可用来要求容器返回一个 interator对象
     提供next 得到collection的第一个元素 不光是collection 其他集合也一样
     提供hasnest判断是否有下一个元素
     提供对当前元素的remove操作
     
      而romove方法在每次调用的时候只能对当前元素的调用 并且只能有一次








list可以根据当前动态 改变list容器的大小  collection没有直接的实现类
list支持指定位置插入元素 add(index,element)
在指定位置加入集合   add(index,collection)
indexof(element)对象的位置
lastindexif(element)最后出现的位置
remove(index)移除指定位置的指定操作
set(index ,object) 指定位置修改指定元素   并且返回老的元素




listInterator
里面有 hasPrevious   hasNext两种遍历方式  


对于 ListInterator  list=myList.listIterator();




   it.add("sss");
 
   it1.next();  不会有东西


   it2.previous()会有作用


ArrayList 的方法


ArrayList() 无参的构造函数
ArrayList(Collection c) 有参的构造函数
ArrayList(int initialCapacity) 扩大容量


Arraylist(collection)  构造函数是用的ArrayList(int inintCapacity)


inintCapacity默认是10






图解


ArrayList==》构造方法有三个  即new ArrayList


 1.arraylist();


 2.arrayList(collection ele)


 3.arrayList(int inintCapaSize)           




 1 2实现的时候调用3


  1 2里面因该是
  
  private int inintCapaSize==10 
  
  arrayList(int inintCapaSize);   
   
 




  


3的内部结构是
   private object ElementData[] ele 数组
   private int  size;  


   public void(){
   this.ele=new object[size];
}






需要注意的是


2里面的collection参数 先是用Object[]  toarray (Object[] a)将集合放到新的集合里面
然后将size设置为 当前collection的数组大小的1.1倍




然后contains方法其实是用的 indexof方法  indexof方法 其实是比较里面的数组 用的equals() 也一定重写了object的equals方法 像String 也是




map  
不是继承了collection
而且键是不可重复的  键和值可以为空




三种重要因素


构造函数  
1.判断inintcapaCity  就是hashmap的大小
2.判断是否合法


3.确定长度的时候其实是2的n次方的  最接近inintCapaCity的但是比ininCapaCity大的值


4.接下来就是threshold重构因子  用来判断是否需要重构


最后用得到的capacity来构建hash表table

猜你喜欢

转载自blog.csdn.net/qq_34936628/article/details/74735182