当前位置:网站首页>Collection interface
Collection interface
2022-07-03 08:27:00 【bai259257】
package Review as a whole in the first seven days . Set system ;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.function.Predicate;
public class CollectionDemo01 {
public static void main(String[] args) {
//Collection Is the superclass of all single column sets ( The root class ), That is, all single column sets are inherited from Collection, Also inherited some of its methods
// There are also two set systems under it ,1.List Set system 2.Set Set system , Both set systems have unique characteristics , Here we mainly explain Collection Class
Collection<String> coll=new ArrayList<>(); // Here we use polymorphic form to create coll aggregate , such coll Only inherit from Collection Unique methods in the collection
//1.public void add(E e): Collection call add Method , Put the parameter element e Save to collection ,e The type of represents a generic , Must be consistent with the generics of the calling collection
Collections.addAll(coll," The small white "," Xiao Chen "," Xiaobao "," Dabao ");
System.out.println(coll);
// Expand knowledge : We know that the print object is actually the address value of the print object , But why print directly Coll A collection of objects , But you can directly print the elements in the set ? Here are some examples .
int[] arr=new int[5];
System.out.println(arr);
// Study this problem , We need to have a look at println Method , Its underlying source code is String Of valueOf Method converts an object to its string representation and valueOf Method first determines whether the object is empty , If it is blank, print directly "null" character string , If it is not empty, let the object call toString Method
So direct print object and print object call toString The result of the method is the same , Let's see toString The underlying source code
You can find toString Method is actually printing the object " Class name information [email protected]+ Hexadecimal number ", So our printed object will directly return the address value ArrayList Class overridden toString Method , Use toString Method uses rewritten logic , So it can print the elements in the set directly !
//2.public boolean remove(E e): Deletes the specified element , Returns whether the deletion succeeded , Delete the first element by default boolean removeResult = coll.remove(" The small white "); System.out.println(removeResult); // Expand knowledge : Think about why the first element is deleted by default ? // Through source code analysis :1. First, judge whether the parameter object is empty , If the passed parameter is null, Then traverse the set , Check whether there are also elements null value , If so, return to true, No element is null The value returns false; //2. Ergodic set , Let the parameter object call equals Method and each element to compare , If there are the same elements , Delete this element directly and return true, If the same element is not found after traversing the set, it returns false // Expand the problem : Why call with parameter object equals Methods to compare with each element , Instead of having every element call equals Method to compare with the parameter object ? // reason : We have judged above , Parameter object o It's definitely not empty to arrive here , So you can call equals Method , And if there is null Value cannot be called equals Method , Abnormal transactions , So we can only let the parameter object o To call equals Method // We can see through source code analysis ,remove The method is to traverse the set , That is, start from the first element , When there are the same elements and they are deleted, they are directly return 了 , So the first element is deleted by default !

//3.public boolean contains(Object o): Determine if there is o Elements , With the return true, No return false
boolean containsResult1 = coll.contains(" Xiao Chen ");
System.out.println(containsResult1); //true
boolean containsResult2 = coll.contains(" Little black ");
System.out.println(containsResult2); //false //4.public boolean addAll(Collection<? extends E>): Pass in a collection with the same data type as the calling object , Add elements from the parameter set to the caller set
ArrayList<String> list = new ArrayList<>();
Collections.addAll(list,"1","2");
coll.addAll(list);
System.out.println(coll); //[ Xiao Chen , Xiaobao , Dabao , 1, 2]//5.public T[] toArray(); This is a method overload , Calling the parameterless method returns a Object An array of types ,
// public T[] toArray(T[] a); The parameterized method needs to pass in an array of the specified type , Length doesn't matter , Returns an array of the specified type
Object[] objects = coll.toArray();
String[] strArray = coll.toArray(new String[0]);
System.out.println(Arrays.toString(objects)); //[ Xiao Chen , Xiaobao , Dabao , 1, 2]
System.out.println(Arrays.toString(strArray)); //[ Xiao Chen , Xiaobao , Dabao , 1, 2]
// Why does it matter that the specified array length ?
// Source code analysis :1. First, we will compare the length of the parameter array with the length of the set , If the array length is less than the collection length , Just through Arrays Class copyOf Method , Pass in the underlying array of the collection 、 The length of the collection array 、 The formal parameter specifies the data type ), It will return an array with the same length as the underlying array of the collection
// When the length of the parameter array is greater than the length of the set , It will be called System Class arrayCopy Method , Directly copy the elements in the set array to the parameter array . So we don't need to specify the length at all , Specify the data type 
//6.public int size(): Returns the number of valid elements in the current set
int collSize = coll.size();
System.out.println(collSize);//5//7.public boolean isEmpty(): Determine whether the current set is empty , Empty return true, Not empty return false
boolean collEmpty = coll.isEmpty();
System.out.println(collEmpty); //false//8.public boolean removeIf(Predicate<? super String>filter): Custom rule deletion method
//Predicate It's an interface , There is a rule method in the interface , We can simply understand it as implementing the methods in this interface to define the elements to be deleted , Here, the interface is implemented in the form of anonymous inner classes
coll.removeIf(new Predicate<String>() {
@Override
public boolean test(String s) { //s Represents each element in the collection
return s.equals(" Dabao "); // Delete the element named Dabao
}
});
System.out.println(coll); //[ Xiao Chen , Xiaobao , 1, 2]
coll.removeIf(new Predicate<String>() {
@Override
public boolean test(String s) {
return s.length()==2; // The deletion length is 2 The elements of
}
});
System.out.println(coll); //[1, 2]边栏推荐
- 100 GIS practical application cases (78) - Multi compliance database design and data warehousing
- C language - Introduction - essence Edition - take you into programming (I)
- Sequence of map implementation classes
- Dotween plug-in
- Storage of data
- 数据的存储
- Osgearth starry background
- 使用base64编码传图片
- jupyter远程服务器配置以及服务器开机自启
- Golang 字符串分割,替换和截取
猜你喜欢

Wpf: solve the problem that materialdesign:dialoghost cannot be closed

Kunlunbase meetup is waiting for you!

二进制转十进制,十进制转二进制

Detailed explanation of all transfer function (activation function) formulas of MATLAB neural network

數據庫應用技術課程設計之商城管理系統

Display terrain database on osgearth ball

Redis的数据结构

Clion toolchains are not configured configure disable profile problem solving

Basic operation and process control

Image processing 8-cnn image classification
随机推荐
One dimensional array two dimensional array (sort Max insert sort)
Use of ue5 QRcode plug-in
String class
MAE
Constraintlayout's constraintset dynamically modifies constraints
Huawei interview summary during the epidemic
Basic operation and process control
Golang string segmentation, substitution and interception
E: Unable to locate package ROS melody desktop full
[cloud native] introduction and use of feign of microservices
Un système de gestion de centre commercial pour la conception de cours de technologie d'application de base de données
Data analysis exercises
Redis cluster series 4
数据的存储
Chain length value
基于SSM的校园失物招领平台,源码,数据库脚本,项目导入运行视频教程,论文撰写教程
Minimap plug-in
Delete the last character of the string in golang
Jupyter remote server configuration and server startup
VIM learning notes from introduction to silk skating


