当前位置:网站首页>_ 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
*
边栏推荐
- What are some tricks that novice programmers don't know?
- Popular cross domain
- The release of autok3s v0.5.0 continues to be simple and friendly
- Tensorflow loading cifar10 dataset
- 转换Cifar10数据集
- 商城风格也可以很多变,DIY了解一下!
- 什么是NFT数字藏品?
- 不要小看了积分商城,它的作用可以很大!
- Golang open source streaming media audio and video network transmission service -lal
- What plug-ins are available for vscade?
猜你喜欢

Detailed explanation of IVX low code platform series -- Overview (I)

Helsinki traffic safety improvement project deploys velodyne lidar Intelligent Infrastructure Solution

分享自己平时使用的socket多客户端通信的代码技术点和软件使用

Alvaria announces Jeff cotten, a veteran of the customer experience industry, as its new CEO
MySQL installation tutorial

Learning notes of rxjs takeuntil operator

Don't underestimate the integral mall, its role can be great!

Nsurlsession learning notes (III) download task

Multiple decorators decorate a function
Introduction to database transactions
随机推荐
Go language - what is critical resource security?
Golang open source streaming media audio and video network transmission service -lal
Tensorflow loading cifar10 dataset
Uncover gaussdb (for redis): comprehensive comparison of CODIS
Check whether the port number is occupied
JS add custom attributes to elements
Leetcode topic [array]-34- find the first and last positions of elements in a sorted array
绕过技术聊'跨端'......
元宇宙系统的概念解析
Activation and value transfer of activity
One minute to familiarize yourself with the meaning of all fluent question marks
Rxjs TakeUntil 操作符的学习笔记
Several ways of SQL optimization
Blue Bridge Cup - practice system login
一文带你搞懂 JWT 常见概念 & 优缺点
Read the configuration, explain the principle and read the interview questions. I can only help you here...
《睡眠公式》:怎么治睡不好?
What exactly is a handler
教务系统开发(PHP+MySQL)
Converting cifar10 datasets