当前位置:网站首页>_ 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
*
边栏推荐
- Helsinki traffic safety improvement project deploys velodyne lidar Intelligent Infrastructure Solution
- 分享自己平时使用的socket多客户端通信的代码技术点和软件使用
- Servlet详解
- The style of the mall can also change a lot. DIY can learn about it!
- 镁光256Gb NAND Flash芯片介绍
- 教务系统开发(PHP+MySQL)
- sql优化的几种方式
- About the use of Aidl, complex data transmission
- AspNetCore&云效Flow持续集成
- Lecun predicts AgI: big model and reinforcement learning are both ramps! My "world model" is the new way
猜你喜欢
Practice of geospatial data in Nepal graph
Create raspberry PI image file of raspberry pie
Golang open source streaming media audio and video network transmission service -lal
Consumer and producer cases of inter thread synchronization (condition variable)

10款超牛Vim插件,爱不释手了
Gold three silver four, an article to solve the resume and interview

Navicat Premium 15 for Mac(数据库开发工具)中文版

Report on Hezhou air32f103cbt6 development board

说下你对方法区演变过程和内部结构的理解

Vscode有什么好用的插件?
随机推荐
What are the reasons why the game industry needs high defense servers?
商城风格也可以很多变,DIY 了解一下!
Prototype mode
Write one file to the marked location of another file
iVX低代码平台系列详解 -- 概述篇(一)
Mt60b1g16hc-48b:a micron memory particles FBGA code d8bnk[easy to understand]
Continuous integration of aspnetcore & cloud flow
元宇宙系统的概念解析
Principle analysis of ThreadLocal source code
《睡眠公式》:怎么治睡不好?
什么是NFT数字藏品?
Cocoapods installation in 2021
One minute to familiarize yourself with the meaning of all fluent question marks
Golang uses Mongo driver operation - increase (Advanced)
镁光256Gb NAND Flash芯片介绍
加载本地cifar10 数据集
Power representation in go language
Webgl and webgpu comparison [4] - uniform
深度学习 pytorch cifar10数据集训练「建议收藏」
说下你对方法区演变过程和内部结构的理解