当前位置:网站首页>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());
}
}
边栏推荐
- [test theory] test the dimension of professional ability
- The last month before a game goes online
- Usage of with as
- Appscan installation steps
- 20 kinds of hardware engineers must be aware of basic components | the latest update to 8.13
- shell awk
- [Galaxy Kirin V10] [desktop] printer
- Canoe the second simulation engineering xvehicle 3 CAPL programming (operation)
- Heartbeat报错 attempted replay attack
- regular expression
猜你喜欢
Quick sort (C language)
Error C4996 ‘WSAAsyncSelect‘: Use WSAEventSelect() instead or define _ WINSOCK_ DEPRECATED_ NO_ WARN
Canoe - the second simulation project -xvihicle1 bus database design (operation)
Postman interface test
[test theory] test process management
JMeter assembly point technology and logic controller
20 kinds of hardware engineers must be aware of basic components | the latest update to 8.13
JMeter correlation technology
[Galaxy Kirin V10] [desktop] can't be started or the screen is black
Canoe - description of common database attributes
随机推荐
Locust installation
Capl: timer event
regular expression
Canoe - the second simulation engineering - xvehicle - 2panel design (principle, idea)
[Galaxy Kirin V10] [server] failed to start the network
re. Sub() usage
shell awk
[Galaxy Kirin V10] [desktop] printer
Canoe: what is vtsystem
[Galaxy Kirin V10] [server] iSCSI deployment
Learning XML DOM -- a typical model for parsing XML documents
Introduction to tree and binary tree
Canoe - the third simulation project - bus simulation-1 overview
Write a thread pool by hand, and take you to learn the implementation principle of ThreadPoolExecutor thread pool
Recursive method to achieve full permutation (C language)
Quick sort (C language)
Safety testing aspects
Day7 list and dictionary jobs
Digital simulation beauty match preparation -matlab basic operation No. 6
Snake (C language)