集合的结构框架
List集合
方法:这些方法时来自jdk1.8进行解读的,有一些其中有两个方法不太知道怎么使用。
int size();
boolean isEmpty();
boolean contains(Object o);
Iterator<E> iterator();
Object[] toArray();
boolean remove(Object o);
boolean containAll(Collection c);
boolean addAll(Collection c);
boolean addAll(int index,Collection c) 从开始到index
boolean removeAll(Collection c)
boolean retainAll(Collection c) 在两个集合不相同的情况下 查询两个集合的交集
default void replaceAll(UnaryOperator<E> operator) 这个方法时1.8后的,不太知道怎么使用
default void sort(Comparator c);
void clear();
boolean equals(Object o);
int hashCode();
int indexOf(Object o);
int lastIndexOf(Object o);
ListIterator listIterator();可以进行元素操作的迭代器
ListIterator listIterator(int index);从index进行迭代
subList(int fromIndex,int toIndex);从fromIndex截取到toIndex
default Spliterator<E> spliterator();这个方法不知道怎么使用
之前一直都不太知道jdk1.8的特性,这次看到有接口中存在方法体和一些静态方法。写方法体的方法必须要用default来修饰。
1.ArrayList集合
1.默认大小为——10
2.使用static final Object[] 数据来装数据
3.使用一个 size 来记录集合的大小
特点:有序可重复
以数组的形式进行存储,查询快,增删慢
1、初始化
public ArrayList(int initialCapacity)
public ArrayList()
public ArrayList(Collection c)
2、ArrayList中一些常用的方法
boolean add(E e)
void add(int index, E e);
public E set(int index, E e);
public E get(int index);
public E remove(int index);
public boolean remove(Object o);
public void clean();
public boolean addAll(Collection c);
public boolean addAll(int index,Collection c)
public boolean removeAll(Collection c);
public ListIterator listIterator(int index);
public ListIterator listIterator();
public Iterator iterator();
Set集合
Set集合感觉和list集合中的方法差不多,
特点:无序、不可重复、允许有null
主要方法:
HashSet集合
它使用HashMap 中的key来存储,保证了它的唯一性。他是按照hash算法排序
没有get()方法 没有get()方法 没有get()方法!!!
方法:
boolean add(object o);
void clear();
boolean contains(object o);
Iterator iterator();
boolean remove(object);
int size();
迭代器遍历
Set set = new HashSet();
Iterator it = set.iterator();
while(it.hasNext()){
system.out.println(it.next());
}
for循环遍历
Set set = new HashSet();
for(Object obj : set){
system.out.println(obj);
}