当前位置:网站首页>_ 17 collection overview
_ 17 collection overview
2022-06-25 16:19:00 【weixin_ forty-eight million six hundred and forty-four thousand】
summary
Java A collection is a set of data that enables a program to store and manipulate elements that are not fixed . all Java Collection classes are all located in java.util In bag .
By comparing arrays and Java Collect tool classes to explain Java The necessity of collecting tool classes .
Inheritance system
Collection Is a collection , The two direct sub interfaces are List and set
List characteristic : Orderly repeatable , Ensure that the data is added in the same order as the data is taken out
Set characteristic : disorder Do not repeat , There is no guarantee that the data is added and taken out in the same order
List There are three subcategories :
ArrayList : At the bottom is an array , Queries and changes are extremely efficient
LinkedList : The bottom layer is a two-way linked list , Adding and deleting are more efficient
Vector : The bottom layer is also an array , Is thread safe , obsolete , It is not recommended to use , Has been ArrayList Instead of
Set There are two subclasses
HashSet : At the bottom is a hash table
TreeSet : At the bottom is a binary tree
- Collection
Collection As the parent of the collection class , therefore ,collection The method in , Is a method that all collection classes have
Common methods
public static void main(String[] args) {
LinkedList list = new LinkedList();
// ArrayList list = new ArrayList();
// add to To the tail
list.add(1);
list.add(11);
list.add(12);
list.add(13);
// Add to specified location
// list.add(index, element);
// Add to head
// list.push(e);
// list.addFirst(e);
// Tail add
// list.addLast(e);
// Number
System.out.println(list.size());
// Is it empty
System.out.println(list.isEmpty());
// Delete... According to subscript
list.remove(0);
// Delete according to data
list.remove(new Integer(11));
// Empty
list.clear();
// change
list.set(2, 111);
// obtain
list.get(2);
// Traverse
for (Object object : list) {
}
}- Usage mode
public static void main(String[] args) { // 100W Take data for example // Tail add long startTime = System.currentTimeMillis(); // addToArrayList(); pushToArrayList(); long endTime = System.currentTimeMillis(); System.out.println("ArrayList Add to complete , Time consuming : " + (endTime - startTime)); startTime = System.currentTimeMillis(); // addToLinkedList(); pushToLinkedList(); endTime = System.currentTimeMillis(); System.out.println("LinkedList Add to complete , Time consuming : " + (endTime - startTime)); // Tail add 100W ArrayList 29 LinkedList 169 // First add 10W ArrayList 599 LinkedList 10 } public static void pushToArrayList() { ArrayList list = new ArrayList(); for (int i = 1; i <= 100000; i++) { list.add(0, i); } } public static void pushToLinkedList() { LinkedList list = new LinkedList(); for (int i = 1; i <= 100000; i++) { list.addFirst(i); } } public static void addToArrayList() { ArrayList list = new ArrayList(); for (int i = 1; i <= 1000000; i++) { list.add(i); } } public static void addToLinkedList() { LinkedList list = new LinkedList(); for (int i = 1; i <= 1000000; i++) { list.add(i); } }
With ArrayList For example
Iterator
iterator
Be careful
forEach
- List
- ArrayList
LinkedList
Basic use
- Underlying implementation
- Node class
A linked list consists of nodes , Because it's a two-way list , So there are three attributes in the node
1 Saved data Object
2 Next node object Node type
3 Previous node object Node type
LinkedList class
In order to add more efficiency from beginning to end , stay LinkedList Class holds the first and last nodes
add to -add
take -get
get data
Get Method is just a way to simulate subscript acquisition , It is essentially a traversal operation
Just made a certain judgment , Decide whether to find the first half fast or the second half fast
- Set And sort
- TreeSet
- Comparable
because User The corresponding... Is not implemented Comparable Interface , So it's using TreeSet When , Will report a mistake
- Comparator
public static void main(String[] args) { // Pass the object of the comparator class into // TreeSet set = new TreeSet(new A()); // Anonymous inner class writing TreeSet set = new TreeSet(new Comparator () { @Override public int compare(Object o1, Object o2) { // o1 Is the element to be added // o2 It's the elements of the collection Integer i1 = (Integer) o1; Integer i2 = (Integer) o2; // The method return 0 Repeated description , Don't add // return Greater than 0 Value Indicates that the element to be added is larger than that in the collection , Just put it back // return Less than 0 Value Indicates that the element to be added is smaller than the element in the collection , Just put it forward return i2-i1; } }); set.add(1); set.add(2); set.add(3); set.add(4); System.out.println(set); } } // Comparator class class A implements Comparator{ @Override public int compare(Object o1, Object o2) { // o1 Is the element to be added // o2 It's the elements of the collection Integer i1 = (Integer) o1; Integer i2 = (Integer) o2; // The method return 0 Repeated description , Don't add // return Greater than 0 Value Indicates that the element to be added is larger than that in the collection , Just put it back // return Less than 0 Value Indicates that the element to be added is smaller than the element in the collection , Just put it forward return i2-i1; }
- List Sort
public static void main(String[] args) { List list = new ArrayList(); list.add(1); list.add(22); list.add(3); list.add(11); // This method will call the... Of the object Comparable Medium compareTo Method or Comparator Methods in interfaces // because Integer There is compareTo Method , And in ascending order , That's why you can use sort Method // For example, if you want to descending order, you can use sort Method overloading // Collections.sort(list); Collections.sort(list, new Comparator() { @Override public int compare(Object o1, Object o2) { // o1 Is the element to be added // o2 It's the elements of the collection Integer i1 = (Integer) o1; Integer i2 = (Integer) o2; // The method return 0 Repeated description , Don't add // return Greater than 0 Value Indicates that the element to be added is larger than that in the collection , Just put it back // return Less than 0 Value Indicates that the element to be added is smaller than the element in the collection , Just put it forward return i2 - i1; } }); System.out.println(list); list = new ArrayList(); list.add(new Student1(18)); list.add(new Student1(11)); list.add(new Student1(15)); list.add(new Student1(4)); // because Student1 It didn't come true comparable Interface So it can't be used sort Method // Collections.sort(list); // But you can use overloaded methods Collections.sort(list,new Comparator () { @Override public int compare(Object o1, Object o2) { return 0; } }); } } class Student1 { int age; public Student1(int age) { super(); this.age = age; }
summary
* Comparable : If treeSet When saving our own defined types in , Use Comparable
* Comparator : If treeSet When the type we write is not saved in , Then use Comparator To specify the collation
* such as Integer The default is ascending sort , If we need to sort in descending order , We can only use Comparator, Because we can't change Integer Source code
* But this time Integer There is Comparable The implementation of the interface , Equal to two comparisons exist , however Comparator High priority ,
* So it will be sorted according to the rules we define
* Opening and closing principle : Turn off for changes , For extension development
*
边栏推荐
- leetcode-8. 字符串转换整数 (atoi)
- Uniapp converts graphic verification codes in the form of file streams into images
- Classic deadlock scenario of multithreading and its solution (philosopher dining problem)
- Inter thread synchronization semaphore control
- AspNetCore&云效Flow持续集成
- Stop "outsourcing" Ai models! The latest research finds that some "back doors" that undermine the security of machine learning models cannot be detected
- 不要小看了积分商城,它的作用可以很大!
- 有哪些新手程序员不知道的小技巧?
- The textfield is encapsulated by the flutter itself, which causes the data display to be disordered when the data in the list is updated.
- Function and implementation of closures
猜你喜欢

Alvaria宣布客户体验行业资深人士Jeff Cotten担任新首席执行官

Read the configuration, explain the principle and read the interview questions. I can only help you here...

读配置、讲原理、看面试真题,我只能帮你到这了。。。
Prototype mode

Stop "outsourcing" Ai models! The latest research finds that some "back doors" that undermine the security of machine learning models cannot be detected
MySQL installation tutorial

B站付费视频使up主掉粉过万

Continuous integration of aspnetcore & cloud flow

What plug-ins are available for vscade?

Overall MySQL architecture and statement execution process
随机推荐
mysql整体架构和语句的执行流程
Constructor Pattern
GO语言-锁操作
leetcode-8. 字符串转换整数 (atoi)
不要小看了积分商城,它的作用可以很大!
In the wechat environment, H5 jumps to the specified page of the applet
Servlet详解
flutter
报错:homebrew-core is a shallow clone
Analysis of the concept of metacosmic system
Interviewer: your resume says you are proficient in mysql, so you say cluster / Union / overlay index, table return, index push down
Lecun predicts AgI: big model and reinforcement learning are both ramps! My "world model" is the new way
Resolve the format conflict between formatted document and eslint
10 Super VIM plug-ins, I can't put them down
LeCun预言AGI:大模型和强化学习都是斜道!我的「世界模型」才是新路
什么是骨干网
Introduction to MgO 256gb NAND flash chip
The release of autok3s v0.5.0 continues to be simple and friendly
Shuttle pop-up returns to the upper level
Converting cifar10 datasets