当前位置:网站首页>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]边栏推荐
- Golang string segmentation, substitution and interception
- Data analysis exercises
- 图像处理8-CNN图像分类
- 了解小程序的笔记 2022/7/3
- php-fpm软件的安装+openresty高速缓存搭建
- Transmit pictures with Base64 encoding
- Osgconv tool usage
- Why can void * be a general pointer
- UE4 source code reading_ Bone model and animation system_ Animation process
- Unity editor expansion - scrolling list
猜你喜欢

Three characteristics

数据分析练习题
![P1596 [USACO10OCT]Lake Counting S](/img/a7/07a84c93ee476788d9443c0add808b.png)
P1596 [USACO10OCT]Lake Counting S

十六进制编码简介
![[cloud native] introduction and use of feign of microservices](/img/39/05cf7673155954c90e75a8a2eecd96.jpg)
[cloud native] introduction and use of feign of microservices

Clion toolchains are not configured configure disable profile problem solving

KunlunBase MeetUP 等您来!

Unity interactive water ripple post-treatment

Dealing with duplicate data in Excel with xlwings

Graphics_ Learnopongl learning notes
随机推荐
Minimap plug-in
Golang中删除字符串的最后一个字符
Unity4.3.1 engine source code compilation process
Wpf: solve the problem that materialdesign:dialoghost cannot be closed
[audio and video] ijkplayer error code
Get to know unity2 for the first time
Gradle's method of dynamically modifying APK package name
Storage of data
Osgearth target selection
Un système de gestion de centre commercial pour la conception de cours de technologie d'application de base de données
Xlua task list youyou
Redis cluster series 4
Classes and objects
Unity interactive water ripple post-treatment
Installation of PHP FPM software +openresty cache construction
UE4 source code reading_ Mobile synchronization
matlab神经网络所有传递函数(激活函数)公式详解
十六进制编码简介
Go resolve ID card
[cloud native] introduction and use of feign of microservices


