(Really simple and clear) Common methods of java collections

//This article has no meaning for students who have a deeper understanding of collections. The
general collection framework system is as shown in the figure:
Insert picture description here
Relevant notes:
E-Element (used in collections because the collections store elements)
T-Type (Java class)
K-Key (key)
V-Value (value)
N-Number (number type)

* Collection interface:

public boolean add(E e);  //添加元素到集合
public boolean addAll(Collection<? extends E> c); //存放一个集合
public boolean contains(Object o);  //查找集合中的元素
public boolean isEmpty();  //判断一个集合是否为空
public boolean remove(Object 0);//删除一个集合中的元素
public int size();//返回集合的长度
public void clear();//清空所有元素
public boolean equals(Object o);//判断是否相等


* List interface (including methods of Collection interface):

 public E get(int index); //根据索引取得元素
 public E set(int index,E element);//替换元素,index为要替换元素下标 element为要替换元素
 public ListIterator<E> listIterator() List //List自己的迭代器

* Queue interface (including methods of Collection interface)

boolean offer(E e);//区别于add ,它的返回值是boolean类型,如果添加未成功add是报错,它是返回false
//E代表的是元素的类型
E poll();//返回首个元素并从队列中弹出,如果队列是空的,就返回null
E peek();//返回首个元素,不会移除首个元素,如果队列是空的就返回null
E element();//返回首个元素,不会移除首个元素,如果队列是空的就抛出异常NoSuchElementException

* Set interface (including methods of Collection interface):
Set has exactly the same interface as Collection, but the behavior is different, Set does not store duplicate elements (specifically described in its implementation class HashSet)

* Map interface
The collection under the Map interface is different from the Collection. The collection in the Collection, the elements exist in isolation (can be regarded as singletons), and the elements stored in the collection are stored in an element-by-element manner. The elements in the Map exist in pairs (understand my husband and wife), and each element is composed of two parts, a key and a value, and the corresponding value can be found through the key. Here we can understand the key as the honest party, it can only correspond to one value (for example, a good girlfriend can only correspond to one boyfriend), and the value is the non-teacher party (a scumbag can correspond to many girlfriends)


int size();//返回有多少对元素
boolean isEmpty();
boolean containsKey(Object xx);//查询集合中是否包含以xx作为键的元素
boolean containsValue(Object xx);//查询集合中是否包含以xx作为值的元素
V get(Object key);//返回key所对应的值
V put(K key, V value);//向map集合中添加Key为key,Value为value的元素,当添加成功时返回null,否则返回value。
V remove(Object key);//删除Key为key值的元素。
void clear();
void putAll(Map<? extends K,? extends V> m)//向map中添加指定集合的所有元素
Set<Map.Entry<K,V>> entrySet()//返回map到一个set集合中
			int u=1;
			String j="杨帆";	 
			Map i =new HashMap();
			i.put(u, j);
			Set xx=i.entrySet();
			System.out.println(xx);
			//此时输出结果为[1=杨帆]
boolean equals(Object o)//判断集合元素是否相等
Set<K> keySet()//返回map集合中所有的键
			int u=1;
			String j="杨帆";	 
			int qq=2;
			String zz="女朋友";
			Map i =new HashMap();
			i.put(u, j);
			i.put(qq, zz);
			Set xx=i.keySet();
			System.out.println(xx);	
			//此时输出结果为[1, 2]
Collection<V> values()//返回map集合中所有的Value到一个Collection集合			
				
		
 

ArrayList
it can be seen as an upgraded version of an array, add some of the ways listed on the basis of the array size and its capacity is dynamic, efficient memory utilization
as inherited from the List interface includes a list of natural methods, but Explain more

public void trimToSize()//我们知道ArrayList 的 容量会随着已用容量的增大而增大,并且一直大于已用容量,这个方法就是根据已用容量消减总容量
public void ensureCapacity(int minCapacity)//预设容量,顾名思义,根据要装东西的容量事先设置好容量,提供内存利用率
public int indexOf(Object o)//找到o元素最早出现位置,如果不存在返回-1
public int lastIndexOf(Object o)//找到o元素最后出现位置,如果不存在返回-1
public Object[] toArray()//将Arraylist转化为一个数组

Linkedlist
LinkedList is also the implementation class of the List interface. It uses a linked list storage method (ArrayList is a linear list). Linked lists are much more efficient than linear lists in deletion and modification, but linear lists are much more efficient in querying, so choose the appropriate one according to your needs. Storage method
As shown in the above structure diagram, it can be inherited from list or queue, and you can choose whom to inherit according to your needs.

public void addFirst(E e)//队头加
public void addLast(E e)//队尾加
public E getFirst()//返回队首
public E getLast()//返回队尾
public E removeFirst()//返回队首的同时移除队首
public E removeLast()
public E peekFirst() //与peek功能相同
public E peekLast() 
public E pollFirst()//返回并且弹出队首
public E pollLast()
public void push(E e)//队首添加,add和offer都是队尾添加
public E pop()//弹出队首
public boolean removeFirstOccurrence(Object o)//功能与remove相同
public boolean removeLastOccurrence(Object o)//remove是从队头开始找,此函数是从队尾


Why does Hashmap talk about hashmap first? Because Hashset is implemented based on hashmap, because it inherits from the Map excuse, naturally includes his method, hashmap, as the name implies, is a hash table, which is to store data in the form of a hash table, because the hash function The existence, query, and modification efficiency are very high (without considering hash conflicts);

 public V getOrDefault(Object key, V defaultValue) //如果key在键中存在就返回key的值,不存在返回defaultValue
 public V putIfAbsent(K key, V value) //如果key在键中存在,就返回key对应的值,不存在就添加key-value
 public boolean replace(K key, V oldValue, V newValue) //取代某个键的值
 public V replace(K key, V value)//在指定的键已经存在并且有与之相关的映射值时才会将指定的键映射到指定的值(新值), 在指定的键不存在时,方法会return回来一个null 


The source code of Hashset is implemented based on hashmap, which is why the hashmap is put in front. It inherits from the set interface, so it has its corresponding methods.
It continues the characteristics of set, the elements cannot be repeated, and the other is that it does not maintain the insertion order and allows to contain null elements, but at most one. The common methods of hashset are basically inheritance and set, and it is not used much. know

Guess you like

Origin blog.csdn.net/jahup/article/details/105894940