当前位置:网站首页>Summary of collection: (to be updated)
Summary of collection: (to be updated)
2022-07-04 10:58:00 【Black demon fairy moon】
The class set has the following interfaces :Collection,List,Set,Map,Iterator,Enumeration,Queue,ListIterator
ps: It is very necessary to clarify these interfaces !!!
collection(interface): Interface of single value data
|collection Methods of interface definition | Method type |
| public boolean add(E e)|–|
public boolean add(E e) | |||
---|---|---|---|
public boolean addAll(Collection<? extends E> c) | |||
public void clear() | |||
public boolean contains(Object o) | |||
public boolean remove(Object o) | |||
public int size() | |||
public Object[] toArray() | |||
public Iterator iterator() |
The two most commonly used interfaces are add and iterator
List( Interface )
public interface List extends Collection
public E get(int index) | ||
---|---|---|
public public set (int index,E element) | ||
public ListIterator listIterator() |
The most common is get(index) Method ,
List: Elements have subscripts , Orderly , Element repeatable
List There are three subclasses :ArrayList,LinkedList,Vector
ArrayList
public class ArrayList extends AbstractList implements List,RandomAccess,Cloneable,Serializable
ArrayList Implementation relationship of interface :
ArratList and LinkedList The difference between ( Interview questions )
ArrayList:
The underlying data structure is an array , Memory space is continuous
Elements have subscripts , Orderly , Allow repetition
Usually subscript
Adding and deleting operations are slow , Query operation is relatively fast ( Large amount of data )
LinkedList:
The underlying data structure is linked list , Memory space is not continuous
Elements have subscripts , Orderly , Allow repetition
Usually carry out the first and last operations
Adding and deleting operations are faster , Query operation is slow ( When there's a lot of data )
Be careful :Linklist Not all queries are slow , The head and tail operation is still relatively fast
Insert a code chip here
set: Element has no subscript , disorder , Element is not repeatable ( Reference types are rewritten equals() and hashCode() Methodical , So if we write generic types by ourselves , in order to set The element of is not repeatable , You need to rewrite it yourself equals() and hashCode())
Map: Yes key value , disorder ,key Do not repeat , however value Can be repeated ( Reference types are rewritten equals() and hashCode() Methodical , So if we are generic key It's my own type , in order to keyt Non repeatability of , You need to rewrite it yourself equals() and hashCode())
HashMap:
HashSet: Packaged HashMap
List Iteration : Among them, according to list With subscript , Orderly features we can use for To iterate , in addition List Iterators that inherit the parent interface :iterator, In addition, it also has its own iterator listIterator(listIterator(int Index) It realizes the iteration from the beginning of the list )listIterator You can also iterate from back to front .
Iterations of sets :
List<String>list=new ArrayList<>();
list.add(" A dream of red mansions ");
list.add(" Water margin ");
list.add(" Journey to the west ");
list.add(" The romance of The Three Kingdoms ");
System.out.println("--------------------------- Traverse set one -------------------------------");
for (int i = 0; i <list.size(); i++) {
System.out.println(list.get(i));
}
System.out.println("--------------------------- Traversal Set II -------------------------------");
for (String i:
list) {
System.out.println(i);
}
System.out.println("--------------------------- Traverse set three -------------------------------");
Iterator it=list.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
System.out.println("--------------------------- Traversal set four traversal in reverse order -------------------------------");
ListIterator<String> it2 = list.listIterator();
while(it2.hasNext())
it2.next();
if(!it.hasNext()){
while(it2.hasPrevious())
System.out.println(it2.previous());
}
Note that the last case is traversal in reverse order , In reverse traversal , You must traverse to the last element in positive order before traversing in reverse order , Otherwise, the pointer of the iterator is in the first , The pointer cannot find the previous point , You can't iterate .
ListIterator<String> it2 = list.listIterator();
while(it2.hasPrevious())
System.out.println(it2.previous());
The result is :
--------------------------- Traversal set four traversal in reverse order -------------------------------
Map( The parent interface )HashMap( Subclass implementation )
Map Iteration :Map You cannot iterate directly , because Map There is no iterator , and hashMap Yes keySet and entrySet The two methods , So use the above two methods to iterate .
public static void main(String[] args) {
Map<Integer,String>map=new HashMap<>();
map.put(1," Bones jing ");
map.put(2," Pipa essence ");
map.put(3," Bubble essence ");
map.put(4," Carp essence ");
System.out.println("================= Iteration method 1 =======================");
Set<Integer> set1 = map.keySet();
Iterator<Integer> it1 = set1.iterator();
while(it1.hasNext()){
Integer key=it1.next();
System.out.println(key+"="+map.get(key));
}
System.out.println("================= Iteration mode 2 =======================");
Set<Map.Entry<Integer, String>> set2 = map.entrySet();
Iterator<Map.Entry<Integer, String>> it2 = set2.iterator();
while(it2.hasNext()){
Map.Entry<Integer, String> entry = it2.next();
//System.out.println(entry);
System.out.println(entry.getKey()+"="+entry.getValue());
}
}
边栏推荐
- Canoe - description of common database attributes
- JMeter assembly point technology and logic controller
- Open the neural network "black box"! Unveil the mystery of machine learning system with natural language
- [Galaxy Kirin V10] [server] grub default password
- Solaris 10网络服务
- Heartbeat报错 attempted replay attack
- [Galaxy Kirin V10] [server] iSCSI deployment
- Heartbeat启动后无反应
- PHP programming language (1) - operators
- Write a thread pool by hand, and take you to learn the implementation principle of ThreadPoolExecutor thread pool
猜你喜欢
20 kinds of hardware engineers must be aware of basic components | the latest update to 8.13
[Galaxy Kirin V10] [desktop and server] FRP intranet penetration
Seven examples to understand the storage rules of shaped data on each bit
JMeter common configuration components and parameterization
JMeter correlation technology
Dichotomy search (C language)
[Galaxy Kirin V10] [desktop] can't be started or the screen is black
Huge number (C language)
Article publishing experiment
[Galaxy Kirin V10] [server] set time synchronization of intranet server
随机推荐
unit testing
Replace() function
[Galaxy Kirin V10] [server] NUMA Technology
TS type gymnastics: illustrating a complex advanced type
Student achievement management system (C language)
Heartbeat报错 attempted replay attack
JMeter Foundation
Function introduction of canbedded component
Dichotomy search (C language)
[Galaxy Kirin V10] [server] set time synchronization of intranet server
/*Write a function to open the file for input, read the contents of the file into the vector container of string class 8.9: type, and store each line as an element of the container object*/
[machine] [server] Taishan 200
Linked list operation can never change without its roots
Appscan installation steps
For and while loops
If function in SQL
试题库管理系统–数据库设计[通俗易懂]
VPS安装Virtualmin面板
Using Lua to realize 99 multiplication table
re. Sub() usage