【JavaSE】5.集合Collection:List、Set;Map



title: JavaSE_5.集合Collection:List、Set;Map
date: 2020-11-09 20:01:08
tags: JavaSE

Generic<>

Single column collection List, Set; double column collection: Map

Collection interface:
list interface (ArrayList, LinkedList), set interface (HashSet, TreeSet, LInkedSet)
Map interface:
HahMap (implementation class), TreeMap (implementation class)

ArrayLIst method:

boolean add(E e); //把参数假如对象集合,返回值代表是否增加成功
boolean addAll(Collection<? extends E>  c);  //指定集合中的所有元素添加到此集合
void clear();  //清除集合中所有元素
boolean remove(Object o)  //移除指定元素
boolean removeAll(Collection<?> c);  //只删除c中有的元素,c是通缉令,删除c里面的坏人。
boolean retainAll(Collection<?> c);  //只保留c中有的元素,c是良民证,留下c里面的好人。
boolean contains(Object o);          //如果此集合包含指定的元素,则返回 true 。
boolean containsAll(Collection<?> c); //如果此集合包含了c,包含关系&&是个大集合,则返回true。
boolean isEmpty();  //集合里面没有元素 , 返回true
boolean equals(Object o);  //比对的方法
int size();          //返回此集合中的元素数量。
Object\[\] toArray();  //返回一个包含此集合中所有元素的数组。

List (the bottom layer is an array)

Features:

  1. There can be repeated elements;
  2. Ordered, indexed, index starts from 1

Commonly used methods:

//对List进行增删操作
boolean add(E e);   //将指定的元素追加到此列表的末尾(可选操作)。
void add(int index, E element);   //将指定的元素插入此列表中的指定位置(可选操作)。
boolean addAll(Collection<? extends E> c);  //按指定集合的迭代器(可选操作)返回的顺序将指定集合中的所有元素附加到此列表的末尾。
void clear();   //从此列表中删除所有元素(可选操作)。
boolean remove(Object o);   //从列表中删除指定元素的第一个出现(如果存在)(可选操作)。
boolean removeAll(Collection<?> c);   //从此列表中删除包含在指定集合中的所有元素(可选操作)。
//判断有什么:
boolean contains(Object o);             //如果此列表包含指定的元素,则返回 true 。
boolean containsAll(Collection<?> c);   //如果此列表包含指定 集合的所有元素,则返回true。
//获取元素
E get(int index);   //返回此列表中指定位置的元素。
int indexOf(Object o);   //返回此列表中指定元素的第一次出现的索引,如果此列表不包含元素,则返回-1。
boolean isEmpty();   //如果此列表不包含元素,则返回 true 。\[极少用到\]


//保留元素、设置元素
boolean retainAll(Collection<?> c);   //仅保留此列表中包含在指定集合中的元素(可选操作)。
E set(int index, E element);   //用指定的元素(可选操作)替换此列表中指定位置的元素。
int size();   //返回此列表中的元素数。
可以对Object类里面的equals进行重写
boolean equals(Object o);     // 将指定的对象与此列表进行比较以获得相等性。

Set (the bottom layer is a linked list)

int size();   //返回元素个数
add(Object obj);   //添加元素
remove(Object obj);   //删除元素
boolean contains(Object obj);  //判断是否包含元素
iterator();  //把set装到迭代器里面

List traversal

  • for loop, (using list.get(i);)
  • Iterator: install Set into iterator
Iterator it = list.iterator();
while(it.hasNext()){
    
    
   System.out.println(it.next());
}
  • Enhanced for

The use of iterators is relatively fast, the get method is too slow, it is not recommended, when you do not need to change the collection, you can use foreach

Features of ArrayList:

  1. For LinkedList, ArrayList query efficiency is high, ArrayList addition and deletion efficiency is low.
  2. The underlying implementation of ArrayList relies on arrays
    • For the add method, first see if the length of the array is enough. ensureCapacityInternal(size + 1);

HashCode (), equals () method rewrite

Collections tools

Guess you like

Origin blog.csdn.net/weixin_43801418/article/details/110632150